Sunday, January 12, 2014

Create CListView of a model class in another model's view (Yii)

Suppose we have two models: Project and User, in which each Project owns a number of Users. We wish to display a list of associated users in the project instance's view

Modify the protected/views/project/view.php by adding the following line in the script file:

<?php $this->renderPartial('_viewUser', array('project'=>$model)); ?>

Next create a partial view protected/views/project/_viewUser.php with the following codes for its implementation:

<?php 
$users=new CActiveDataProvider('User', 
    array(
     'criteria'=>array( 
      'condition'=>'audit_project_id=:projectId', 
      'params'=>array(':projectId'=>$project->id),
     ),
    ),
    array( 
     'pagination'=>array( 
      'pageSize'=>20,
     ), 
    )
   );
   
$this->widget('zii.widgets.CListView', array(
 'dataProvider'=>$users,
 'viewData'=>array('project'=>$project),
 'itemView'=>'/user/_view',
)); 
?>

No comments:

Post a Comment