3

Cake PHP コードのマルチレベル関連テーブルに行き詰まりました。

私は次のモデルを持っています

多くの学生を抱えているガーディアンと、さまざまな学生が学費を持っています。2 人の学生を持つ保護者を作成する場合、関連付けられた 2 つの行を StudentFees テーブルに作成する必要があります。保護者を追加するときに2人の学生を追加することに成功しましたが、学生の2行の料金を追加する方法がわかりません。私のコードは以下の通りです。

    class Guardian extends AppModel {
    public $name = 'Guardian';

    public $recursive =2;
    public $hasMany = array(
            'Student' => array(
                'className'     => 'Student',
                'dependent'     => true
            )
    );

}
class Student extends AppModel {
    public $name = 'Student';

        public $hasMany = array(
            'StudentFee' => array(
                'className'     => 'StudentFee',
                'dependent'     => true
            )
    );
}
class StudentFee extends AppModel {
    public $name = 'StudentFee';
    public $belongsTo = array(
                    'Student' => array(
                    'className'     => 'Student',
                    'dependent'     => true
            )
    );
}

スチューデンフィーの詳細も保存してください。保護者と生徒の詳細を保存する SaveAssociated 関数を使用します。

4

3 に答える 3

3

私があなたを正しく理解していれば、これでうまくいくはずです:

Model::saveAll()が処理を行い、適切な方法を選択する必要があります。saveManyまたはsaveAssociated. さらに、foreignKeys が自動的に設定されるため、すべてがデータベースにきちんと挿入されます。

$this->Guardian->saveAll(array('Guardian' => array(
   [...],
   'Student' => array(
       0 => array(
          [here's your first Student],
          'StudentFee'  => array(
             0 => array(
                [here's your first StudentFee]
             )
          )
       )
    )
)));
于 2013-08-27T13:54:24.097 に答える
0

saveAllメソッドを使用できますsaveAssociated

于 2013-08-27T21:09:55.380 に答える
0

このような状況で常に使用するこの方法を試すことができます。

$this->Studentfee->create();

$this->Studentfee->set(array(
'field1' => 'value',
'field2' => 'value'
));

 $this->Studentfee->save();

そして、すべての学生料金をループに入れることができます

于 2013-08-28T06:08:34.790 に答える