Manage many_many relations

Hi, f.e. I have next model


class Post extends CActiveRecord

{

    public function relations()

    {

        return array(

            'categories'=>array(self::MANY_MANY, 'Category',

                'tbl_post_category(post_id, category_id)'),

        );

    }

}

How I can add or remove relations between Post and Category models (entry in the tbl_post_category table) using my Post model class?

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

it might be what you need, if not please elaborate.

It works, thanks.

You simply just create a new model:


addCategory($category_id) {

$post_cat = new PostCategory();

$post_cat->category_id = $category_id;

$post_cat->post_id = $this->id;

$post_cat->save();

}

That should basically be it. :)

Also checkout this behavior, it handles this within a transaction:

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