I'd be tempted to unset them all THEN set the default (one less WHERE clause).
You could override the default save in the Model with (untested) : function save($date) { if (!empty($this->data[$this->alias]['default'])) { $this->UpdateAll(array("default"=>"0"); } parent::save($data); } On Jan 18, 5:07 pm, grigri <[EMAIL PROTECTED]> wrote: > Something like this? (This actually sets the others off AFTER setting > this one on, but it makes little difference). If you put the login in > beforeSave(), and the save fails, then the album won't have a default > photo. > > (code not tested, might be typos) > > class Photo extends AppModel { > var $name = "Photo"; > > var $belongsTo = array('PhotoAlbum'); > > function afterSave($created) { > if (!empty($this->data[$this->alias]['photo_album_id'])) { > if (!empty($this->data[$this->alias]['default'])) { > // We have set this photo to default, so unset the default > flag in all other photos in this album > $this->updateAll(array('default' => '0'), > array('photo_album_id' => $this->data[$this->alias] > ['photo_album_id']), 'id' => '<> ' . $this->id); > } > } > } > > } > > On Jan 18, 3:27 pm, Jaime <[EMAIL PROTECTED]> wrote: > > > Hello bakers! > > > Imagine this (common) scenario: a photo album that HasMany photos. A > > user may select one of them as a "cover" or "default" photo to > > illustrate the cover of the album. So a boolean field called "default" > > is set in the Photo model. Of course one and only one photo within > > that album should have the "default" bit set, because it's non-sense > > to flag more than one for the same album. > > > This leads to some extra logic within the controller in order to unset > > all other possibly previously flagged photos before setting the new > > one. This is not a big deal nor very important, but is not very > > beautiful either, is it? > > > As the great piece of software CakePHP is, perhaps there is the > > possibility to specify within the model that only one register within > > a given set can have a boolean field set. Or maybe can be defined > > somehow in the MySQL DB. > > > I hope this is not a too philosophical question :-) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---