gpt4 book ai didi

php - Cakephp:同时插入 tinyint 字段。仅获取 "0"或 "1"

转载 作者:行者123 更新时间:2023-11-29 01:52:58 25 4
gpt4 key购买 nike

我是 cakephp 的菜鸟。在一个开源项目上工作。问题是:

当我为某个表(“is_adjusted”(tinyint))插入一个值时,我的 php 代码成功执行。但是该表仅将 0 或 1 作为其值。示例代码:

        $reward = $ta_customer_reward->newEntity();
$string_reward = var_export($reward, true);
$reward->customer_email = $some_preset_xyz;
$reward->reward_amount = $some_preset_xyz;;
$reward->particulars = $some_preset_xyz;
.. .. ..

// This is_adjusted is the culprit.
$reward->is_adjusted = 2;

$reward = $ta_customer_reward->save($reward);

现在,每当我将它保存(插入)到数据库中时,它都会存储为 1。我被困了三天。我检查过的东西:

  1. 数据库中没有 is_adjusted 的默认值。

  2. 没有其他函数覆盖该字段。

*** 1.奖励对象在我看来很不寻常。有一个属性名称脏。我还在研究这个。但现在在我看来它是一些 cakephp db 对象结构。

  1. 这是 cakephp v 3.xyz***

最佳答案

这是由 CakePHP 设计的。 CakePHP 始终将 tinyint(1) 视为 bool 值,因此它将始终将您的值转换为 true/false 因此 1/0

要解决此问题,请使用 tinyint(2) 作为您的列类型。记得清除模型缓存!


CakePHP 数据类型文档:
http://book.cakephp.org/3.0/en/orm/database-basics.html#data-types

关于此的博文:
http://blog.room34.com/archives/2649

类似问答:
CakePHP and tinyint as boolean

关于php - Cakephp:同时插入 tinyint 字段。仅获取 "0"或 "1",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36425408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com