MySQL BIT value always shows true

Hi, i have a Model Cargo which has the following rules:




public function rules()

    {

        return [

            ...

            ['cargo_repicare', 'boolean'],

            ['cargo_replegal', 'boolean'],

            ['cargo_vigencia', 'boolean'],

            ['cargo_replegal', 'default', 'value' => 0],

            ['cargo_repicare', 'default', 'value' => 0],

            ...

        ];

    }



Whenever i create a new record of this model and set cargo_repicare or cargo_replegal to either 1 or 0 (should be “interpreted” as true or false as i understand) the data gets saved correctly in the DB. But when i try to show this data (update an existing Cargo Model for example, or even doing a query with QueryBuilder) these attributes are always true. I can see that the values are sent correctly via Yii::$app->request->post(), i’ve also checked the value after performing $model->save() and it shows “1” or “0” based on the value of a radio button i select as it should. But for some reason, when retrieving this data to display it in my form or other places it always shows “1” (true).

I have read some topics about this like this, and the issue seems to have been fixed via PR’s, but i still can’t make it work.

Any help is greatly appreciated, thanks.

This does not solve your problem but maybe it can help you.

For a recent project I was thinking to use the BIT data type for some flags columns in order to save space.

But searching about space requirement I found out that BIT do not bring any save over TINYINT

https://dev.mysql.com/doc/refman/5.7/en/storage-requirements.html#data-types-storage-reqs-numeric

TINYINT1 byte

BIT(M)approximately (M+7)/8 bytes

So BIT of 1 digit is exaclty 1 byte same as TINYINT

If you use BIT to save space, change it to TINYINT and all your problem will be solved.

Thanks for your answer, Roberto. You’re correct, i’m using the BIT data type in order to save space, i’ll take a look on other options a little bit more. But I’ll further look upon the option you provided as well.

You need to check your RDBS. If you have only ONE BIT column then you are correct, but I think that you get the benefit when you have MORE than one. Either MySQL and/or SQLserver, combines the BIT columns and stores them together up to 8. So 8 BIT columns is equal to TINYINT(1). 8+1 = TINYINT(2), 16+1 = TINYINT(3), etc.