marcocamejo
marcocamejo

Reputation: 828

Yii - CDetailView value using model function (which uses model attribute)

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?

Upvotes: 0

Views: 3554

Answers (1)

Pitchinnate
Pitchinnate

Reputation: 7556

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() {
    ...
}

Upvotes: 1

Related Questions