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

标签 php mysql cakephp

我是 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.奖励对象对我来说看起来很不寻常。有一个属性名称 dirty。我还在研究这个。但现在对我来说它似乎是一些 cakephp 数据库对象结构。

  • 这是 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/

    相关文章:

    sql - 使用 MySQL 查询选择最接近的数值

    cakephp - 更改密码不起作用 Cakephp 3.0.3

    php - MySQL 到 PHP 再到 XCode?

    php - Laravel - 将数组转换为 eloquent 集合

    php - PHP 数组中带有条件的 if 语句

    javascript - 保护 Javascript 中的 API key

    date - CakePhp FormHelper - 日期字段问题

    php - 通配符子域的单独资源

    php - 同一个表单如何向多个表提交数据

    php - 循环中的 SQL 查询累积每个循环