Cakephp:在插入tinyint字段时.仅获得"0"或"1" [英] Cakephp: while inserting tinyint field. Getting only "0" or "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;
.. .. ..
// This is_adjusted is the culprit.
$reward->is_adjusted = 2;
$reward = $ta_customer_reward->save($reward);
现在,每当我将其保存(插入)到db中时,该存储为1.我被卡住了三天.我检查过的事情:
-
db中没有为is_adjusted的默认值.
-
没有其他函数可以覆盖该字段.
*** 1.奖励对象在我看来很不寻常.有一个肮脏的属性名称.我还在研究这个.但就目前而言,在我看来,它似乎是一个cakephp db对象的结构.
- 这是cakephp v3.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为布尔值 >
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:
No default value in db for is_adjusted.
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.
- 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屋!