3

我卡在 Cake PHP 代码中的多级关联表上。

我有以下型号

有很多学生的监护人,不同的学生有他们的学生费。当我创建一个有 2 个学生的监护人时,必须为 StudentFees 表创建关联的 2 行。添加监护人时我成功添加了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
            )
    );
}

请帮我保存 studenFee 的详细信息。我使用保存监护人和学生详细信息的 SaveAssociated 功能。

4

3 回答 3

3

如果我理解正确,这应该可以解决问题:

Model::saveAll()应该为您处理并选择适当的方法,saveManysaveAssociated. 此外,它会自动设置您的外键,以便将所有内容都整齐地插入数据库。

$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

可以使用saveAllsaveAssociated方法。

于 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 回答