doudao1369 2014-02-19 18:30
浏览 39
已采纳

Yii - 使用模型函数的CDetailView值(使用模型属性)

Certain class has a property called status (that can be 0 or 1). In the corresponding model I have defined two variables STATUS_CLOSED = 1 and STATUS_OPEN = 2.

I am using a CDetailView to display model info inside a "View" view like:

$this->widget('zii.widgets.CDetailView', array(
    'data'=>$model,
    'attributes'=>array(
        'account_number',
        'account_type',
        array(
            'label'=>'Banco',
            'type'=>'raw',
            'value'=>CHtml::encode($model->bank->bank_name),
        ),
    ),
));

I have defined these two functions in my model:

public function statusLabels()
{
    return array(
        self::STATUS_CLOSED => 'Inactiva',
        self::STATUS_OPEN   => 'Activa',
    );
}

public function getStatusLabel($status)
{
    $labels = self::statusLabels();

    if (isset($labels[$status])) {
        return $labels[$status];
    }

    return $status;
}

I need to customize the CDetailView (possibly using these two functions) to display the corresponding label, depending on the status value.

I thought this would work:

$this->widget('zii.widgets.CDetailView', array(
    'data'=>$model,
    'attributes'=>array(
        'account_number',
        'account_type',
        array(
            'label'=>'Estado',
            'type'=>'raw',
            'value'=>$model->statusLabel($model->status),
        ),
    ),
));

But I get: Missing argument 1 for BankAccount::getStatusLabel()

What I am doing wrong?

  • 写回答

1条回答 默认 最新

  • dongyilu3143 2014-02-19 19:21
    关注

    Ok so first off you don't need to send in the status for a model since the model already knows its own status so I would change your function to this:

    public function getStatusLabel() {
        $labels = self::statusLabels();
    
        if (isset($labels[$this->status])) {
            return $labels[$this->status];
        }
    
        return $this->status;
    }
    

    So then your widget would just be like this:

    $this->widget('zii.widgets.CDetailView', array(
        'data'=>$model,
        'attributes'=>array(
            'account_number',
            'account_type',
            array(
                'label'=>'Estado',
                'type'=>'raw',
                'value'=>$model->statusLabel
            ),
        ),
    ));
    

    Also it doesn't cause an error but in reality you should make the function statusLabels() a static function.

    public static function statusLabels() {
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探