我试图一次保存多个数据,但无法保存数据。
我有Post的模型和评论模型。
Post有很多评论。
$this->WhAreaAllocation->saveAll($tempArray, array('deep' => true));
$tempArray
如下:
array(
'post' => array(
(int) 0 => array(
'title' => 'title1',
'content' => 'content1',
'comment' => array(
(int) 0 => array(
'comment'=>'1st comment for post 1'
),
(int) 1 => array(
'comment'=>'2nd comment for post 1'
)
)
),
(int) 1 => array(
'title' => 'title2',
'content' => 'content2',
'comment' => array(
(int) 0 => array(
'comment'=>'1st comment for post 2'
),
(int) 1 => array(
'comment'=>'2nd comment for post 2'
)
)
)
)
)
答案 0 :(得分:0)
<强> Saving hasMany through data 强>
我不知道您是使用表单保存帖子评论,而且我不确定您使用的是哪个版本的 cakephp ,但我的解决方案是 cakephp2.x < / strong>。如果您想使用表单保存数据,那么创建这样的帖子表格,并保存在字段中有许多提到的相关型号名称。例如,这里你想保存相关的帖子评论见下面的代码
<?php echo $this->Form->create('Post'); ?>
<?php echo $this->Form->input('Post.title'); ?>
<?php echo $this->Form->input('Post.content'); ?>
<?php echo $this->Form->input('Comment.comment'); ?>
<button type="submit">Save</button>
<?php echo $this->Form->end(); ?>
使用此表单,您将获得这样的请求数据。
Array
(
[0] => Array
(
[Post] => Array
(
[title] => It is a post title
[content] => you post content
)
[Comment] => Array
(
[comment] => this is my post comment
)
)
)
或者如果你想使用表格保存数据,那么你必须创建这样的数据,并且还要看 CakePhp Save hasmany
Array
(
[0] => Array
(
[Post] => Array
(
[title] => It is a post title
[content] => you post content
)
[0] => Array
(
[Comment] => Array
(
[comment] => this is my post comment1
)
)
[1] => Array
(
[Comment] => Array
(
[comment] => this is my post comment2
)
)
)
)