Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get boolean values as true or false instead 1 or 0

I'm using a MySql database, so I defined the column type as Tinyint(1) in db schema.

In my ActiveRecord I've set boolean validator. Save logic is working as expected.

What I wanted now is that when I call Yii2 REST service, return boolean field as true or false instead 1 or 0, because on the client side the framework comes with strict comparison (===) and 1 is not the same as true.

Of course I could overwrite the value manually before sending the content, or on the client side before loading it into the model, but I would appreciate a cleaner solution.

like image 398
edrian Avatar asked Oct 04 '15 21:10

edrian


1 Answers

Inside afterFind i would modify the values from 0 or 1 to true or false:

public function afterFind() {
    $this->booleanField = ($this->booleanField === 1);
    parent::afterFind();
}
like image 125
marche Avatar answered Oct 02 '22 00:10

marche