Saving Many to Many Record

Hi all, I tried a lot on google to find some example that simplify the many-many relation, but I could not find exact answer.

So for my and others understanding, I’ll consider posts and categories example:

Three tables:

1- categories(cat_id, cat_name)

2- posts(post_id, post_name, post_content)

3- posts_categs(cat_id, post_id)

Now, I have created CRUD for both Categories and Posts table. GII generated forms for both Categories and Posts models.

At this point, saving Post and Category works fine but how do I implement the third table. I have specified many-many relations:

In category model:


'posts' => array(self::MANY_MANY, 'Posts', 'posts_categs(cat_id, post_id)'),

In posts model:


'categories' => array(self::MANY_MANY, 'Categories', 'posts_categs(cat_id, post_id)'),

What do I do to save their association? If you could give me some example or url, that would be great.

Thanks

Try using this extension: http://www.yiiframework.com/extension/esaverelatedbehavior/

Be sure to add ‘categories’ to your posts model validation rules




public function rules()

{

	return array(

		array('categories', 'safe'),			

	);

}



Thanks jmcmasterj

But I need to the standard way of saving Many-to-Many record.

Please visit the link

http://www.yiiframework.com/extension/save-relations-ar-behavior/#hh3

Hope will help you.

,Chander

There isn’t a built-in way to save many_many records, so you will need to use an extension or write it yourself.

I wrote some days before, if someone needed can use it: github.com/hastenax/yii-cmanymanyactiverecord

@hastenax, have just used your ManyManyActiveRecord - great stuff. Thank you.