开发者

Complicated Query in CakePHP

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&g开发者_如何学运维t;
            <?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!


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']]


You probably forgot to set de displayField attribute on your User Model.

class User extends AppModel { var $displayField = 'username';}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜