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 7692167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:47:26+00:00 2026-05-31T20:47:26+00:00

I have 3 users tables on my DB: Users users_twitter users_web Users is the

  • 0

I have 3 users tables on my DB:

  • Users
  • users_twitter
  • users_web

Users is the parent of both users_twitter and users_web.

And then another table for comments. Both kind of users can comment.

So, when I want to show the comments I need to get information of the author of each comment.
In order to do this I am using the variable $belongsTo on the /Model/Comment.php like this:

public $belongsTo = array(
    'users' => array(
        'foreignKey' => 'idUser'
    )
);

In Controller/CommentController, when i do this:

public function index($idPost) {
    $this->Comment->recursive = 0;
    return $this->Comment->find('all');
}

I get this kind of array:

[0] => Array
        (
            [Comment] => Array
                (
                    [id] => 1
                    [idPost] => 441
                    [idUser] => 387371023
                    [comment] => hello word
                    [created] => 2012-03-01 00:00:00
                )

            [User] => Array
                (
                    [id] => 1
                    [username] => myname
                )
        )

And what I want is to get more info yet, the one of the subtype of Users, depending on the existence of the ID on the subtype tables.

In case it was an id of a user_web table, I would like to get this:

[0] => Array
        (
            [Comment] => Array
                (
                    [id] => 1
                    [idPost] => 441
                    [idUser] => 387371023
                    [comment] => hello word
                    [created] => 2012-03-01 00:00:00
                )

            [User] => Array
                (
                    [id] => 1
                    [username] => myname
                    [users_web] =>  Array
                        (
                            [id] => 1
                            [username] => myName
                            [password] => XXXXX
                            [verified] => 1
                            [created] => 1
                        )
        )

Do you know if that’s possible with CakePHP?
As far as I know, with $hasOne I only get one level more, I need two.

  • 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-05-31T20:47:27+00:00Added an answer on May 31, 2026 at 8:47 pm

    In your AppModel add

    public $actsAs = array('Containable');
    

    In your index:

    public function index($idPost) {
        return $this->Comment->find('all', array(
            'contain' => array('User.UsersWeb')));
    }
    

    Your User model has to be associated with a UsersWeb model to make this work.

    See http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html#containing-deeper-associations for a complete description.

    Also why do you return the data in the controller index? Are you using requestAction for this? And why don’t you use pagination? Do you really want to display hundreds of comments on one page?

    See http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables: Users: ID, first_name, last_name Networks: user_id, friend_id, status I want
hope someone can help. I have two tables: Users -UserID -UserName UsersType -UserTypeID -UserID
I have two tables in my MySQL database, users and tweets, as follows: TABLE
I have 3 tables: users (id, name) currency (id, name) accounts (id, user_id, currency_id,
I have 3 tables users(id,name),groups(id,name) and users_groups(user_id,group_id). users and groups have many to many
I have 3 tables: users: Id Login 1 John 2 Bill 3 Jim computers:
I have four tables: Users PrivilegeGroups rdPrivileges LinkPrivilege ----------- ---------------- --------------- --------------- userId(pk) privilegeGroupId(pk)
I have 2 tables: users and subscribers . The relationship of the user to
I have two tables- users and language with a foreign key link of their
In my database I have three tables: Users: UserID (Auto Numbering), UserName, UserPassword and

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.