Niels
(Niels)
September 5, 2012, 10:10pm
1
I want to render a php view file in the cell of a CGridView.
Therefore I wrote this code:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'value'=>array($this, 'renderPartial("_lineProblems")')
))));
But I get this error:
BookController and its behaviors do not have a method or closure named “renderPartial(’_lineProblems’ )”.
The stack trace shows this as the problem:
call_user_func_array(array(BookController, "renderPartial('_lineProblems' )"), array("data" => line, "row" => 0, 0 => CDataColumn))
I don’t understand the error. $this is an instance of BookController and BookController is a child of CController. CController has the methode renderPartial, so $this also has this method. How do I fix this?
abennouna
(Abennouna)
September 6, 2012, 5:53am
2
I’m even more surprised that you could declare an array as the ‘value’ property… Doc says ‘string’
http://www.yiiframework.com/doc/api/1.1/CDataColumn#value-detail
Normally, $this used in the context of a component refers to that component. You may want to read this: http://stackoverflow.com/questions/11250342/basic-yii-concept-where-is-this-defined
Niels
(Niels)
September 6, 2012, 7:42am
3
Passing an array is a feature:
http://www.yiiframework.com/doc/api/1.1/CComponent#evaluateExpression-detail
And $this refers to the controller, because $this is called in the view, not in the component.
abennouna
(Abennouna)
September 6, 2012, 8:11am
4
Good to know, thanks!
For the $this part, I guess I meant when someone would want to use it in a regular string value: ‘value’ => ‘$this->…’ then $this wouldn’t be the controller, but CDataColumn in this case.
Anyway , it’s not what you need, but I’ve learnt something new this morning, thanks.
I tried on some existing code, and doing this works:
'value' => function(){
$this->renderPartial('_lineProblems');
},
But layout disappears even with just render
Niels
(Niels)
September 6, 2012, 8:18am
5
The code you suggested doesn’t work here. I get this error:
Fatal error: Using $this when not in object context in view.php on line 112
abennouna
(Abennouna)
September 6, 2012, 8:30am
6
Weird, it worked for me. Which PHP and Yii versions do you have?
Niels
(Niels)
September 6, 2012, 8:35am
7
This works for me:
$controller = $this;
...
'value'=>function($data, $row) use ($controller){
return $controller->renderPartial('_lineProblems', NULL, true);
}
Thanks for your help.
I use Yii 1.1.9 with PHP 5.3.6.
abennouna
(Abennouna)
September 6, 2012, 8:42am
8
Here 5.3.5 with 1.1.10 – maybe there’s a difference, too lazy to check
And your layout is ok with your syntax?