CakePHP:如何从视图中查找相关的字段值

时间:2013-09-19 15:21:55

标签: cakephp

我对MVC有所了解,但我是CakePHP的新手。我认为我的问题很简单,但无论我尝试了多少种方法,我都无法检索我正在寻找的数据。

让我们从2个模型开始:用户和活动。 用户 - (有很多活动) -ID -name

活动(属于用户) -ID -user_id(与用户的id相关) -created_by(与用户ID相关)

现在,在我的“活动”视图中,user_id和created_by都显示了User Name ...这就是我想要的,但是在我的用户view.cpt中,bake在我的用户信息下面给出了一个列出相关活动的部分与我正在查看的用户,但在此列表中,只显示用户的ID。

以下是我的Users / view.cpt

中的烘焙代码
<?php
    $i = 0;
    foreach ($user['Activity'] as $activity): ?>
    <tr>
        <td><?php echo $activity['id']; ?></td>
        <td><?php echo $activity['title']; ?></td>
        <td><?php echo $activity['description']; ?></td>
        <td><?php echo $activity['user_id']; ?></td>
        <td><?php echo $activity['created_by']; ?></td> //I Want the User's NAME not iD
        <td><?php echo $activity['due_date']; ?></td>   //I Want the User's NAME not iD
        <td><?php echo $activity['created']; ?></td>
        <td><?php echo $activity['modified']; ?></td>
        <td><?php echo $activity['cfc_id']; ?></td>
        <td class="actions">
            <?php echo $this->Html->link(__('View'), array('controller' => 'activities', 'action' => 'view', $activity['id'])); ?>
            <?php echo $this->Html->link(__('Edit'), array('controller' => 'activities', 'action' => 'edit', $activity['id'])); ?>
            <?php echo $this->Form->postLink(__('Delete'), array('controller' => 'activities', 'action' => 'delete', $activity['id']), null, __('Are you sure you want to delete # %s?', $activity['id'])); ?>
        </td>
    </tr>
<?php endforeach; ?>

我知道我将不得不在整个地方做这件事(因为我的所有模型都将基于用户)什么是正确的方法来检索这些数据。我已经尝试将方法添加到模型和用户/活动控制器,但我似乎无法正确访问方法。

1 个答案:

答案 0 :(得分:0)

将此代码放入Activity.php模型类:

public $belongsTo = array('User' => array('className' => 'User',
                      'foreignKey' => 'user_id'),
              'Creator' => array('className' => 'User',
                         'foreignKey' => 'created_by'));

在控制器的类方法中:

$this->Activity->find('all', array('conditions' => array('Activity.user_id' => $user_id)));

在您的视图文件中:

<?php
    $i = 0;
    foreach ($user['Activity'] as $activity): ?>
    <tr>
        <td><?php echo $activity['id']; ?></td>
        <td><?php echo $activity['title']; ?></td>
        <td><?php echo $activity['description']; ?></td>
        <td><?php echo $activity['User']['username']; ?></td> //username is the name of user
        <td><?php echo $activity['Creator']['username']; ?></td> //username is the name of user
        <td><?php echo $activity['due_date']; ?></td>
        <td><?php echo $activity['created']; ?></td>
        <td><?php echo $activity['modified']; ?></td>
        <td><?php echo $activity['cfc_id']; ?></td>
        <td class="actions">
            <?php echo $this->Html->link(__('View'), array('controller' => 'activities', 'action' => 'view', $activity['id'])); ?>
            <?php echo $this->Html->link(__('Edit'), array('controller' => 'activities', 'action' => 'edit', $activity['id'])); ?>
            <?php echo $this->Form->postLink(__('Delete'), array('controller' => 'activities', 'action' => 'delete', $activity['id']), null, __('Are you sure you want to delete # %s?', $activity['id'])); ?>
        </td>
    </tr>
<?php endforeach; ?>