CakePHP:保存相关数据 [英] CakePHP: saving associated data
问题描述
我试图保存IssueHistoryDescription,它属于IssueHistory。
所以IssueHistory有很多IssueHistoryDescription。
I'm trying to save IssueHistoryDescription, which belongsTo IssueHistory. So IssueHistory hasMany IssueHistoryDescription. This has all been set in the model.
然而,当我在IssueHistory中保存时,使用 $ IssueHistory-> save($ data) ;
Yet, when I save this in IssueHistory, using $IssueHistory->save($data);
(有或没有 $ IssueHistory-> create();
before ...)
(With or without a $IssueHistory->create();
before...)
Array
(
[IssueHistory] => Array
(
[id] => 22
)
[IssueHistoryDescription] => Array
(
[old_description] => OLD
[description] => NEW
)
)
不起作用,不保存任何内容。
It doesn't work, nothing is saved.
当我尝试使用 saveAssociated()
时出现错误:
When I try to use saveAssociated()
I get an error:
致命错误:不能在第2248行的/var/www/xdev/kipdomanager/cakephp/lib/Cake/Model/Model.php中将字符串偏移量用作数组
Fatal error: Cannot use string offset as an array in /var/www/xdev/kipdomanager/cakephp/lib/Cake/Model/Model.php on line 2248
推荐答案
您可以尝试:
$data = array(
'IssueHistory' => array('id' => 2),
'IssueHistoryDescription' => array(
array('old_description' => 'OLD', 'description' => 'new')
)
);
$IssueHistory->create();
$IssueHistory->saveAll( $data );
这篇关于CakePHP:保存相关数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!