0

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

配列は次のように表示されます。

[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
    )
)

私が書いたものが最適化されているかどうか、または改善できるかどうかはわかりません。

$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']);

コードは機能しますが、これは良い方法ですか?

4

1 に答える 1

1

これまでのところ、ソリューションは問題ありません。

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

も動作するはずです。どちらが読みやすいかを自分で決めてください。

于 2012-04-21T00:08:23.110 に答える