Temporary solution
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'];
Question
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?
Solution 3
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'];
OTHER TIPS
check for your rules first for your scenario!
I suppose by attributes you mean one of your price amount period attributes, right. you can do like:
$foo->setAttributes(array(
'price' => $_POST['Foo']['price'],
'period' => $_POST['Foo']['period'],
'amount' => $_POST['Foo']['amount'],
));
or simply
$foo->amount = $_POST['Foo']['amount'];
UPDATE: try changing
$foo->attributes = $_POST['Foo'][$i]
to
$foo->setAttributes($_POST['Foo']);
I think you're using validation for fields price
, period
, but not for amount
. You have 2 way:
$foo->setAttributes($_POST['Foo'][$i], false)
- not recommendamount
to validation rules as: array('amount', 'safe')
, or better: array('amount', 'numerical', 'integerOnly'=>false),)
OR
You haven't amount
in attributes list of the model Foo