2

Have User and UserProfile models. User model has hasOne association with UserProfile. UserProfile mysql table is empty. When I do $this->User->find('all', array('contain' => array('UserProfile'))) instead of an empty UserProfile array, as you would expect, I get an array populated with empty fields that correspond to schema:

Array
(
    [0] => Array
        (
            [User] => Array
                (
                    [id] => 1
                    [firstname] => Joe
                    [surname] => Bloggs
                    [email] => [email protected]
                    [password] => $2a$10$re4r7AXWQcXgkKcabUqmtO6j.7p2bA1t2SLeG93eVsiDPBgGaeXRS
                    [enabled] => 1
                    [user_group_id] => 1
                    [created] => 2014-06-26 15:01:38
                    [modified] => 2014-06-26 15:01:38
                )

            [UserProfile] => Array
                (
                    [id] => 
                    [job_title] => 
                    [user_id] => 
                    [enabled] => 
                    [created] => 
                    [modified] => 
                )

        )

Anyone seen this and know how to fix it?!

1 Answer 1

4

This is correct behavior because the hasOne association is not optional. If you want to make it optional, then change it to a hasMany and only ever create one record.

Cake is performing a LEFT JOIN from User to UserProfile and the query result produced NULL values for the missing record. This is the same result you would get if you executed the SQL in an editor outside of Cake.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.