Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8139739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:55:46+00:00 2026-06-06T11:55:46+00:00

a post has a user (belongsTo) and a user has a profile (hasOne). Normally

  • 0

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'
            )
        )
    )
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:55:48+00:00Added an answer on June 6, 2026 at 11:55 am

    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')
                )
            )
        )
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now, I have three models Post, Comment and User (using Devise ) associated
My app has a user model and a post model, where user has_many posts
Let's say post has_many comments and comment belongs_to user I want to get all
I have users, posts and comments. User can post only one comment to each
Imagine these models: class User belongs_to :profile # has email here end class Profile
Post :belongs_to :user User :has_many :posts In my signup workflow they draft a Post
A Post belongs_to a User, and a User has_many Posts. A Post also belongs_to
I have a User model, a Post model, and an Interest model. User has_many
My table 'Post' has field: id, name, content, post_id When i create new post,
I have a wordpress site with 5k post and each post has average 25

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.