I have two tables Center and Class. the relation between this Centerclass. relation in Class,
'centers' => array(self::MANY_MANY, 'Center', 'centerclass(cl_id, cent_id)'),
and relation in Center,
'classes' => array(self::MANY_MANY, 'Class', 'centerclass(cent_id, cl_id)'),
Now if I have center id, how can I get all classes that associate with this Center.
Please give me criteria in Yii for this.
Thank you.
You can use the 'with()' method to apply a JOIN relation on the query:
$center = Center::model()->with('classes')->findByPk($centerId);
And get the classes with a foreach like this:
foreach($center->classes as $class){
echo $classes->cl_id; // Attribute that you like to show.
}
Edited:
To use in CGridView:
Controller:
$dataProvider = new CArrayDataProvider('Class');
$dataProvider->setData($center->classes);
Add $dataProvider in render to send it to the view.
More info in: http://www.yiiframework.com/doc/api/1.1/CDataProvider
Or directly in view:
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>new CActiveDataProvider('Class', array(
'data'=>$center->classes)
),
....
More info in: http://www.yiiframework.com/doc/api/1.1/CBaseListView