Cakephp +枚举支持:无法保存或选择枚举0和1 [英] Cakephp + enum support : unable to save or select enum 0 and 1

查看:109
本文介绍了Cakephp +枚举支持:无法保存或选择枚举0和1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我保存具有两个枚举字段的数据以管理来管理消息的状态,即用户读取或未读取。我使用枚举('1','0')来管理状态'1'=>读取和'0'=>未读



但在状态栏中保存空文件夹

  $ data = array(
'message'=>'test message' ,
'status'=> 1
);

$ this-> Message-> save($ data);

数据库结构如下

 字段类型Collat​​ion Null键默认值
------------------ ------------- - ---------------- ------ ------ -------
id bigint(20)(NULL)NO PRI NULL)
message varchar(255)(NULL)NO MUL(NULL)
status enum('0','1')latin1_swedish_ci NO MUL 0
/ pre>

即使我已使用数据数组

  $ data = array(
'message'=>'test message',
'status'=>'1'
);


$ data = array(
'message'=>'test message',
'status'=>'.1
);


解决方案

你使用cakephp - 不支持ENUM。
,在你的情况下,甚至使用枚举是错误的。枚举用于两个以上的状态,应该被模拟为ArrayDatasource或者在你的模型中(像我一样)。



但是read / unread是一个布尔

 

code> tinyint(1)[unsigned] [default 0]

是一个布尔值,它会将表单字段转换为复选框。


When I am saving data having two enum fields to manage to manage status of message i.e. read or unread by user. I am using enum ('1','0') to manage status '1' => read and '0'=> unread

following code will save the message but in status column saving empty filed

$data = array(
              'message' => 'test message',
              'status' => 1
             );

$this->Message->save($data);

database structure is as follow

Field                Type                  Collation          Null    Key     Default 
------------------  -------------        -----------------  ------  ------  -------  
id                   bigint(20)            (NULL)             NO      PRI     (NULL)   
message              varchar(255)          (NULL)             NO      MUL     (NULL)                                                         
status               enum('0','1')         latin1_swedish_ci  NO      MUL     0 

even I have used data array as

$data = array(
              'message' => 'test message',
              'status' => '1'
             );


$data = array(
              'message' => 'test message',
              'status' => "'".1."'"
             );

解决方案

you are using cakephp - it does (as documented) not support ENUM. and in your case it is wrong to even use enums in the first place. Enums are used for more than two states and should be emulated as ArrayDatasource or in your model (as I do).

But "read/unread" is a boolean (two definite states!).

and there is an easy way to accomplish this correctly:

tinyint(1) [unsigned] [default 0]

cake will automatically assume this is a boolean and will transform the form field for it into a checkbox.

这篇关于Cakephp +枚举支持:无法保存或选择枚举0和1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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