I'm working with Yii. In my controller I use the following code to update my model attribute values with my POST input values:
$foo->attributes = $_POST['Foo'][$i];
This overrides all my attributes, except one. I cannot figure out why it won't override that single one.
Table structure:
price (decimal 11,2)
amount (int 11)
period (varchar 255)
I cannot override the amount
field, not even manually. Is it because of it being an int
? I haven't had issues with int
before though.
I have used var_dump()
to check the contents of both $foo->attributes
and $_POST['Foo'][$i]
and they are correct and all filled in. It just won't override the amount
in $foo->attributes
.
Validation rules
array('period, price', 'required'),
array('amount', 'numerical', 'integerOnly'=>true),
array('period', 'length', 'max'=>255),
array('price, amount', 'length', 'max'=>10),
array('amount, period, price', 'safe', 'on'=>'search')
amount
should always be an integer. Testing values were 10, 20, 30.
Example of issue
var_dump( $foo->attributes );
var_dump( $_POST['Foo'][$i] );
$foo->attributes = $_POST['Foo'][$i];
var_dump( $foo->attributes );
Outputs the following:
//$foo->attributes
array (size=3)
'price' => string '140.00' (length=6)
'amount' => string '10' (length=2)
'period' => string 'monthly' (length=6)
//$_POST['Foo'][$i]
array (size=3)
'price' => string '150.00' (length=6)
'amount' => string '20' (length=2)
'period' => string 'yearly' (length=6)
//$foo->attributes after rebinding
array (size=3)
'price' => string '150.00' (length=6)
'amount' => string '10' (length=2)
'period' => string 'yearly' (length=6)
There are some additional fields in there. For example the model has a few more fields that the $_POST
array doesn't have, but they seem to merge nicely. Should I add these too or are they irrelevant?
In the end I used the following (temporary) solution, but it is very odd that I actually need to set the value twice before it works:
$foo->attributes = $_POST['Foo'][$i];
$foo->amount = $_POST['Foo'][$i]['amount'];