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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:02:09+00:00 2026-05-23T15:02:09+00:00

I create a simple blog with cakephp where i have some posts and some

  • 0

I create a simple blog with cakephp where i have some posts and some comments.
After i bake an application it create a comments folder with the index.ctp and a posts folder with the index.ctp

What i want to do is display them as fallow:

Post1
  comment1
  comment2
Post2
  comment1
  comment2

If i place the comments inside the posts/index.ctp i get an error telling me that the $comments is not defined.

How can i do this?
Thanks

edit:
alright, im sorry for the comeback, but it’s still a bit unclear. I do have the $hasMany relationship setup, in fact on the page that displays the posts i have a link that points me to the comments. The only thing that i want them to be displayed in the same page as the posts.

i should be able to say <?php echo $comment['Comment']['content']; ?>

  • 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-23T15:02:09+00:00Added an answer on May 23, 2026 at 3:02 pm

    Check your controllers.

    If you want to display Comment information in a Posts view, you need to be sure that the Posts controller can load the data.

    The “CakePHP” way is to define relationships between models. Check your baked models and see if there is something like this:

    class Post extends AppModel{
    
    var $hasMany = array( 'Comment' );
    
    }
    

    When the models are associated with each other, your Posts controller will automatically find Post objects and their associated Comment objects.

    For instance, this line:

    $this->Post->findById( $id );
    

    Will produce something like this:

    Array
    (
        [Post] => Array
        (
            [id] => 42
            [text] => Post1
        )
    
        [Comment] => Array
        (
            [0] => Array
            (
                [id] => 1
                [post_id] => 42
                [text] => Comment1
            )
    
            [1] => Array
            (
                [id] => 2
                [post_id] => 42
                [text] => Comment2
            )
        )
    )
    

    Good documentation at http://book.cakephp.org/

    • Model Associations
    • Retreiving Data

    EDIT: Adding more info after your comment

    As long as the models have the association, CakePHP will pull the data appropriately (unless you set Recursive => false or are using Containable which I assume you aren’t).

    Check your PostsController controller and see how it’s loading the data. I’m guessing it is doing something like the following:

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

    or

    $this->data = $this->Post->findById( $id );
    

    Check which way it is storing the retrieved data, and then access that variable from the view.

    For example, if it is storing the data in a variable named “$post”, you would put something like this in your view:

    // output the 'text' field of the 'post' object
    echo $post[ 'post' ][ 'text' ];  
    
    // loop through associated comments
    foreach ( $post[ 'comment' ] as $comment ){  
    
        //output the 'text' field of a 'comment' object
        echo $comment[ 'text' ];  
    
    }
    

    By default CakePHP stashes tons of detail in arrays after retrieving data. The trick is to know the hierarchy of the data and fetch it from the array accordingly.

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

Sidebar

Related Questions

No related questions found

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.