Laravel 4.2 BIT数据类型问题 [英] Laravel 4.2 BIT datatype issue

查看:65
本文介绍了Laravel 4.2 BIT数据类型问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据库表中有一个bit(1)类型的列.但它没有按我预期的那样工作.

there is a bit(1) type column in database table. but its not working as i expected.

问题是

$invitee = new Invitee();
$invitee->name = "name1";
$invitee->email = "example@mail.com";
$invitee->isActive = 0;    // "b'0'", '0', false,   are also not working
$invitee->save();

每次尝试添加带有0的记录时,我都需要在isActive列中放入零0,但其获取值1.

I need to put a zero 0 in the isActive column but its getting value 1 every time when i try to add a record with a 0.

,我在此处找到一个问题..但是答案并没有说明问题的原因.很高兴有人可以解释这个问题.

and i found a question in here.. but the answers are not describing the cause of the problem. glad if someone can explain the issue.

推荐答案

具有bit类型字段意味着您每次插入/更新该字段时都需要使用原始值作为解决方法.

Having bit type field means that you need to use raw values as a workaround whenever you are inserting/updating that field.

这是因为默认情况下,PDO将绑定这些值并将它们视为字符串,因此bit将导致1:

That's because PDO by default will bind these values and they will be treated as strings, thus bit will result in 1:

DB::table('table')->insert(['bit_field' => 0]); // inserts 1
DB::table('table')->insert(['bit_field' => DB::raw(0)]); // inserts 0

如果可以的话,我建议将其更改为tinyint.

And I suggest changing it to tinyint if you could.

这篇关于Laravel 4.2 BIT数据类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆