2012-09-24 34 views
1

我想实现类似Yii CActiveDataProvider解析复杂表达式的方式。以下面的代码为例,我基本上希望能够在值中指定类似'date(“M j,Y”,$ data-> create_time)'“的东西。如何实现Yii CActiveDataProvider解析复杂表达式?

任何人都知道Yii中的哪个班级将提供良好的见解?我看了一下CDataColumn类,但没有多少运气。

$this-widget('zii.widgets.grid.CGridView', array(
'dataProvider'=$dataProvider, 
'columns'=array(
    'title',   // display the 'title' attribute 
    'category.name', // display the 'name' attribute of the 'category' relation 
    'content:html', // display the 'content' attribute as purified HTML 
    array(   // display 'create_time' using an expression 
     'name'='create_time', 
     'value'='date("M j, Y", $data-create_time)', 
    ), 
), 

));

+0

看起来像是在这里的答案? http://www.yiiframework.com/doc/api/1.1/CComponent#evaluateExpression-detail – user1693090

回答

0

是否要创建一个可以评估PHP表达式的小部件?

有这种方法evaluateExpression这也是由CDataColumn使用。您可以在方法renderDataCellContent中看到CDataColumn如何使用它。

正如您在方法evaluateExpression中看到的代码,它使用的是evalcall_user_func

如果你使用PHP 5.3,你可以使用匿名函数。例如

$this-widget('zii.widgets.grid.CGridView', array(
    'dataProvider' = $dataProvider, 
    'columns' = array(
     'title',   // display the 'title' attribute 
     'category.name', // display the 'name' attribute of the 'category' relation 
     'content:html', // display the 'content' attribute as purified HTML 
     array(   // display 'create_time' using an expression 
      'name' => 'create_time', 
      'value' => function($data){ 
       return date("M j, Y", $data->create_time); 
      } 
     ), 
    ), 
)); 
+0

感谢佩特拉的回应。基本上,我想创建自己的评估 – user1693090