Getting Userdata from another controller

I am building a forum into an existing website with CakePHP 3, and I have controllers Users and Forumposts (among others).

I am making a page (/forumposts/user/<user_id>) that looks up all the forum posts where the user id of the post matches the user id in the url.

That works great since I save the ID as part of the forum post in the user, but I want the user lookup page to contain the username and some other user information from the usercontroller.

I have properly setup my modal assosiations, but I can’t figure out how to exactly display userdata in the forumpost controller for the user page. Here is my current code for the forumpost controller and the user page:

    public function user($id = null) {
        $this->loadComponent('Paginator');
        $posts = $this->Paginator->paginate($this->Forumposts->find()->where(['is_topic' => 1])->where(['user_id' => $id])->where(['is_approved' => 1])->where(['hidden' => 0]));
        $replies = $this->Paginator->paginate($this->Forumposts->find()->where(['is_topic' => 0])->where(['user_id' => $id])->where(['is_approved' => 1])->where(['hidden' => 0]));
        $this->set(compact('posts', 'replies'));   
    }

Just for context, replies are stored with the posts in the Forumposts table, just with is_topic set to 0 instead of one.

Any help would be appreciated. Thanks!

Assuming you’ve made your ForumPosts belongsTo Users association you can use contain in your queries.

I’ve take the liberty of tightening up your code for clarity.

    public function user($id = null) {
        $this->loadComponent('Paginator');
        $posts = $this->Paginator->paginate(
            $this->Forumposts->find()
                ->where([
                    'is_topic' => 1,
                    'user_id' => $id,
                    'is_approved' => 1,
                    'hidden' => 0
                ]))
                ->contain(['Users']);
        $replies = $this->Paginator->paginate(
            $this->Forumposts->find()
                ->where([
                    'is_topic' => 0,
                    'user_id' => $id,
                    'is_approved' => 1,
                    'hidden' => 0
                ]))
                ->contain(['Users']);
        $this->set(compact('posts', 'replies'));
    }

Also I wouldn’t allow $id = null in the method arguments since the method won’t work properly in that case.

public function user($id) {
    // body here
}
1 Like

The key conceptual takeaway here is that you’re not getting data “from another controller”. You’re getting data from another table. The other controller just happens to use that table as it’s default source.

2 Likes

Thanks, but I can’t figure out how to display the user data based on the ID in the url.

The code provided should give you everything you need in the view. What does your view code look like, where you’re trying to access the user data?

Your url is going to look something like

site.com/forum-posts/user/35

and cake standards will do the rest

Yes, but what do I need to put in the view to lookup and display the user’s data?

It’s just a tabbed list with those 2 paginators rendered.

I should have been more clear. Post the existing code from your view.

I do not have any existing code for accessing userdata. All I am doing is rendering the two paginators for the user’s posts and replies, but that isn’t grabbing any userdata, just filtering the user_id to match the id in the url. The page just renders 2 tabs, one for replies, one for posts.

I don’t understand how they relate to looking up a user’s name and profile picture based on a user ID, and I would rather not post the view code since it is unnecessary. If you can share why you need the code or what specific part you need, I can post that.

Once again, all I need to do is use a variable that I already have (user_id), and look up the user’s information based on it.

Here is a screenshot of what the page currently looks like. I want the spots in brackets to have userdata in them, based on the ID in the URL. The two paginators are working fine and nothing about them needs to be changed. I think you guys misunderstood my issue.

Thanks in advance

Okay, that’s clearer now. Presumably something like this in your controller:

$this->set('user', $this->Forumposts->Users->get($id));

And you won’t need to ->contain(['Users']) that @dreamingmind suggested in this case.

Thanks, I got it working based on that!