help with relations()

I’m trying to wrap my mind around how the relations() method works. For instance, in the Yii Guide (Relational Active Record

), this example is given for a MANY_MANY relationship in the Post model:

‘categories’=>array(self::MANY_MANY, ‘Category’, ‘tbl_post_category(post_id, category_id)’),

How is Yii creating this relationship? Is the method getting category_id’s from the Category model where the category_id exists in the tbl_post_category only after filtering on post_id? I’m pretty sure it’s doing this, but I don’t understand how. In the Post table referenced in the guide, the ID field is named “ID”, not post_id. All I can guess is that Yii is looking at the underlying SQL relationships to do the actual linking. Then again, the Guide states:

"it is recommended that primary-foreign key constraints are declared for tables that need to be joined"

Not required.

Anyone?

First, Scan all table in the databases when used gii build model.

Gii base on table foreign key then find relation. e.g.

The table tbl_post_category used innodb engine and assign foreign key post_id to table post.post_id and table category.category.category_id.

Last, When used gii build model for tabe post’s model. Gii auto automatic scan tbl_post_category and find relation and create


'categories'=>array(self::MANY_MANY, 'Category', 'tbl_post_category(post_id, category_id)')

, code on relations method in the Post model.