【问题标题】:cakephp instead of user_id how do I get for instance username in the viewcakephp 而不是 user_id 我如何在视图中获取例如用户名
【发布时间】:2015-11-07 17:45:35
【问题描述】:

我有 3 个表:users、cmets 和 videos。

在我的视频 view.ctp 中,我无法显示用户名,只有 user_id。

我想不通,谁能指出我正确的方向?

用户模型:

public $hasMany = array(
        'Comment' => array(
            'className' => 'Comment',
            'foreignKey' => 'user_id',
            'dependent' => false
        ),
}

视频模型:

public $hasMany = array(
        'Comment' => array(
            'className' => 'Comment',
            'foreignKey' => 'video_id',
            'dependent' => false
        ),
}

评论模型:

public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id'
        ),
        'Video' => array(
            'className' => 'Video',
            'foreignKey' => 'video_id'
        )
    );

}

视频 view.ctp

<?php if (!empty($video['Comment'])): ?>
      <?php foreach ($video['Comment'] as $comment): ?>
         <p><?php echo $comment['comment_created']; ?></p>
         <p><?php echo $comment['User']['user_username']; ?></p>
         <p><?php echo $comment['user_id']; ?></p>
         <p><?php echo $comment['comment_body']; ?></p>
      <?php endforeach; ?>
<?php endif; ?>

视频控制器

public function view($id = null) {
    $this->loadModel('Comment');
    if (!$this->Video->exists($id)) {
        throw new NotFoundException(__('Invalid video'));
    }
    $options = array('conditions' => array('Video.' . $this->Video->primaryKey => $id));
    $this->set('video', $this->Video->find('first', $options));

    $current_user = $this->Auth->user('user_id');

    if ($this->request->is('post')) {
        $this->Comment->create();
        /*save the current's user id in database*/
        $this->Comment->set('user_id', $current_user);
        /*save the current's video id in database*/
        $this->Comment->set('video_id', $id);
        if ($this->Comment->save($this->request->data)) {
            $this->Session->setFlash(__('You\'re comment has been placed.'));
            return $this->redirect(array('action' => 'view/' . $id));
        } else {
            $this->Session->setFlash(__('You\'re comment could not be placed. Please, try again.'));
        }
    }

    $this->set(compact('users', 'videos'));
}

【问题讨论】:

  • 为什么会有'conditions' => '', 'fields' => '','order' => '', 像空的东西
  • 删除空的东西解决了你的问题吗?
  • 不,很遗憾没有。
  • 我们可以在这里讨论吗:chat.stackoverflow.com/rooms/94517/…
  • 我更新了我的问题并将我的整个控制器代码放入其中。当把 在视图或控制器中,它给了我“null”或什么都没有..

标签: mysql cakephp controller


【解决方案1】:

解决方案:

在控制器中:

$comments = $this->Video->Comment->find('all', array('conditions' => array('Comment.video_id' => $id)));

在视图中:

<?php foreach ($comments as $comment): ?>
    <p><?php echo $comment['User']['user_username']; ?></p>
    <p><?php echo $comment['Comment']['comment_created']; ?></p>
    <p><?php echo $comment['Comment']['comment_body']; ?></p>
<?php endforeach; ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    相关资源
    最近更新 更多