私はYiiで働いています。私のコントローラーでは、次のコードを使用して、モデルの属性値を POST 入力値で更新します。
$foo->attributes = $_POST['Foo'][$i];
これは、1 つを除くすべての属性をオーバーライドします。その単一のものをオーバーライドしない理由がわかりません。
テーブル構造:
price (decimal 11,2)
amount (int 11)
period (varchar 255)
amount
手動でもフィールドをオーバーライドできません。であるからint
ですか?私はint
以前に問題があったことはありません。
私はvar_dump()
両方の内容を確認し$foo->attributes
、それらは正しく、すべて入力されています。 in を$_POST['Foo'][$i]
上書きしません。amount
$foo->attributes
検証規則
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
常に整数でなければなりません。テスト値は 10、20、30 でした。
問題の例
var_dump( $foo->attributes );
var_dump( $_POST['Foo'][$i] );
$foo->attributes = $_POST['Foo'][$i];
var_dump( $foo->attributes );
以下を出力します。
//$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)
そこにはいくつかの追加フィールドがあります。たとえば、モデルには$_POST
配列にはないフィールドがいくつかありますが、それらはうまくマージされているようです。これらも追加する必要がありますか、それとも無関係ですか?