2

I have functions for work with database in one of my controllers. Something like this:

static function getCamByClassId($id)
{
    $connection=Yii::app()->db;
    $command=$connection->createCommand("some query");
    $camReader=$command->query();
    $camList=$command->queryAll();
    return $camList;
}

Also I'm using zii widget. Like this:

    $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'someId',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        array(
            'name' => 'Image',
            'type' => 'raw',
            'value' => 'CHtml::image("/uploads/thumb_".$data->Lesson_ID."_".$data->Image, "" ,array(\'width\'=>195, \'height\'=>110))', 
        ,
        ),  
...
),));

Now I need to use other pictures there, that I get with help of this method. I tried to replace 'value' property on

'value' => 'CHtml::image(".$this->getCamByClassId($data->Lesson_ID)[0][\'Cam1\'].", "default image" ,array(\'width\'=>195, \'height\'=>110))',

And then I receive this exception:

Property "CDataColumn.getCamByClassId" is not defined.

There is no sense as I see to copy method into CDataColumn.php, because it looks for property and I can't set method return value to some property of this calss because it's result depends on $id parameter. How get I pass to 'value' property result of getCamByClassId?
UPDATE Ok, I can use it like LessonController::getCamByClassId, but how can I pass $data->LessonId then?
UPDATE1 tried this

'value' => 'CHtml::image(".$this->grid->controller->getCamByClassId($data->Lesson_ID).", "default image" ,array(\'width\'=>195, \'height\'=>110))',

It says: Object of class CGridView could not be converted to string UPDATE2 Checked last suggestion of topher

'value' => function($data, $row) {
    return CHtml::image(
        LessonController::getCamByClassId($data->Lesson_ID)[0]['Cam1'],
        "default image",
        array('width'=>195, 'height'=>110)
    );
}

My mistake: I had a typo. Now it says that 0 is undefined offset, gonna debug it.

Sergey Scopin
  • 2,217
  • 9
  • 39
  • 67

2 Answers2

2

$this in value refers to the CDataColumn instance and not your controller. Since your method is static and assuming your controller is autoloaded you can call the method using:

myController::getCamByClassId(...)

If not you can access it through CDataColumn's grid property which returns the CGridView object that has a controller property in turn:

$this->grid->controller->getCamByClassId(...)

For readability and easier maintenance, you can use an anonymous function instead:

'value' => function($data, $row) {
    return CHtml::image(
        LessonController::getCamByClassId($data->Lesson_ID)[0]['Cam1'],
        "default image",
        array('width'=>195, 'height'=>110)
    );
}
topher
  • 14,790
  • 7
  • 54
  • 70
0

you use $this->getCamByClassId but it is defined as static. So either remove the static attribute or use self::getCamByClassId.

Let us know if this works

Mark

mark
  • 344
  • 3
  • 8
  • `$this->myStaticMethod()` is [valid PHP code](http://stackoverflow.com/a/15707148/428543). Whether it is good practice is another issue. – topher Jul 06 '15 at 09:59