Resolved Many_Many relationship with CAdvancedArBehavior and application.components.Relation Widget
5.01.2011
POSTED IN Programming TAGS : CAdvancedArBehavior, PHP, Yii
I spent so much time to accomplish little thing – MANY to MANY relationship to work with a Widget in Yii. (I wish to write as least code as possible).
Here is my solution:
Use this component, it depends on GHelper. You will also want the views files too.
Once you got them setup, the easiest way to get the MANY to MANY update (and create), do the followings:
1. You will need CAdvancedArBehavior.
2. Use the same relation name as the table name e.g.:
'Location' => array(self::MANY_MANY, 'Location', 'client_location(client_id, location_id)')
3. Simply use the widget in your view, e.g.:
$this->widget('application.components.Relation', array(
'model' => $model,
'style' => 'listbox',
'relation' => 'Location',
'fields' => 'name',
'showAddButton'=>false,
'htmlOptions' => array('style' => 'width: 100px;')
));
4. Don’t forget you need to give “safe” in the rules in your model of the attribute (‘Location’ in my case)
5. You may want to alter the $sycndb at around line 82 of CAdvancedArBehavior.php so that it will actually do things with your DB.

