在多级关联表上保存多行-蛋糕PHP
我被困在Cake PHP代码中的多级关联表上 我有以下型号 监护人有很多学生,不同的学生有他们的学费。当我创建一个有2名学生的监护人时,必须为StudentFees表创建一个关联的2行。我在添加监护人时成功地添加了2名学生,但我不知道如何为学生添加2行费用。我的代码如下在多级关联表上保存多行-蛋糕PHP,php,cakephp,Php,Cakephp,我被困在Cake PHP代码中的多级关联表上 我有以下型号 监护人有很多学生,不同的学生有他们的学费。当我创建一个有2名学生的监护人时,必须为StudentFees表创建一个关联的2行。我在添加监护人时成功地添加了2名学生,但我不知道如何为学生添加2行费用。我的代码如下 class Guardian extends AppModel { public $name = 'Guardian'; public $recursive =2; public $hasMan
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功能保存监护人和学生的详细信息。如果我理解正确,这应该可以做到:
Model::saveAll()
应该为您处理它,并选择适当的方法,saveMany
或saveAssociated
。此外,它将自动设置您的外键,以便将所有内容整齐地插入数据库
$this->Guardian->saveAll(array('Guardian' => array(
[...],
'Student' => array(
0 => array(
[here's your first Student],
'StudentFee' => array(
0 => array(
[here's your first StudentFee]
)
)
)
)
)));
可以使用
saveAll
和saveAssociated
方法。您可以尝试使用此方法,我通常在以下情况下使用此方法:
$this->Studentfee->create();
$this->Studentfee->set(array(
'field1' => 'value',
'field2' => 'value'
));
$this->Studentfee->save();
您可以为所有学生费用设置一个循环请尝试
$student->saveAssociated($data,array('deep'=>true))代码>请参考