This behavior lets a model return CActiveDataProviders for its relations in a dynamic way.
Yii 1.1 or above
Often you find yourself in the need to show a list of a model related objects, and of course you'll want to use built-in tools like CGridView or CListView to display this data.
But this Widgets require a CDataProvider to render its items, needing you write something like the following in your controller:
//BarController.php public function actionFoo(){ $model = $this->loadModel(); $criteria = new CDbCriteria; $criteria->condition = "parent_id = {$model->id}"; $dataProvider = new CActiveDataProvider('relatedModel', array( 'criteria' => $criteria, )); $this->render('view', array( 'model' => $model, 'dataProvider' => $dataProvider, )); } //view.php $this->widget('zii.widgets.grid.CGridView', array('dataProvider' => $dataProvider));
Wouldn't it be easier if you could do this instead?
//BarController.php public function actionFoo(){ $model = $this->loadModel(); $this->render('view', array( 'model' => $model, )); } //view.php $this->widget('zii.widgets.grid.CGridView', array('dataProvider' => $model->getDataProvider('relatedModel')));
public function behaviors() { return array( 'dataProvider'=>array( 'class'=>'DynamicDataProviderBehavior', ), ); }
//view.php $this->widget('zii.widgets.grid.CGridView', array('dataProvider' => $model->getDataProvider('relatedModel')));
Total 4 comments
I offer the following code change to make this extension support MANY_MANY relations (from line 79):
I have to change like 30 to
php $criteria->compare($relations[$relation][2], $this->owner->{$this->owner->getTableSchema()->primaryKey});For it to work.
Yeah! In Like to bother people on purpose :)
pd: not really, my english just sucks
For some reason the misspelling of 'dynamic' is really bothering me! Was it on purpose?
Leave a comment
Please login to leave your comment.