Janka
Janka

Reputation: 1988

yii2 how ovverride integer fields into model?

I read here

https://github.com/yiisoft/yii2/issues/6797

it's possible override a field, to transfer it into integer

i want force ID to number

i try to add into model this function:

public function fields()
{
$fields = parent::fields();
    $fields = [
        'ID' => 'integer',
        'CODICE_SPEEDY',
        'DESCRIZIONE',
        'DESCRIZIONE_COMPLETATO',
    ];
return $fields;
}    

but yii2 crash: Getting unknown property: app\models\Interventi::integer

Can you help me?

Upvotes: 0

Views: 640

Answers (2)

Janka
Janka

Reputation: 1988

public function fields()
{
$fields = parent::fields();
    $fields = [
        'ID' => function ($model) {
             return intval($model->ID);
        },
        'CODICE_SPEEDY',
        'DESCRIZIONE',
        'DESCRIZIONE_COMPLETATO',
        'SERVIZIO', 'ASSEGNAZIONE', 'PIANIFICAZIONE', 'ESECUZIONE', 'ATTESA_CLIENTE', 'COMPLETATO'
    ];

return $fields;
}    

Upvotes: 0

Mihai P.
Mihai P.

Reputation: 9357

Fields are used for renaming properties. They are not use for type casting. with this

'ID' => 'integer',

you are tying to make it possible to call $model->ID and that will show the table column 'integer'

When you say to force what do you mean? that is something your database should do, or the model validation. You want to force to get an integer from what / where?

Upvotes: 0

Related Questions