0

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

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

Activity(属于用户) -id -user_id(与用户id相关) -created_by(与用户id相关)

现在,在我的活动视图中,user_id 和 created_by 都显示为用户名......这是我想要的,但在我的用户视图中。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; ?>

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

4

1 回答 1

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; ?>
于 2013-09-20T09:25:57.373 回答