Yii Framework Forum: how to: CGridView with HAS_MANY or MANY_MANY relation - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

how to: CGridView with HAS_MANY or MANY_MANY relation Rate Topic: ***** 2 Votes

#1 User is offline   Taker 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 09-September 11

Posted 20 September 2011 - 12:29 AM

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:

Quote

PHP Error
htmlspecialchars() expects parameter 1 to be string, array given


anyone help~
0

#2 User is offline   Mukesh 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 107
  • Joined: 17-May 11
  • Location:India

Posted 20 September 2011 - 01:29 AM

outside CGridView -

foreach($model->businessTimes->id as $businessTimesId) 
    $businessTimesId[]=$businessTimesId->id; 
?> 

inside CGridView -

'value'=>implode(', ',$businessTimesId), 


check out this thread -
http://www.yiiframew...ry/page__st__20
1

#3 User is offline   Taker 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 09-September 11

Posted 20 September 2011 - 02:11 AM

View PostMukesh, on 20 September 2011 - 01:29 AM, said:

outside CGridView -

foreach($model->businessTimes->id as $businessTimesId) 
    $businessTimesId[]=$businessTimesId->id; 
?> 

inside CGridView -

'value'=>implode(', ',$businessTimesId), 


check out this thread -
http://www.yiiframew...ry/page__st__20


Wow, very thank you!!!
you help me a lot, appreciate :)
0

#4 User is offline   Taker 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 09-September 11

Posted 20 September 2011 - 04:15 AM

View PostMukesh, on 20 September 2011 - 01:29 AM, said:

outside CGridView -

foreach($model->businessTimes->id as $businessTimesId) 
    $businessTimesId[]=$businessTimesId->id; 
?> 

inside CGridView -

'value'=>implode(', ',$businessTimesId), 


check out this thread -
http://www.yiiframew...ry/page__st__20


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?

This post has been edited by Taker: 20 September 2011 - 04:32 AM

0

#5 User is offline   rinat.io 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 20-January 11

Posted 20 September 2011 - 05:53 AM

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',
                ),
        ),
));

1

#6 User is offline   rinat.io 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 20-January 11

Posted 20 September 2011 - 05:59 AM

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.
1

#7 User is offline   Taker 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 09-September 11

Posted 20 September 2011 - 11:36 PM

Wow, cool
I don't know it can pass a function like this
thanks a lot :lol:

View Postmaschingan, on 20 September 2011 - 05:59 AM, said:

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.

0

#8 User is offline   YearningForYii 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 21-September 12

Posted 15 October 2012 - 02:26 AM

Well this works fine,,,but how to use when we want to display these datas in two separate rows,not in the same row.




View PostTaker, on 20 September 2011 - 04:15 AM, said:

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?

0

#9 User is offline   yJeroen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 06-September 11
  • Location:The Netherlands

Posted 16 October 2012 - 01:00 AM

View Postmaschingan, on 20 September 2011 - 05:59 AM, said:

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.


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')
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users