Deleting one entry in a Many to Many relation

Hi everyone,

I have two models 'Gallery' and 'Picture', in a many-to-many relation (a gallery has many pictures, and a picture can belong to several galleries).

How can i update the mapping table Galley_x_Picture?

The user must be able to change the list of pictures assigned to a gallery, or delete them.

I know… that’s a noob question  ::)

You can use beforeSave() and beforeDelete() functions to handle this

Hi Will, can I find an example somewhere?

How do I change the mapping table in itself?

Something like: Gallery->Pictures->Delete() would delete the Picture object, but not the relation.

If i understand correctly you want to remove a picture from a specific gallery and not the picture record itself from the pictures table.

Then i suppose you should try something like that:



gallery_has_pics::model()->find(array('condition' => 'gal_id=:galid AND pic_id=:pid', 'params' => array(':galid' => $gallery_id, ':pid' => $picture_id)))->delete();


Just a thought…

Hi. Thanks for the reply, that's what I need!

Actually I thought I didn’t have to model the mapping table, but in that case it makes sense.  ;D

Ok it works great!

Thank you again!