saveall()
功能起作用。我说这是神奇的,但我不能有足够的二氯甲烷或任何花哨的东西来让它产生火花。
所以我有四个不同的表 - 问题,评论,员工和答案。问题有很多答案,答案有一个评论。答案属于员工。这是我的模型中设置的内容:
答案型号
var $hasOne = array(
'Comment' => array(
'className' => 'Comment',
'foreignKey' => 'answer_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
var $belongsTo = array(
'Question' => array(
'className' => 'Question',
'foreignKey' => 'question_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Employee' => array(
'className' => 'Employee',
'foreignKey' => 'employee_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
评论模型
var $validate = array(
'answer_id' => array(
'numeric' => array(
'rule' => array('numeric'),
),
),
);
var $belongsTo = array(
'Answer' => array(
'className' => 'Answer',
'foreignKey' => 'answer_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
当我为我的数据运行saveall()
时,我收到一条错误消息,指出我的评论的answer_id不是数字。这就是我设置的数据树,就像我正在尝试保存的那样(使用$this->Answer->saveall($this->data);
:
[Answer] => Array
(
[1] => Array
(
[question_id] => 1
[employee_id] => 14
[result] => 7
[unique_hash] => t9279kn7vh
)
[3] => Array
(
[question_id] => 3
[employee_id] => 14
[result] => 6
[unique_hash] => t9279kn7vh
)
[4] => Array
(
[question_id] => 4
[employee_id] => 14
[result] => 3
[unique_hash] => t9279kn7vh
)
[5] => Array
(
[question_id] => 5
[employee_id] => 14
[result] => 2
[unique_hash] => t9279kn7vh
)
[6] => Array
(
[question_id] => 6
[employee_id] => 14
[result] => 7
[unique_hash] => t9279kn7vh
)
)
[Comment] => Array
(
[1] => Array
(
[user_id] => 14
[text] => test1
)
[3] => Array
(
[user_id] => 14
[text] => test2
)
[4] => Array
(
[user_id] => 14
[text] => test3
)
[5] => Array
(
[user_id] => 14
[text] => test4
)
[6] => Array
(
[user_id] => 14
[text] => test5
)
)
任何人都可以帮我弄清楚这里有什么问题吗?我很感激帮助。
编辑:我已经更新了$ data数组,使其键开始为零,增量为1(即array(0=>array(),1=>array(),2=>array());
等),这已经消除了验证错误,但数据仍未保存。
答案 0 :(得分:1)
试试这个:
$this->Answer->saveall($this->data['Answer']);
$this->Comment->saveall($this->data['Comment']);
由于某种原因,即使您定义了关系,cakephp有时也不会自动保存相关表的所有记录。我还在试图找出原因。
答案 1 :(得分:1)
SaveAll只是不保存其他相关表。我们完全按照你说的做,我们自己定义它们,我们甚至手动链接id。这有点痛苦,但它是最好的选择。