0
votes

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?

2

2 Answers

0
votes

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?

0
votes
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;
}