Adding and deleting MANY_MANY relations

Hello again!!

I’m having problems with something that seemed simple at begin.

Which is the best way to add and delete objects from another in a Many-Many relation?

In other words, having Post and Tag, so having too 3 tables in database: post, tag, post_has_tag. Which is the best way to add an existing Tag to an existing Post?

The only way I could think of for now is create a method Post::addTag(tag:Tag) that create a database insert query by hand.

There is another better way?

PS: I’m still checking the extensions that recommended to me:

http://www.yiiframework.com/extension/eadvancedarbehavior/

and

http://www.yiiframework.com/extension/cadvancedarbehavior/

Thanks!!

Post::addTag/Post::removeTag is definitely the way to go IMO.

No need to complicate things.

Check out the Taggable extension, btw.

But thats probably overkill, unless you have several models which are taggable.

What my partner wanted to refer is how to add or delete on any MANY_MANY relations, Post-Tag was only an example.

Best Regards!

erama, there is no built-in way to do so. You can either look at extensions available or implement your own solution. We’ve planned this functionality for a future major version.

You could use this behavior:

http://www.yiiframework.com/extension/esaverelatedbehavior/