Assume that you have teachers and students model.
teacher model has: - person_id, - id_teacher, - name, - surname, - bachelor, - master, ... etc student model has: - person_id, - id_student, - name, - surname, - age, - apartment
(person_id is a global id both of teachers and students)
Now we want a CArrayProvider like that
PERSON DETAILS
person_id, id, name, surname, bachelor, master, age, apartment 1 1 name1 surname1 yes no - - 2 2 name2 surname2 yes yes - - 3 1 name3 surname3 - - 23 engineer 4 2 name4 surname4 - - 25 doctor 5 3 name5 surname5 no yes - -
How to achieve that?
This is a code that I used for a project
$t1 = Teachers::model()->findAll(); $t2 = Students::model()->findAll(); //prepair column's header (easy way) if (isset($t1[0])) $h = array_flip(array_keys($t1[0]->attributes)); if (isset($t2[0])) $h = array_merge($h, array_flip(array_keys($t2[0]->attributes))); foreach ($h as $k => $v) { $h[$k] = null; } //collect the data using common key $res = array(); foreach ($t1 as $v) { $res[$v->person_id] = $h; $res[$v->person_id] = array_merge($h, $v->attributes); } //merge with second data collection $test = 0; foreach ($t2 as $v) { $res[$v->person_id] = array_merge(isset($res[$v->person_id]) ? $res[$v->person_id] : array(), $v->attributes); } //convert to CArrayDataProvider $dataProviderCompined = new CArrayDataProvider($res, array( 'pagination' => array( 'pageSize' => 10, ), )); //display the data on CGridView $this->widget('zii.widgets.grid.CGridView', array( 'id' => 'cost-grid-detail', 'dataProvider' => $dataProviderCompined, 'columns' => array( 'person_id', 'id', 'name', 'surname', 'bachelor', 'master', 'age', 'apartment' ), ));