Mirko Pagliai
Mirko Pagliai

Reputation: 1241

Cakephp: associations of associations in "fields"

a post has a user (belongsTo) and a user has a profile (hasOne). Normally everything works: I can access from Post to User and from User to Profile.

From Post, all works using a generic Find. The result is more or less this (I deleted some keys, just for example):

array(
    'Post' => array(
        'id' => '1',
        'category_id' => '1',
        'user_id' => '1',
        'title' => 'Example',
        'text' => '',
    ),
    'User' => array(
        'password' => '*****',
        'id' => '1',
        'group_id' => '1',
        'username' => 'mirko',
        'status' => 'active',
        'Profile' => array(
            'id' => '1',
            'user_id' => '1',
            'first_name' => 'Mirko',
            'last_name' => 'Pagliai',
        ),
    )
)

The problem comes when I use "fields", when I want to extract only a few fields. For example, this works:

'fields'        => array('id', 'title', 'User.id')

and the result is:

array(
    'Post' => array(
        'id' => '1',
        'title' => 'Articolo di prova :-)'
    ),
    'User' => array(
        'id' => '1'
    )
)

But I don't understand, always using "fields", how to access Profile. These don'yt work:

'fields'        => array('id', 'title', 'User.id', 'Profile.id')

'fields'        => array('id', 'title', 'User.id', 'User.Profile.id')

I always get this error "Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'User.Profile.id' in 'field list'".

What's wrong? Thanks.

EDIT: I'm not using the Containable behavior. Maybe is this the problem? Is it essential in this case?

An example, using paginate:

$this->paginate = array(
    'conditions'    => $conditions,
    'fields'        => array('id', 'title', 'User.id', 'Profile.id'),
    'limit'         => 10,
    'recursive'     => 2,
);

EDIT2: thanks to @Hoff, I am close to the solution. But there is still something wrong (and Containable is very useful!). In AppModel.php I added:

...
class AppModel extends Model {
    public $actsAs = array('Containable');
    public $recursive = -1;
...

But this doesn't work:

$this->paginate = array(
            'conditions'    => $conditions,
            'contain'       => array(
                'User' => array(
                    'fields' => array('id'),
                    'Profile' => array(
                        'fields' => array('id', 'full_name'),
                    ),
                ),
                'Category' => array(
                    'fields' => 'title',
                ),
            ),
            'fields'        => array('id', 'user_id', 'category_id', 'title', 'created', 'modified', 'published'),
            'limit'         => 10,
        );

Cause I get:

array(
    (int) 0 => array(
        'Post' => array(
            'id' => '1',
            'user_id' => '1',
            'category_id' => '1',
            'title' => 'Articolo di prova :-)',
            'created' => '2012-06-21 18:46:00',
            'modified' => '0000-00-00 00:00:00',
            'published' => true
        ),
        'Category' => array(
            'title' => 'prova',
            'id' => '1'
        ),
        'User' => array(
            'id' => '1'
        )
    )
)

but if I add any field to User (email, username, password, etc.), this works:

            'User' => array(
                'fields' => array('id', 'username'),
                'Profile' => array(
                    'fields' => array('id', 'full_name'),
                ),
            ),

And I get:

array(
    (int) 0 => array(
        'Post' => array(
            'id' => '1',
            'user_id' => '1',
            'category_id' => '1',
            'title' => 'Articolo di prova :-)',
            'created' => '2012-06-21 18:46:00',
            'modified' => '0000-00-00 00:00:00',
            'published' => true
        ),
        'Category' => array(
            'title' => 'prova',
            'id' => '1'
        ),
        'User' => array(
            'id' => '1',
            'username' => 'mirko',
            'Profile' => array(
                'id' => '1',
                'full_name' => 'Mirko Pagliai'
            )
        )
    )
)

Upvotes: 0

Views: 1593

Answers (1)

Hoff
Hoff

Reputation: 1772

Instead of using the 'recursive' key, I highly suggest using the containable behavior. The documentation on the behavior can be found here. I would suggest applying the behavior in your AppModel so you don't need to add it to all your models. In Model/AppModel.php:

App::uses('Model', 'Model');
class AppModel extends Model {
    public $actsAs = array('Containable');
    public $recursive = -1;
}

Then, for your pagination:

$this->paginate = array(
    'conditions'    => $conditions,
    'fields'        => array('id', 'title'),
    'limit'         => 10,
    'contain' => array(
        'User' => array(
            'fields' => array('id'),
            'Profile' => array(
                'fields' => array('id')
            )
        )
    )
);

Upvotes: 1

Related Questions