Dear Ryaz
This is Seeni from chennai.
As our friend Softark suggested whatever the logic you have done to customize a field in CGrid,
You have to apply that logic at database level in the form of sql.
I tried to simulate your scenario.
Your requirements and demands may be complex.I may not be able to meet them.
Any way I hope this will help you.
I have two tables.
1.Table:group, Model:Group, attributes:id,name.
2.Table:worker, Model:Worker, attributes:[b]id,name,assigned/b,[b]completed/b,[b]g_id/b
One user belongs to a group through g_id.
Every worker is given some tasks(assigned).
How many of tasks they have completetd is known from completed.
Now we want to know the overall efficiency of a group.Of all the tasks assigned for a particular group,
we need to know ,how many have been completed.We want to dispaly it as efficiency in Group CGrid.
Group.php
class Group extends CActiveRecord
{
public $efficiency; //declared a virtual property efficiency.
public function rules()
{
return array(
array('name', 'required'),
array('name', 'length', 'max'=>64),
array('id, name,efficiency', 'safe', 'on'=>'search'),//Made that property safe in search.
);
}
public function search()
{
$sql="SUM(worker.completed)/SUM(worker.assigned)*100 "; //leave a space before the end of quotes.
/**
* We are using join directly .No need to bother about "with" or "together".
* We are grouping by group.id. So calculation done in $sql is applied within the group only.
* There is regExp check for user input. which is going to accept integer or float with prefixed operator signs.
*/
$criteria=new CDbCriteria;
$criteria->select="t.id,t.name,".$sql."AS efficiency";
$criteria->join="LEFT JOIN worker ON worker.g_id=t.id";
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->group='t.id';
if(isset($this->efficiency)&&preg_match('/^(>|<|<>|>=|<=|=|){1}(\d)*(\.)?(\d)*$/',$this->efficiency))
{
if(is_numeric($this->efficiency))
$criteria->having=$sql."=".$this->efficiency;
else $criteria->having=$sql.$this->efficiency;
}
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array("attributes"=>array("t.id","t.name","efficiency"=>array(
'asc'=>$sql."ASC",
'desc'=>$sql."DESC",
)),
)));
}
views/group/admin.php
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'group-grid',
'dataProvider'=>$model->search(),
'filter'=>$model
'columns'=>array(
'id',
'name',
'efficiency',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
I may be doing very simple thing. Situation in your hand may be complex.
Any way we can customize the situation playing with sql and also passing parameters to search function.
Regards.