Get booleans as true or false instead of 1 or 0

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

In my ActiveRecord, I installed a logical validator. The save logic works as expected.

Now I wanted to say that when I call the Yii2 REST service, I return the boolean field as true or false instead of 1 or 0, because on the client side the structure has a strict comparison (===), and 1 is not the same as true.

Of course, I can rewrite 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.

+6
source share
1 answer

Inside afterFind, I would change the values ​​from 0 or 1 to true or false:

public function afterFind() { $this->booleanField = ($this->booleanField === 1); parent::afterFind(); } 
+1
source

All Articles