tags:

views:

26

answers:

2

Ok, in CakePHP, I have the following code in my view add.ctp:

echo $this->Form->radio('color', array('1' => 'red', '2' => 'green', '3' => 'blue'), array('value' => false));

which results in the correct html:

<fieldset>
    <legend>Color</legend>
    <input type="radio" name="data[Some][color]" id="SomeColor1" value="1"  />
    <label for="SomeColor1">red</label>
    <input type="radio" name="data[Some][color]" id="SomeColor2" value="2"  />
    <label for="SomeColor2">green</label>
    <input type="radio" name="data[Some][color]" id="SomeColor3" value="3"  />
    <label for="SomeColor3">blue</label>
</fieldset>

If I check "green" for example, a debug($this->data); produces the expected result:

Array
(
    [Some] => Array
        (
            [color] => 2
        )

)

However, CakePHP inserts the wrong data in the table:

INSERT INTO `somes` (`color`) VALUES (1)

Any clue what's going on here? What am I missing?

EDIT:

  • I am using $this->Some->save($this->data) and the datatype of color is TINYINT(1) UNSIGNED NOT NULL DEFAULT 0.
  • For the sake of the example I removed the other data, but basically, everything else works just fine and the record is saved.
+2  A: 

This is because of TINYINT(1). Change it for example TINYINT(3) and your data will be saved correctly.

Nik
+2  A: 

cakePHP considers tinyInt(1) to be a Boolean so.. 0=0 and >0 =1

FatherStorm