Cakephp 2.0在使用contain时出现奇怪的array_merge错误

3

我在使用contain来限制数据时遇到了奇怪的错误。以下是我的contain代码:

$this->Movie->contain(array(
        'Review' => array(
            'fields' => array('id', 'title', 'spoilers', 'body', 'created'),
            'User' => array(
                'fields' => array('id', 'username'),
                'Profile' => array('fields' => array('id', 'image_thumb_small'))
            ),
            'order' => 'Review.created DESC',
            'limit' => 2,
        ),
        'Movieupdates' => array(
            'User' => array('Profile' => array('fields' => 'image_thumb_small')),
            'order' => 'Movieupdates.created DESC',
            'limit' => 2
        ),
        'Actor' => array(
            'fields' => array('name', 'slug', 'image_thumb'),
        ),
        'Genre' => array(
            'fields' => array('name', 'id'),
        )
    ));

问题在于Cakephp给我抛出了两个错误:
Warning (2): array_merge() [function.array-merge]: Argument #1 is not an array [CORE\Cake\Model\Behavior\ContainableBehavior.php, line 400]

Warning (2): array_unique() expects parameter 1 to be array, null given [CORE\Cake\Model\Behavior\ContainableBehavior.php, line 400]

我认为问题与以下这行代码有关(在contain: Review -> User中):
'fields' => array('id', 'username'),

如果我删除它,一切都很好,但是我不想要用户的所有数据,特别是密码,所以我必须限制它。
我的Contain有什么问题?错误建议我应该在某个地方使用数组,但我不知道在哪里。 编辑添加查询结果
Array
(
    [Movie] => Array
        (
            [id] => 27
            [user_id] => 
            [title] => The Amazing Spider-Man
            [slug] => The_Amazing_Spider_Man_2012
            [year] => 2012
            [summary] => Peter Parker finds a clue that might help him understand why his parents disappeared when he was young. 
            [poster] => /files/movies/53cb8be658a2e04a21909e288cdcb8.jpg
            [poster_thumb] => /files/movies/53cb8be658a2e04a21909e288cdcb8_resized_101x150.jpg
            [backdrop] => /files/movies/backdrop/amazing_spider_man.jpg
            [rating] => 7.4285714285715
            [like_count] => 5
            [review_count] => 7
            [see_count] => 7
            [rating_count] => 14
        )

    [Movieupdates] => Array
        (
            [0] => Array
                (
                    [id] => 20
                    [user_id] => 4
                    [movie_id] => 27
                    [type] => 4
                    [info] => 26
                    [created] => 2012-12-08 11:06:18
                    [User] => Array
                        (
                            [id] => 4
                            [username] => Ceriksen
                            [email] => skdji@gmail.com
                            [password] => ---
                            [validation_code] => 082b7735cfb3a3d5c74547d702bd97f9af517870
                            [group_id] => 3
                            [created] => 2012-11-27 11:23:57
                            [modified] => 2012-11-27 11:23:57
                            [Profile] => Array
                                (
                                    [id] => 5
                                    [user_id] => 4
                                    [image] => /files/profiles/771a98602dc7849b2d004952f1f35f.jpg
                                    [image_thumb] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_130x130-2.jpg
                                    [image_thumb_medium] => 
                                    [image_thumb_small] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_30x30.jpg
                                    [name] => 
                                    [last_name] => 
                                    [bio] => 
                                    [country] => 
                                    [city] => 
                                )

                        )

                )

            [1] => Array
                (
                    [id] => 19
                    [user_id] => 4
                    [movie_id] => 27
                    [type] => 3
                    [info] => 10
                    [created] => 2012-12-08 11:06:08
                    [User] => Array
                        (
                            [id] => 4
                            [username] => Ceriksen
                            [email] => skdji@gmail.com
                            [password] => ---
                            [validation_code] => 082b7735cfb3a3d5c74547d702bd97f9af517870
                            [group_id] => 3
                            [created] => 2012-11-27 11:23:57
                            [modified] => 2012-11-27 11:23:57
                            [Profile] => Array
                                (
                                    [id] => 5
                                    [user_id] => 4
                                    [image] => /files/profiles/771a98602dc7849b2d004952f1f35f.jpg
                                    [image_thumb] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_130x130-2.jpg
                                    [image_thumb_medium] => 
                                    [image_thumb_small] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_30x30.jpg
                                    [name] => 
                                    [last_name] => 
                                    [bio] => 
                                    [country] => 
                                    [city] => 
                                )

                        )

                )

        )

    [Genre] => Array
        (
            [0] => Array
                (
                    [name] => Action
                    [id] => 11
                    [MoviesGenre] => Array
                        (
                            [id] => 56
                            [movie_id] => 27
                            [genre_id] => 11
                        )

                )

            [1] => Array
                (
                    [name] => Adventure
                    [id] => 12
                    [MoviesGenre] => Array
                        (
                            [id] => 57
                            [movie_id] => 27
                            [genre_id] => 12
                        )

                )

            [2] => Array
                (
                    [name] => Fantasy
                    [id] => 18
                    [MoviesGenre] => Array
                        (
                            [id] => 58
                            [movie_id] => 27
                            [genre_id] => 18
                        )

                )

        )

    [Actor] => Array
        (
            [0] => Array
                (
                    [name] => Emma
                    [slug] => Emma_Stone
                    [image_thumb] => /files/actors/d59efc7614151acb7ea5d08da47112_resized_30x30.jpg
                    [MoviesActor] => Array
                        (
                            [id] => 1
                            [movie_id] => 27
                            [actor_id] => 8
                        )

                )

            [1] => Array
                (
                    [name] => Andrew
                    [slug] => Andrew_Garfield
                    [image_thumb] => /files/actors/0a9354d741328f4cf3e2954641e4eb_resized_25x30.jpg
                    [MoviesActor] => Array
                        (
                            [id] => 2
                            [movie_id] => 27
                            [actor_id] => 9
                        )

                )

            [2] => Array
                (
                    [name] => Martin
                    [slug] => Martin_Sheen
                    [image_thumb] => /files/actors/2383fc87054b2e8b4249fc7e3ffba5_resized_30x27.jpg
                    [MoviesActor] => Array
                        (
                            [id] => 3
                            [movie_id] => 27
                            [actor_id] => 10
                        )

                )

        )

    [Review] => Array
        (
            [0] => Array
                (
                    [id] => 26
                    [title] => W00t
                    [spoilers] => 0
                    [created] => 2012-12-08 11:06:18
                    [user_id] => 4
                    [MoviesReview] => Array
                        (
                            [id] => 25
                            [movie_id] => 27
                            [review_id] => 26
                        )

                    [User] => Array
                        (
                            [id] => 4
                            [username] => Ceriksen
                            [Profile] => Array
                                (
                                    [id] => 5
                                    [user_id] => 4
                                    [image] => /files/profiles/771a98602dc7849b2d004952f1f35f.jpg
                                    [image_thumb] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_130x130-2.jpg
                                    [image_thumb_medium] => 
                                    [image_thumb_small] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_30x30.jpg
                                    [name] => 
                                    [last_name] => 
                                    [bio] => 
                                    [country] => 
                                    [city] => 
                                )

                        )

                )

            [1] => Array
                (
                    [id] => 25
                    [title] => Testing the update
                    [spoilers] => 1
                    [created] => 2012-12-08 11:04:44
                    [user_id] => 4
                    [MoviesReview] => Array
                        (
                            [id] => 24
                            [movie_id] => 27
                            [review_id] => 25
                        )

                    [User] => Array
                        (
                            [id] => 4
                            [username] => Ceriksen
                            [Profile] => Array
                                (
                                    [id] => 5
                                    [user_id] => 4
                                    [image] => /files/profiles/771a98602dc7849b2d004952f1f35f.jpg
                                    [image_thumb] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_130x130-2.jpg
                                    [image_thumb_medium] => 
                                    [image_thumb_small] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_30x30.jpg
                                    [name] => 
                                    [last_name] => 
                                    [bio] => 
                                    [country] => 
                                    [city] => 
                                )

                        )

                )

        )

)

好的,我确实看到了order,但是我没有看到limit,http://api20.cakephp.org/view_source/containable-behavior#line-302行302-314。 - Tim Joyce
没有什么特别需要注意的。你尝试过使用点语法来访问这些字段吗?例如:'fields' => array('User.id', 'User.username') - Tim Joyce
刚刚尝试了一下,可惜问题没有解决。 - Dakuipje
但是嵌套的Profile数组工作得很好,很奇怪,因为它看起来就像是完全相同的代码...... - Dakuipje
1
哈哈,由于我有很多个配置选项,我真的想要将它们分开保留... - Dakuipje
显示剩余13条评论
1个回答

1
解决了,我的内容有错误。很愚蠢的错误。
'User' => array('Profile' => array('fields' => 'image_thumb_small')),

应该是:

'User' => array('Profile' => array('fields' => array('image_thumb_small'))),

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接