dinamicdataproviderbehavior Behavior that lets a model return CActiveDataProviders for its relations

  1. Requirements
  2. Usage
  3. Installation
  4. Changelog

This behavior lets a model return CActiveDataProviders for its relations in a dynamic way.

Requirements

Yii 1.1 or above

Usage

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:

<?php
//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?

<?php
//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')));
?>

Installation

  1. copy DynamicDataProviderBehavior.php to your 'components' folder (so that it is automatically loaded)
  2. Install it like any other behavior in your models: behaviors()
public function behaviors()
{
    return array(
        'dataProvider'=>array(
            'class'=>'DynamicDataProviderBehavior',
        ),
    );
}
  1. Use it:
<?php
//view.php
  $this->widget('zii.widgets.grid.CGridView', array('dataProvider' => $model->getDataProvider('relatedModel')));
?>

Changelog

0.4
  • Added support for 1 level through relations
    0.3
  • Fixed a bug that made the CDbCriteria ignore the relation criteria configuration
    0.2
  • DynamicDataProvider now allows the user to configure criteria, sort and pagination objects
    0.1
  • initial release
9 0
14 followers
936 downloads
Yii Version: 1.1
License: GPL-3.0
Category: Database
Developed by: Asgaroth
Created on: Feb 10, 2011
Last updated: 12 years ago

Downloads

show all

Related Extensions