CakePHP saveAll()方法只保存传递数组的最后一个元素

时间:2012-02-02 13:47:18

标签: php mysql cakephp

我正在尝试在CakePHP中执行saveAll但由于某种原因,只保存了数组的最后一个元素。这是我正在使用生成数组并保存它的代码:

        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] =>排列         (             [article_id] => 86503             [name_of_social_site] =>谷歌             [num_of_instances] => 1         )

[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
    )

保存到数据库的唯一数据是数组元素5中的数据。我还试图为数组中的每个元素使用一个save方法,但它仍然只保存最后一个。我在这里有点不知所措,任何帮助都会非常感激。

0 个答案:

没有答案