damusnet
damusnet

Reputation: 4398

Why doesn't my form save the correct value?

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:

Upvotes: 0

Views: 297

Answers (2)

FatherStorm
FatherStorm

Reputation: 7183

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

Upvotes: 3

Nik Chankov
Nik Chankov

Reputation: 6047

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

Upvotes: 2

Related Questions