nguyendh
(Duynguyen0511)
March 23, 2011, 9:42pm
1
Hi guys,
I jsut started learning Yii for last few days and I stuck on this problem.
Relationships are as follows:
Brokerage has many Agents and an agent belongs to a brokerage
An Agent belongs to A person and a person has one agent.
$dataProvider = new CArrayDataProvider($data=$model->agents, $config);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'name' => 'First Name',
'value' => '$data->person->first_name',
),
array(
//display a column with "view", "update" and "delete" buttons
'class'=>'CButtonColumn',
),
)
));
I got the error -> Property "Agent.id" is not defined.
If I run the code below,
$agents = $model->agents;
foreach($agents as $agent)
{
var_dump($agent->person->first_name);
var_dump($agent->person->last_name);
var_dump($agent->person->email);
var_dump($agent->person->home_phone);
var_dump($agent->person->mobile_phone);
}
I got what i want without error. Why the CGridView give me [Property "Agent.id" is not defined] error ?
Thanks
sbasuki
(Sriyono Basuki)
March 24, 2011, 1:31am
2
CArrayDataProvider need ID to identification unique column value for ID CGridView
sbasuki
(Sriyono Basuki)
March 24, 2011, 1:34am
3
This is my Example code using CArrayData Provider
$count=Yii::app()->db->createCommand('select count(*) from project_request where project_sts_id=3')->queryScalar();
$sql="select t.project_no as id, t.project_no, b.project_name,
sum(case when engpack_type='3D' then 1 else 0 end) 3d,
sum(case when engpack_type='2D' then 1 else 0 end) 2D,
sum(case when engpack_type='TOR' then 1 else 0 end) tor,
sum(case when engpack_type='OE' then 1 else 0 end) oe
from project_eng_pkg t, project b
where t.project_no=b.project_no
group by t.project_no";
$dataProvider=new CSqlDataProvider($sql, array(
'id'=>'id',
'totalItemCount'=>$count,
'sort'=>array(
'attributes'=>array(
'id','project_no', 'project_name', 'tor', 'oe', '2d', '3d'
),
),
'pagination'=>array(
'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),
),
));
SJFriedl
(Steve)
March 24, 2011, 3:22am
4
I’ve been working in the #yii channel with the OP, we found the problem.
Original code:
$config = array();
$dataProvider = new CArrayDataProvider($data=$model->agents, $config);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
...
But because he didn’t use ‘id’ as the primary key to his tables, we have to tell the CArrayDataProvider about it.
Rewritten code is:
$dataProvider = new CArrayDataProvider($model->agents, array(
'keyField' => 'agent_id' // Agent primary key here
));
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(...
This is one of the many reasons why it’s such a good idea to universally use ‘id’ as your database table primary keys.
nguyendh
(Duynguyen0511)
March 24, 2011, 7:04pm
5
I’ve been working in the #yii channel with the OP, we found the problem.
Original code:
$config = array();
$dataProvider = new CArrayDataProvider($data=$model->agents, $config);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
...
But because he didn’t use ‘id’ as the primary key to his tables, we have to tell the CArrayDataProvider about it.
Rewritten code is:
$dataProvider = new CArrayDataProvider($model->agents, array(
'keyField' => 'agent_id' // Agent primary key here
));
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(...
This is one of the many reasons why it’s such a good idea to universally use ‘id’ as your database table primary keys.
This solved my problem. Thumb up for Steve