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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:59:16+00:00 2026-05-30T15:59:16+00:00

In my application I have two tables (I have more but I’m only discussing

  • 0

In my application I have two tables (I have more but I’m only discussing two here):

**Posts**
id
title
datetime
content

**Comments**
id
content
post_id
user_id

As you can see their is a relationship using the post_id as a foreign key.

When viewing the Post I have the following code to display comments:

<?php if ( ! empty($post['Comment']) ): ?>
        <ul>
            <?php foreach ($post['Comment'] as $comment): ?>
            <li>
                <?php echo $comment['title']; ?>
            </li>
            <?php endforeach; ?>
        </ul>
        <?php else: ?>
        <p>No comments...</p>
        <?php endif; ?>

Here is the controller method for viewing a post:

function view ( $id = null, $slug = null )
    {
        $post = $this->Post->find('first',array('contain'=>array('User'=>array('Comment','Profile')),'conditions'=>array('Post.id'=>Tiny::reverseTiny($id))));

        if (!$post)
        {
            throw new NotFoundException('404');
        }
        else if($post['Post']['status'] == '0') // 0=draft 1=open 2=open
        {
            if($post['Post']['user_id'] == $this->Auth->user('id'))
            {
                $this->Session->setFlash('Your post has NOT been published yet');
            }
            else
            {
                throw new NotFoundException('404');
            }
        }

        if (Inflector::slug($post['Post']['title']) != $slug || $slug = null)
        {
            $this->redirect(array('id'=>Tiny::toTiny($post['Post']['id']),'slug'=>Inflector::slug($post['Post']['title'])));
        }

        $this->set(compact('post'));
    }

and this is the comment model:

class Comment extends AppModel
{
    public $name = 'Comment';

    public $belongsTo = array('User','Post');

    public $actsAs = array('Containable');
}

and a typical url for a post would be: /posts/Sample_post-1ns

However I ALWAYS get the message that there are no comments… But I have looked in the database and their is… and I have double checked that all the associations are correct. So I can only presume that the code above is wrong!

Also is their a better way of calling the comments? As I want to add the ability to filter and paginate them too.

Cheers

  • 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-30T15:59:18+00:00Added an answer on May 30, 2026 at 3:59 pm

    From your schema it looks like the comment model is related to the post model, not to the user, so:

    $post = $this->Post->find('first',array('contain'=>array('User'=>array('Comment','Profile')),'conditions'=>array('Post.id'=>Tiny::reverseTiny($id))));
    

    Should be:

    $post = $this->Post->find('first',array('contain'=>array('Comment','User'=>array('Profile')),'conditions'=>array('Post.id'=>Tiny::reverseTiny($id))));
    

    I.e. the Comment should be outside of the User contain array.

    Providing that the correct hasMany and belongsTo relationships are set up on the Post/User and Comment model respectively, the first example would produce an array like this:

     array(
        'Post'=>array(...),
        'User'=>array(
            'id'=>1,
             //Other user data
    
             'Comment'=>array(
                  array(...), //Comment 1
                  array(...), //Comment 2
              )
         )
     );
    

    And the second example would produce:

    array(
        'Post'=>array(...),
        'User'=>array('id'=>1 //Other user data)
        'Comment'=>array(
              array(...), //Comment 1
              array(...), //Comment 2
         )
     );
    

    Hopefully, that shows that the difference is in the position of the “Comment” key in the array – it is either under the User key, or at the top level depending on how you do the contain.

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

Sidebar

Related Questions

I have two tables in my application. The first one, Sections, stores content for
We have two tables in our application that both have a ShowOrder column. We
I have two tables, a user table and a application table: User id username
I am trying to develop an application using ADO.NET. I have two tables in
In our application, we have two sets of tables: One set of working tables
Here's my scenario: I have two tables A, B which (for the sake of
I have two data tables which have a primary key column in common but
I have two tables applications and leads application table submitDate | fName | lName
I have two innodb tables: articles id | title | sum_votes ------------------------------ 1 |
In my application I have two tables: Users and Profiles. A user has a

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.