Displaying master detail data

I have used Gii to create models/CRUD classes. I have a master-detail relationship in my model, where "…each employee has 0 or more tasks…". Within my Employee model I have the following method:




public function getAllEmployeesAndTasks()

{

  return $this->model()->with('tasks')->findAll();

}



I want to display the results in a view so that the employee’s name is displayed as a heading, and then the list of tasks for that employee is displayed. For example:





Employee 1

  Task 1 for employee 1

  Task 2 for employee 1

  Task 3 for employee 1


Employee 2

  Task 1 for employee 2

  Task 2 for employee 2

  Task ... for employee 2


Employee 3

  Task 1 for employee 3

  Task 2 for employee 3

  Task ... for employee 3




How can I achieve this?

  • Would I have to manually use loop/foreach constructs within the view?

  • Would it be possible to use zii.widgets.grid.CGridView to list the tasks for each employee?

  • Should I approach it by spending the time designing and implementing my own widget?

Any guidance/pointers on the best way to achieve this would be much appreciated.

Thanks in advance.

my opinion is:

you can use CListView for master data. and use partial view for datail.

in CListView you can send data for partial view.

http://www.yiiframework.com/doc/api/1.1/CListView




$this->widget('zii.widgets.CListView', array(

    'dataProvider'=>$dataProvider, // this data for master

    'itemView'=>'_viewdetail',   // refers to the partial view named '_viewdetail'

    'viewData'=>array('data'=>$datadetail), // this data for view detail

    

));




Hi Hermans,

Thank you very much for your reply. Unfortunately I’m stuck on the ‘viewData’ part. I have modified my code to use a CActiveDataProvider:




  $dataProvider = new CActiveDataProvider(Person::model()->with('actions'));

  $this->render('index', array('personTasks' => $dataProvider));



I can then use the CListView as follows:




<?

$this->widget('zii.widgets.CListView', array(

  'dataProvider'=>$personTasks,

  'itemView'=>'_personTasks',   // refers to the partial view named '_personTasks'

  //'viewData' => array('data' => $personTasks->actions),

  'sortableAttributes'=>array(

    'firstname',

    'lastname',

    'initials',

    'id',

  ),

));

?>



If I uncomment the ‘viewData’ line, I get the following error:




Property "CActiveDataProvider.Actions" is not defined.



I have noticed that within the _personTasks partial view $data->actions is accessible.

What should I populate viewData with?

Thanks.

use $data to get data from CListView, tobe $data->actions

you can send fk_id of Employee to personTasks partial view through dataView.




'viewData' => array('id' => $data->id), // fk from employee



and in your partial view:




TaskModel::model()->findByPk($id);