【问题标题】:determine if a model attribute is empty in yii判断yii中的模型属性是否为空
【发布时间】:2014-01-15 22:12:57
【问题描述】:
            'buttons'=>array(
            'catalog'=>array(
                'label'=>'Catalog tehnic',
                'imageUrl' => strlen($data->catalog) > 1 ?  Yii::app()->baseUrl . "/images/pdf.jpg" : Yii::app()->baseUrl . "/images/bw.png",
                'url'=>'$data->catalog',
            ),

如果设置了值,我正在尝试为 cgridview buttoncollumn 中的按钮设置不同的图标。 我尝试了不同的方法,结果都是一样的。

                'imageUrl' => strlen($data->catalog) > 1 ?  Yii::app()->baseUrl . "/images/pdf.jpg" : Yii::app()->baseUrl . "/images/bw.png",
                'imageUrl' => isset($data->catalog)  ?  Yii::app()->baseUrl . "/images/pdf.jpg" : Yii::app()->baseUrl . "/images/bw.png",
                'imageUrl' => !empty($data->catalog) ?  Yii::app()->baseUrl . "/images/pdf.jpg" : Yii::app()->baseUrl . "/images/bw.png",
                'imageUrl' => $data->catalog == '' ?  Yii::app()->baseUrl . "/images/pdf.jpg" : Yii::app()->baseUrl . "/images/bw.png",
                'imageUrl' => !$data->catalog ?  Yii::app()->baseUrl . "/images/pdf.jpg" : Yii::app()->baseUrl . "/images/bw.png",

它变得非常令人沮丧,所以请帮助我。提前谢谢

【问题讨论】:

  • 尝试使用 is_null( $data->attribute )
  • 它返回的一切都与我的其他尝试一样真实。如果我对“可见”数组元素进行任何检查,它们都可以正常工作,但是在“imageurl”元素处它们都返回真或假。我很困惑,他们唯一能找到的解释是 yii 不能在那里做检查。

标签: php yii cgridview


【解决方案1】:

CButtonColumn 类渲染按钮默认生成zii.widgets.grid.CGridView

读取类代码,只有urlvisible属性can be evaluated as PHP expression

该类中的imageUrl 属性被处理为纯字符串(不是 PHP 表达式)。

我做了这个实验。完全欢迎提出建议。希望对你有帮助:

protected/components 中创建一个新类:例如ExtCButtonColumn.php。这个类应该继承类CButtonColumn

class ExtCButtonColumn extends CButtonColumn
{
...
}

在类代码中,重写渲染方法,保持原有概念,避免破坏其他CGridViews上现有或遗留的实现:

protected function renderButton($id,$button,$row,$data)
{
    if (isset($button['visible']) && !$this->evaluateExpression($button['visible'],array('row'=>$row,'data'=>$data)))
            return;
        $label=isset($button['label']) ? $button['label'] : $id;
        $url=isset($button['url']) ? $this->evaluateExpression($button['url'],array('data'=>$data,'row'=>$row)) : '#';
        $options=isset($button['options']) ? $button['options'] : array();
        if(!isset($options['title']))
           $options['title']=$label;

            $imageUrl = isset($button['imageUrl']) ? $this->evaluateExpression($button['imageUrl'],array('data'=>$data,'row'=>$row)) : '';
            if ( $imageUrl == '')
               $imageUrl = isset($button['imageUrl']) ? $button['imageUrl'] : '#';
        if( $imageUrl != '#' )
           echo CHtml::link(CHtml::image($imageUrl,$label),$url,$options);
        else
           echo CHtml::link($label,$url,$options);
    }

最后在您的视图代码中,在小部件定义($this->widget 数组)中,在columns 数组中,添加一个类似的按钮定义(这是一个带有自定义按钮的示例)。现在可以在所有imageUrl 属性中使用PHP 表达式,例如urlvisible 属性:

array(
    'class'    => 'ExtCButtonColumn',
    'template' => '{process}{view}{update}{close}{back}',
    'buttons'  => array
     (
        'process' => array
        (
           'label'    => 'Procesar',
           'imageUrl' => 'strlen($data->catalog) > 1  ? Yii::app()->request->baseUrl . "/images/icons/myicon1.png" : Yii::app()->request->baseUrl . "/images/icons/myicon2.png"',   
     ...

【讨论】:

    【解决方案2】:

    我认为你可以这样做:

       'imageUrl' => '!empty($data->catalog) ?  Yii::app()->baseUrl . "/images/pdf.jpg" : Yii::app()->baseUrl . "/images/bw.png"'
    

    或者你可以这样做:

       'imageUrl' => function($data){
              $url = '';
              if(strlen($data->catalog > 1 ){
                 // do stuff
                 $url = $data->catalog; // assign url here
              elseif(){
                  // implement your logcic here
              }
              elseif{
    
              }
              return $url;
       }
    

    【讨论】:

    • 我尝试过 empty、isset、is_null、strlen、!$data->attribute。没有任何效果,它在所有实例上都返回 true 或 false。亚历杭德罗的方式最适合我的需要。在我看到他的答案之前,我实际上已经这样做了,但答案给了我一个更好的视角
    【解决方案3】:

    我想你现在应该试试这个

        'imageUrl' => 'strlen($data->catalog) > 1 ?  Yii::app()->baseUrl . "/images/pdf.jpg" :
     Yii::app()->baseUrl . "/images/bw.png"',
    

    将您的代码用引号括起来,然后它应该适合您

    【讨论】:

    • 我认为必须使用单引号,双引号!
    • @tinyByte 可能你是对的兄弟,但我从来没有遇到过这样的问题。
    猜你喜欢
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多