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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:36:15+00:00 2026-06-09T03:36:15+00:00

In my Users controller I’m trying to display a given users’s posts in their

  • 0

In my Users controller I’m trying to display a given users’s posts in their profile. How can I accomplish this? Basically, I’m in the Users controller trying to access the Posts table

These are my Tables

//Posts
id | body | user_id

//Users
user_id | username

This is my profile function of my users

  UsersController.php

  public function profile($id = null) {
    $this->User->id = $id;
    if (!$this->User->exists()) {  //if the user doesn't exist while on view.ctp, throw error message
        throw new NotFoundException(__('Invalid user'));
    }
    $conditions = array('Posts.user_id' => $id);
    $this->set('posts', $this->User->Posts->find('all',array('conditions' => $conditions))); 
}


/Posts/profile.ctp     

<table>
<?php foreach ($posts as $post): ?>

    <tr><td><?php echo $post['Post']['body'];?>
    <br>

    <!--display who created the post -->
    <?php echo $post['Post']['username']; ?>
    <?php echo $post['Post']['created']; ?></td>
</tr>
<?php endforeach; ?>
</table>

I’m getting several “undefined index errors” in reference to each line of profle.ctp

Undefined index: Post [APP/View/Users/profile.ctp, line 11]
  • 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-09T03:36:17+00:00Added an answer on June 9, 2026 at 3:36 am

    In your UsersController on your profile action, you can use the User model to access associated information.

    Example:

    class UsersController extends AppController {
        public function profile($id = null) {
            $this->User->recursive = 2;
            $this->set('user', $this->User->read(null, $id));
        }
    }
    

    In your User and Post model, you should have the correct associations setup:

    User model:

    class User extends AppModel {
        public $hasMany = array('Post');
    }
    

    Post model:

    class Post extends AppModel {
        public $belongsTo = array('User');
    }
    

    You will see now that in your view, on the $user variable, you have all the user data for the specified profile id, along with associated posts for that user.

    This page in the CakePHP documentation has some great helpful tips in reading your data from models.

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

Sidebar

Related Questions

Users can access others users profiles like this: site.com/username My find method in my
I have an and users can login and view their own profile with a
I have controller users . In this controller I have action account which is
I currently have this as a search: <%= form_tag users_path, :controller => 'users', :action
In my users controller I am trying to put in place the remember me
Suppose this is my users controller:- class UsersController < ApplicationController def show @user =
I have a Posts and a Users controller. I use the Auth Component and
i have table car_types ,a Controller users_controller and action url localhost/carsdirectory/users/dashboard in users_controller.php file
i have table car_types ,a Controller users_controller, model Car_type and action url localhost/carsdirectory/users/dashboard dashboard.ctp(view)
I have a system setup where users can post microposts (basically status updates) 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.