Laravel 4.2 BIT datatype issue(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();
我需要在 isActive
列中放一个零 0
但每次我尝试添加记录时它的值 1
一个 0
.
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 数据类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 4.2 BIT 数据类型问题


基础教程推荐
- 带有WHERE子句的LAG()函数 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01