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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:31:32+00:00 2026-05-23T16:31:32+00:00

I was wondering if you guys could help with a complicated query. I have

  • 0

I was wondering if you guys could help with a complicated query.
I have three tables of note.

Users, Jobs and Comments.

Jobs are assigned to users, but any user can leave a comment on a job.
At the moment, after baking the traditional way, the View Job view shows related comments at the bottom. However, the Comments table shows a numeric user_id whereas I want it to display the ‘username’ that is matched with said user_id.

In the View Job view, I have this array, with their respective keys:

$job;
$job['Users'];
$job['Comments'];
$job['Job'];

And here is my debug output of the $job Array.

Array
(
    [Job] => Array
        (
            [id] => 1
            [user_id] => 1
            [title] => New Job
            [body] => adsljbfalwsjbflkjb
            [deadline] => 2011-07-15
            [completed] => 0
        )

    [User] => Array
        (
            [id] => 1
            [username] => dan
            [password] => 2fd27a6319ef3faf9ed55b59830d786b5ed890be
        )

    [Comment] => Array
        (
            [0] => Array
                (
                    [id] => 3
                    [job_id] => 1
                    [user_id] => 2
                    [hours] => 6
                    [body] => Comment from Phil
                    [date] => 2011-07-12
                )

            [1] => Array
                (
                    [id] => 2
                    [job_id] => 1
                    [user_id] => 1
                    [hours] => 2
                    [body] => This is a Comment from the 9th
                    [date] => 2011-07-09
                )

        )

)

Could I do something with these that would allow me to display the username and not the user_id? Do I modify the Model? The Controller?
I’m rather new to CakePHP and although I’ve grasped the basics, I’m struggling with the more complicated queries like this.

Here is my view file that I want to change:

<div class="related">
    <h3><?php __('Comments'); ?></h3>
    <?php if (!empty($job['Comment'])): ?>
        <table cellpadding = "0" cellspacing = "0">
            <tr>
                <th><?php __('User'); ?></th>
                <th><?php __('Date'); ?></th>
                <th><?php __('Hours'); ?></th>
                <th><?php __('Body'); ?></th>
                <th class="actions"><?php __('Actions'); ?></th>
            </tr>
            <?php
            $i = 0;
            foreach ($job['Comment'] as $comment):
                $class = null;
                if ($i++ % 2 == 0) {
                    $class = ' class="altrow"';
                }
                ?>
                <tr<?php echo $class; ?>>
                    <td><?php echo $comment['user_id']; ?></td> // NEEDS TO BE USERNAME NOT USER ID
                    <td><?php echo $comment['date']; ?></td>
                    <td><?php echo $comment['hours']; ?></td>
                    <td><?php echo $comment['body']; ?></td>                   
                    <td class="actions">
                        <?php echo $this->Html->link(__('View', true), array('controller' => 'comments', 'action' => 'view', $comment['id'])); ?>
                        <?php echo $this->Html->link(__('Edit', true), array('controller' => 'comments', 'action' => 'edit', $comment['id'])); ?>
                        <?php echo $this->Html->link(__('Delete', true), array('controller' => 'comments', 'action' => 'delete', $comment['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $comment['id'])); ?>
                    </td>
                </tr>
            <?php endforeach; ?>+
        </table>
    <?php endif; ?>

</div>

Thanks for your help!

  • 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-23T16:31:33+00:00Added an answer on May 23, 2026 at 4:31 pm

    I resolved the same issue by running a find(‘list’) on the model you would like to retrieve the names from.

    In your case you would like to find Users->find(‘list’) and assign it to a variable in your controller and then in your view where the id displays you add that new variable over the id.

    e.g. change $job[‘Comment’][‘user_id’] to $users[$job[‘Comment’][‘user_id’]]

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

Sidebar

Related Questions

Hi Guys just wondering if you could help, I have a page in C#
Now I am wondering if you guys could help me on serializing an ArrayList
Hey guys I was wondering if someone could provide a little help. I've been
Wondering if someone could help me. I have next to no knowledge with Ajax,
Hi guys I was wondering if anyone could help me out, I need to
Hey guys I was wondering if anyone could help me with an issue im
Hi guys just wondering if somebody could help me try and correctly thread my
Hi guys I was wondering if anyone could help me with the following: I
Hi guys I was wondering if you could help me if the folliwng: I
I was wondering if you guys could help me. Here are my .h: Class

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.