tellegence
(Aaron Feng2010)
February 13, 2011, 5:12am
1
I have two tables,
Student (id, name);
Report_receipt (id, status);
They have MANY-MANY relationship.
Third table
Student_report_receipt (student_id, report_receipt_id)
I want to show a table with three attributes: student_id, student_name, report_receipt_status.
In the model of Student:
public function relations()
{
// add relationship for tbl_report_receipt
'reportreceipts' => array(self::MANY_MANY, 'reportreceipt',
'Student_report_receipt(student_id, report_receipt_id)',
),
);
}
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
// add for receipt_report
$criteria->compare('id',$this->id,true);
$criteria->compare('name',$this->name,true);
$criteria->with=array('reportreceipts');
$criteria->together = true;
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}
tellegence
(Aaron Feng2010)
February 13, 2011, 5:18am
2
In the admin view file (related to Student model)
$this->widget(‘zii.widgets.grid.CGridView’, array(
'id'=>'student-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'name',
array(
'header'=>'Report Receipt Status',
'name'=>'reportreceipt.status',
'value'=>'',
),
array(
'class'=>'CButtonColumn',
),
)
And the result of student/admin
is in the picture
tellegence
(Aaron Feng2010)
February 13, 2011, 5:24am
3
In the result of the table, the column of Report Receipt Status is empty.
I try to modify that code as following:
array(
'header'=>'Report Receipt Status',
'name'=>'reportreceipt.status',
'value'=>'$data->report_receipt->status',
),
But is does not work.
Can anyone help me got the status in table Report_receipt.status?
Thanks.
tri
(tri - Tommy Riboe)
February 13, 2011, 12:59pm
4
In the result of the table, the column of Report Receipt Status is empty.
I try to modify that code as following:
array(
'header'=>'Report Receipt Status',
'name'=>'reportreceipt.status',
'value'=>'$data->report_receipt->status',
),
But is does not work.
Can anyone help me got the status in table Report_receipt.status?
Thanks.
You named the relationship "reportreceipts". This should work:
'value'=>'$data->reportreceipts->status',
Edit: now tested with CGridView (a many_many relationship returns an array)
'value'=>'$data->reportreceipts[0]->status',
(CGridView seems to handle related data different than CListView, where the dot notation obviously works for many_to_many relationships.)
/Tommy
tri
(tri - Tommy Riboe)
February 14, 2011, 12:50pm
5
Songtao FENG, did you notice my update? (Immediately removed your PM after posting an answer pointing this out).
/Tommy
tellegence
(Aaron Feng2010)
February 15, 2011, 3:29am
6
Thanks very much, Tommy.
It works.
I may relate to 3D relationship in this case.