3

I have a weird error when using contain to limit my data to only what I need. Here is my 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'),
        )
    ));

The problem is that Cakephp throws me two errors:

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]

I think the problem has to do with the following line (in contain: Review -> User):

'fields' => array('id', 'username'),

If I remove that everything is fine, but then again I dont want all the data from User, especially not the passwords so I have to limit that.

What is wrong with my Contain? The error suggest that I should be using an array somewhere, but I have no idea where.

Edit added query outcome

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] => 
                                )

                        )

                )

        )

)
Dakuipje
  • 91
  • 5
  • Not sure this is your problem but, I don't think you can use any conditions like order and limit. Those are supposed to be in your `find()` statement. Only use conditions and fields. – Tim Joyce Dec 08 '12 at 13:26
  • You actually can use order and limit in contain. It works and there is actually an example in the Cakebook that uses an order in a contain array: http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html So I doubt that is the problem, I am using read() instead of find(). But that should also not cause the problem.... – Dakuipje Dec 08 '12 at 13:32
  • Okay, I do see order is in there but I do not see limit, http://api20.cakephp.org/view_source/containable-behavior#line-302 lines 302 - 314 – Tim Joyce Dec 08 '12 at 13:34
  • Hmm I see, but deleting the limit doesnt solve my problem. Weird thing is the limit is actually working quite well in the contain...... – Dakuipje Dec 08 '12 at 13:44
  • Nothing else really sticks out. Have you tried using the dot syntax for those fields, `'fields' => array('User.id', 'User.username')` – Tim Joyce Dec 08 '12 at 13:49
  • Just did and it didnt solve the problem unfortunately. – Dakuipje Dec 08 '12 at 13:54
  • Can you try something for a sec. Remove the nested `Profile` array and just use `'User' => array('id', 'username')`? I'm thinking, if that works, you can do `'User' => array('id', 'username', 'Profile.id', 'Profile. image_thumb_small')` – Tim Joyce Dec 08 '12 at 14:00
  • That creates the exact same error. This also creates the same error: `'User' => array('fields' => array('id', 'username'))` – Dakuipje Dec 08 '12 at 14:06
  • No, without the `fields` index... just `'User' => array('id', 'username')`... Or maybe that's what you are saying you did – Tim Joyce Dec 08 '12 at 14:08
  • But the nested Profile array does its job just fine, weird cause it seems like the exact same piece of code...... – Dakuipje Dec 08 '12 at 14:09
  • Yeah I tried it without the `fields` index, same error – Dakuipje Dec 08 '12 at 14:09
  • Just delete your Profile table... It's 1 to 1 anyway... you don't need it... half kidding :D – Tim Joyce Dec 08 '12 at 14:11
  • Maybe do a `pr($this->Movie->find('first'));` and let us see the output of that.. Maybe there is something we are missing that we would see with that – Tim Joyce Dec 08 '12 at 14:14
  • 1
    Haha, with the ammount of profile options I have I really want to keep them separate .... – Dakuipje Dec 08 '12 at 14:16
  • I added the pr outcome in the question. Now that I look at that it seems like the Contain is actually working, but I still get the errors :S I deleted the passwords. – Dakuipje Dec 08 '12 at 14:23
  • Yes, it is working. However, It appears that Profile is not working. – Tim Joyce Dec 08 '12 at 14:33
  • Yeah i just noticed it. Actually did forget an array, guess ive been coding for too many straight hours. Thnx a lot :) – Dakuipje Dec 08 '12 at 14:34
  • No problem, you fixed it then? – Tim Joyce Dec 08 '12 at 14:34

1 Answers1

1

Solved it, I had an error in my contain. Stupid one.

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

Should have been:

'User' => array('Profile' => array('fields' => array('image_thumb_small'))),
Dakuipje
  • 91
  • 5