open-ecommerce.org
open-ecommerce.org

Reputation: 1814

Yii gridview use function in value

I am trying to use a function that i added to the controller inside the admin.php

This is the code

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'articles-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
            'DocType',
            array(
              'type' => 'image',
              'value' => $this->getArticleImage($data->PageImage),
            ),
            array(
                    'class'=>'CButtonColumn',
            ),
    ),

));

getArticlesImages works perfect at the view php but here I don't know if it is possible to use it....

In the getArticleImage i am using some information of the field PageImage to recreate the correct external path,

Any Ideas?

Thanks a lot

Upvotes: 1

Views: 1739

Answers (2)

Julz
Julz

Reputation: 73

Quoting the doc from Yii class reference, value has to be a string :

a PHP expression that will be evaluated for every data cell using evaluateExpression and whose result will be rendered as the content of the data cell.

A good practice is to use "view helpers", i.e. a collection of static methods, to put your getArticleImagefunction, so you can keep the View clean and light.

View Helper:

<?php
class ArticleViewHelper
{
    public static function getArticleImage($article)
    {
        return  ... your code goes here
    }
}

Then call it from the column definition:

array(
    'type'  => 'image',
    'value' => 'ArticleViewHelper::getArticleImage($data)',
)

This will work.

Upvotes: 3

Nisic Jovan
Nisic Jovan

Reputation: 309

Try this:

array(
    'type' => 'image',
    'value' => 'Yii::app()->controller->getArticleImage($data->PageImage)',
),

It should work.

Upvotes: 4

Related Questions