In dataprovider, I have a “sum(t.status=“accept”) as acceptstatus” and I want the result of acceptstatus to be displayed as a “value” attribute in the array of the column of the cgridview just like the value for project as shown below (eg. ‘value’=>’$data->projects->project_title’).
How am I able to do so?
Below is my codes:
DataProvider used for CGridView:
$dataProvider=new CActiveDataProvider('Approval', array(
'criteria'=>array(
'join'=>'LEFT JOIN project ON t.project_id=project.project_id',
'group'=>'t.project_id',
'select'=>'t.project_id, sum(t.status="accept") as acceptstatus, sum(t.status="reject" as rejectstatus',
),
));
Kindly check the query in PhpMyAdmin and ensure that it displays what you intend.
And each row corresponds to the parent table.
Then add the acceptStatus as a virtual attribute to AR model.
Add a rule to make it safe.That may be helpful when you decide to filter the records based on this
relational attribute.
class Approval extends CActiveRecord
{
public $acceptStatus;
public function rules()
{
return array(
array('acceptStatus','safe','on'=>'search'),
);
}
}
Set the together property to true in CDbcriteria.
$dataProvider=new CActiveDataProvider('Approval', array(
'criteria'=>array(
'join'=>'LEFT JOIN project ON t.project_id=project.project_id',
'together'=>true,
'group'=>'t.project_id',
'select'=>'t.project_id, sum(t.status="accept") as acceptstatus, sum(t.status="reject" as rejectstatus',
),
));
Then you make a column for acceptStatus in admin.php in the following way.