2012-01-21 7 views
2

編集することはできませんので、のようなものです:CakePHPの2.0:コントローラ内の配列データ(ポストからは)私は、コントローラ内の単純なCRUDアプリケーション、 と編集ページを持っている

function admin_edit($id = null) { 

     if (empty($this->data)) { 
      $this->data = $this->Product->read(); //product is the model 

     } else { 
      //its a post request, and $this->data is populated 
      debug($this->data);  

      //i force the id to another id 
      $this->data["Product"]["id"] = 115; 

      debug($this->data); //the data remains the same, doesnt change.. why? 

      //i will save this later   
     } 
} 

どちらのデバッグは、このの結果:

Array 
(
    [Product] => Array 
     (
      [id] => 8 
      [alias] => ME 
      [order] => 80 
      [open_close_images] => 1 
      [gallery_id] => 8 
      [video_id] => 2 
     ) 
) 

後:

Array 
(
    [Product] => Array 
     (
      [id] => 8 //it must be 115 now!! 
      [alias] => ME 
      [order] => 80 
      [open_close_images] => 1 
      [gallery_id] => 8 
      [video_id] => 2 
     ) 
) 

これはなぜですか?

cakephp 1.3ではうまくいきましたが、どのようにしてその配列を「ロック」することができるのか分かりません。 (再)このように、モデルのID VARを設定することで、IDを設定

答えて

3

試してみてください。

$this->Product->id = 115; 

適切にIDを更新する必要があります。

EDIT

あなたが他の値を更新しようとしている場合は、例えば、(それはCakePHPの2.0以降ということと呼ばれています)の代わりに$this->request->dataを使用します。

$this->request->data['Product']['id'] = 115; 
+0

は大丈夫そうそれがポイントということではありませんそのモデルのより複雑な関連データを変更する必要があるので、私はオブジェクトでそれを取得するとは思わない。申し訳ありませんが、私はその権利を説明していない場合 – apelliciari

+0

@avastregああ、まあ実際にあなたの質問から完全にはっきりしていなかった。上記の私の更新答えを見てください。 – Oldskool

+0

thx!一方、もっと深い検索で私はこれが本当に問題だったhttp://stackoverflow.com/questions/8203445/cakephp-2-request-data-and-modelを見つけました! – apelliciari

関連する問題