2012-04-21 18 views
0

CakePHPに複数のレコードを保存して配列内にリレーショナルデータ用のidを含む配列要素を挿入しようとしています。配列の配列内に配列要素を挿入する最も良い方法

これは、配列がどのように表示されるかである:

[Venue] => Array (
    [0] => Array (
     [name] => Great mansion 
     [where] => New York 
    ) 
    [1] => Array (
     [name] => Diamond palace 
     [where] => London 
    ) 
    [2] => Array (
     [name] => Palazzo Falcone 
     [where] => Bologna 
    ) 
) 

私は、配列の各要素にarchitect_idを追加したい:

[Venue] => Array (
    [0] => Array (
     [name] => Great mansion 
     [where] => New York 
     [architect_id] => 33 
    ) 
    [1] => Array (
     [name] => Diamond palace 
     [where] => London 
     [architect_id] => 33 
    ) 
    [2] => Array (
     [name] => Palazzo Falcone 
     [where] => Bologna 
     [architect_id] => 33 
    ) 
) 

私はわからないんだけど、私は「何場合書いたVEのは、最適化されているか、それを向上させることができます。

$tot = count($this->request->data['Venue']); 

for ($i = 0; $i < $tot; $i ++) { 
    $this->request->data['Venue'][$i]['architect_id'] = $this->Architect->id; 
} 

$this->Venue->saveAll($this->request->data['Venue']); 

コードは動作しますが、これはそれを行うには良い方法ですか?

答えて

1

解決策は今のところ問題ありません。

foreach ($this->request->data['Venue'] as &$venue) { 
    $venue['architect_id'] = $this->Architect->id; 
} 

でも動作します。自分自身を決めてください。どちらが読みやすくなっていますか?

+0

私はこのソリューションを選択しました。ありがとうございます。 – vitto

関連する問題