CakePHP saveAll()方法仅保存传递数组的最后一个元素
我试图在CakePHP中执行saveAll,但由于某些原因,只保存数组的最后一个元素。下面是我正在使用的代码生成数组并保存它:CakePHP saveAll()方法仅保存传递数组的最后一个元素,php,mysql,cakephp,Php,Mysql,Cakephp,我试图在CakePHP中执行saveAll,但由于某些原因,只保存数组的最后一个元素。下面是我正在使用的代码生成数组并保存它: if(isset($article['id'])){ $data[] = array( 'article_id' => $article['id'], 'name_of_social_site' => 'Google',
if(isset($article['id'])){
$data[] = array(
'article_id' => $article['id'],
'name_of_social_site' => 'Google',
'num_of_instances' => $google);
$data[] = array(
'article_id' => $article['id'],
'name_of_social_site' => 'Facebook',
'num_of_instances' => $facebook);
$data[] = array(
'article_id' => $article['id'],
'name_of_social_site' => 'Twitter',
'num_of_instances' => $twitter);
$data[] = array(
'article_id' => $article['id'],
'name_of_social_site' => 'Reddit',
'num_of_instances' => $reddit);
$data[] = array(
'article_id' => $article['id'],
'name_of_social_site' => 'Stumbleupon',
'num_of_instances' => $stumbleupon);
$data[] = array(
'article_id' => $article['id'],
'name_of_social_site' => 'Digg',
'num_of_instances' => $digg);
debug($data);
$this->saveAll($data);
我已经调试了阵列,以确保其结构正确。以下是调试结果:
Array
(
[0]=>阵列
(
[文章id]=>86503
[社交网站名称]=>谷歌
[num_of_实例]=>1
)
)
保存到数据库的唯一数据是数组元素5中的数据。我还尝试为数组中的每个元素使用一个save方法,但它仍然只保存最后一个元素。我在这里有点不知所措,任何帮助都将不胜感激。在保存时是否在数组上循环?它应该是
$this->Model->saveAll($data)代码>。试试看,跟我们一起回去。(显然,将“Model”替换为单数/大写模型)在循环的一次迭代中构建并保存数组。在保存或创建数据时,它不会循环。声明在模型中。我解决了问题。我最近修改了我的表和代码。我已经更改了表中的主键,但我没有删除代码public$primaryKey=article\u id,因此它不允许重复article\u id。不用说,我应该得到一个facepalm。不用担心-我们都做过类似的事情…你在保存时是否在数组上循环?它应该是$this->Model->saveAll($data)代码>。试试看,跟我们一起回去。(显然,将“Model”替换为单数/大写模型)在循环的一次迭代中构建并保存数组。在保存或创建数据时,它不会循环。声明在模型中。我解决了问题。我最近修改了我的表和代码。我已经更改了表中的主键,但我没有删除代码public$primaryKey=article\u id,因此它不允许重复article\u id。不用说,我应该得到一个掌心。不用担心,我们都做过这样的事情。。。
[1] => Array
(
[article_id] => 86503
[name_of_social_site] => Facebook
[num_of_instances] => 0
)
[2] => Array
(
[article_id] => 86503
[name_of_social_site] => Twitter
[num_of_instances] => 11
)
[3] => Array
(
[article_id] => 86503
[name_of_social_site] => Reddit
[num_of_instances] => 0
)
[4] => Array
(
[article_id] => 86503
[name_of_social_site] => Stumbleupon
[num_of_instances] => 0
)
[5] => Array
(
[article_id] => 86503
[name_of_social_site] => Digg
[num_of_instances] => 0
)