Cakephp:同时插入 tinyint 字段.只得到“0"或“1" [英] Cakephp: while inserting tinyint field. Getting only "0" or "1"

查看:59
本文介绍了Cakephp:同时插入 tinyint 字段.只得到“0"或“1"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 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;…………//这 is_adjusted 是罪魁祸首.$reward->is_adjusted = 2;$reward = $ta_customer_reward->save($reward);

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

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

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

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

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

解决方案

这是由 CakePHP 设计的.CakePHP 总是将 tinyint(1) 视为布尔值,因此它总是将您的值转换为 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 和 tinyint as boolean

I'm a noob in cakephp. Working on an opensource project. The issue is:

When I'm inserting a value for a certain table ( "is_adjusted" (tinyint)), the my php code executes successfully. But the table is only taking 0 or 1 as it's value. Sample code :

        $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);

Now whenever I save (insert) this in db, this is stored as 1. I'm stuck for three days. Things I've checked:

  1. No default value in db for is_adjusted.

  2. No other function is overwriting that field.

*** 1.The reward object looked quite unusual to me. There is a property name dirty. I'm still studying this. But for now it seems to me as some cakephp db object structure.

  1. This is cakephp v 3. xyz***

解决方案

This is by CakePHP's design. CakePHP always see tinyint(1) as boolean hence it will always convert your value to true/false hence the 1/0.

To overcome this issue, use tinyint(2) instead for your column type. Remember to clear your model cache!


CakePHP data type documentation:
http://book.cakephp.org/3.0/en/orm/database-basics.html#data-types

Blog post about this:
http://blog.room34.com/archives/2649

Similar Q&A:
CakePHP and tinyint as boolean

这篇关于Cakephp:同时插入 tinyint 字段.只得到“0"或“1"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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