Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cakephp + enum support : unable to save or select enum 0 and 1

Tags:

enums

cakephp

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."'"
             );
like image 387
Subodh Ghulaxe Avatar asked Feb 19 '23 07:02

Subodh Ghulaxe


1 Answers

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.

like image 146
mark Avatar answered May 15 '23 06:05

mark