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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:39:09+00:00 2026-06-14T14:39:09+00:00

I have two model. PostModel & CommentModel. I defined two relation in these classes.

  • 0

I have two model. PostModel & CommentModel.
I defined two relation in these classes.

<?php
class PostModel extends AppModel {
    var $name = 'Post';
    var $hasMany = array(
        'Comment' => array(
            'className' => 'Comment',
            'foreignKey' => 'post_id'
        )
    );
}
 ?>

and

<?php 
class CommentModel extends AppModel {
var $name = 'Comment';
var $belongsTo = array(
    'Post' => array(
        'className' => 'Post',
        'foreignKey' => 'post_id'
    )
);
}
?>

and in database i have posts table and comments table with post_id column.
In the PostController class when i fetch record with this command :

$this->set('post',$this->Post->findById($id)); 

and debug $post array i see this array:

array(
'Post' => array(
    'id' => '4',
    'title' => 'روز اول دانشگاه',
    'body' => 'سلام. روز اول بود که اومده بودیم '
    'created' => '2012-11-11 21:49:48',
    'modified' => null
)
)

I expect that [comment] row be in this array but there isn’t.

I search net an debug my app but i can’t solve this issue.!!!!!

note : cakephp version is 2.2.3 stable

  • 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-14T14:39:10+00:00Added an answer on June 14, 2026 at 2:39 pm

    Your models are named incorrectly. PostModel should be Post with a posts table, CommentModel should be Comment with a comments table and a post_id in it. Furthermore, this isn’t working because you’ve told your Comments model to look for a ‘post_id’ on the posts table as a foreign key. This is not correct for a Posts hasMany Comments relationship, and since that column doesn’t exist it’s going to fail. If you follow the convention for Models, you don’t actually need to pass anything into the arrays. The ORM will figure it out.

    class Post extends AppModel {
        public $hasMany = array('Comment');
    }
    class Comment extends AppModel {
        public $belongsTo = array('Post');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes (MVC view model) which inherits from one abstract base class.
I have two PHP model classes named Category and Item. A Category may have
I have two Django model classes that are structured similar to the following: class
I have two simple Model classes and a ViewModel... public class GridItem { public
I have two model classes, say parent and child. Parents are created first, and
I have two questions. 1: At the moment I have two model classes that
I have two models: class Contact(models.Model): name = models.CharField(max_length=255) class Campaign(models.Model): contact = models.ForeignKey(Contact,
I have two models like this: class ClassA(models.Model): ida = models.AutoField(primary_key=True) classb = models.ForeignKey(ClassB)
I have two tables registries and names . Consequently, I have two model classes.
I have two model: class Questions(models.Model): question = models.TextField(verbose_name='Question') def get_answers(self): query = self.answers.select_related()

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.