我有员工和计划之间的许多关系。 编辑员工时,我设法获取当前时间表并填充表单。cakephp保存相关属于
保存时,我用:
if ($this->Employee->save($this->request->data)) {
$this->Employee->Schedule->save($this->request->data, $validate = false);
....
}
员工数据保存很好,但日程记录保持不变。
我实际上提供了id作为数组的一部分。在我的控制器上,debug($ this-> request-> data)显示:
array(
'Employee' => array(
'emp_position' => '1',
'name' => 'the name',
'emp_gender' => '1',
'emp_father' => '',
'emp_mother' => '',
'emp_dob' => 'October 13, 1964',
...
..
),
'Schedule' => array(
(int) 0 => array(
'id' => '7',
'ho_lu2' => '10:00',
'ho_ma2' => '01:00',
'ho_mi2' => '00:00',
'ho_ju2' => '00:00',
'ho_vi2' => '00:00',
'ho_do4' => '00:00'
)
)
)
。
***************** Update ************
我认为这与外键是我的两个表不同做
我的员工表:
CREATE TABLE IF NOT EXISTS `employees` (
`id` int(5) DEFAULT NULL,
`emp_appserial` int(5) unsigned NOT NULL AUTO_INCREMENT,
...
(请注意,在雇员表,emp_appserial是我PK autoincr和EMPLOYEE_ID可以为一些记录为空)
我的日程表:
CREATE TABLE IF NOT EXISTS `schedules` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`employee_id` int(5) unsigned NOT NULL,
...
自从我链接使用不同的领域,这些表中,我使用外键:
在我的模型,我联系: 员工型号:
var $hasMany = array(
'Schedule' => array(
'foreignKey' => 'employee_id', //(field on Schedule)
'dependent' => true
)
);
public $primaryKey = 'emp_appserial'
调度模型:
var $belongsTo = array('Employee',
'Employee' => array(
'foreignKey' => 'emp_appserial' //(field on Employee)
)
);
我发布的数据现在包括用于Schedule的employee_id和员工的emp_appserial
array(
'Employee' => array(
'id' => null,
'emp_appserial' => '119'
...
),
'Schedule' => array(
(int) 0 => array(
'id' => '6',
'name' => 'segundo horario',
'emp_appserial' => '119',
'employee_id' => '119'
)
)
)
(试图追加到安排两个ID和序列试试我luck-不好。)
任何帮助极大的赞赏。
非常感谢!
'$ this-> request-> data'是如何构成的?你能打印这个数组吗?如果结构正确,则可以使用saveAll。 – tyjkenn
@tykenn,非常感谢。有效!。其实我试着先实现答案 - 还没有看到你的评论 - 并更新了我的问题。然而,这工作。不过,我想知道我的foreignKeys问题是否对保存为$ this-> Employee-> Schedule-> save($ this-> request-> data ...)有负面影响...再次感谢您。 –