2012-10-20 61 views
0

我有员工和计划之间的许多关系。 编辑员工时,我设法获取当前时间表并填充表单。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-不好。)

任何帮助极大的赞赏。

非常感谢!

+0

'$ this-> request-> data'是如何构成的?你能打印这个数组吗?如果结构正确,则可以使用saveAll。 – tyjkenn

+0

@tykenn,非常感谢。有效!。其实我试着先实现答案 - 还没有看到你的评论 - 并更新了我的问题。然而,这工作。不过,我想知道我的foreignKeys问题是否对保存为$ this-> Employee-> Schedule-> save($ this-> request-> data ...)有负面影响...再次感谢您。 –

回答

0

“通常,在使用hasOne,hasMany和belongsTo关联时,它的所有关键字” - CakePHP手册“。

您的员工数组必须传递一个id,并且计划数组需要有一个employee_id,以便蛋糕知道链接的模型是什么。

+0

非常感谢尝试发送密钥,但仍然没有运气,我认为它与我的两张桌子上的外键不同,请问你会看到更新的问题吗?增加一些表格的细节。 –

+0

如果没有太大的麻烦,我仍然很想知道我的foreignKeys问题是否对保存为$ this-> Employee-> Schedule-> save($ this-> request-> data ...)有负面影响...谢谢许多。 –