我被困在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函数保存监护人和学生的详细信息.
解决方法:
如果我对您的理解正确,这应该可以解决问题:
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]
)
)
)
)
)));