pizwu
(Pizwu000)
1
I found that I can use BELONGS_TO relation to let CGridView get relative table contents, such as
$criteria->with = array('car');
but if I want to with a relation which is HAS_MANY or MANY_MANY, it cannot be show, because CGridView expect a string, not array.
so How can I let CGridView show these kind of HAS_MANY, MANY_MANY relation in a page?
here is my code:
relation
public function relations()
{
return array(
'businessTimes' => array(self::HAS_MANY, 'BusinessTime', 'link_id'),
);
}
criteria in search function:
public function search()
{
$criteria=new CDbCriteria;
...
$criteria->with = array('businessTimes');
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
and my view, admin.php:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'info-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
...
array(
'name'=>'businessTimes',
'value'=>'$data->businessTimes->id',
),
array(
'class'=>'CButtonColumn',
),
),
));
and get an error from showing admin page:
anyone help~
mukeshsoni
(Mukeshsoni)
2
outside CGridView -
foreach($model->businessTimes->id as $businessTimesId)
$businessTimesId[]=$businessTimesId->id;
?>
inside CGridView -
'value'=>implode(', ',$businessTimesId),
check out this thread -
http://www.yiiframework.com/forum/index.php?/topic/23671-stuck-on-displaying-lazy-relational-query/page__st__20
pizwu
(Pizwu000)
3
Wow, very thank you!!!
you help me a lot, appreciate 
pizwu
(Pizwu000)
4
I found that it is great solution for detail view because it just have 1 primary key
but now I’m using CGridView, which has a lot of primary key index
so I can’t just put an implode result for each row
how can I put each combination result for each row?
edit:
I got this:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'info-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
...
array(
'value'=>'$data->businessTime[0]->id',
),
array(
'class'=>'CButtonColumn',
),
),
));
which can print id at index 0, also, I can expand in several combination
businessTime[0], businessTime[1], businessTime[2], etc.
but is there a method that can work like "foreach", which can print out unknown numbers of array?
I mean, maybe I have 3 businessTime, but next row has 7 businessTime
how can I print all them out?
maschingan
(Maschingan)
5
Something like this:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'info-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
...
array(
'value'=>function($data) {
$timeId = array();
foreach ($data->businessTime as $bTime) {
$timeId[] = $bTime->id;
}
return implode(', ', $timeId);
}
),
array(
'class'=>'CButtonColumn',
),
),
));
maschingan
(Maschingan)
6
Or this:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'info-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
...
array(
'value'=>function($data) {
return implode(', ', array_keys($data->businessTime(array('index' => 'id'))));
}
),
array(
'class'=>'CButtonColumn',
),
),
));
The same thing in the end.
pizwu
(Pizwu000)
7
Wow, cool
I don’t know it can pass a function like this
thanks a lot 
Well this works fine,but how to use when we want to display these datas in two separate rows,not in the same row.
jeroen84
(Jeroendenhaan)
9
This is data handling. MVC concept suggests this function should be in your Model or BaseModel.
You can easily make a method so you can do something like: $data->getRelatedAttr(‘businessTime.id’)