cakePHP:获取当前用户和当前视频

时间:2014-09-02 18:15:49

标签: php cakephp

我有一个评论控制器。在这个CommentsController中,我有一个添加操作,用于为视频添加注释。添加评论时,我需要选择放置评论的用户,然后我需要选择获得评论的视频。将此视为我的后台。

我有一个VideosController,我想在view动作中做的是加载CommentsController的add动作。评论由用户创建,视频具有评论。此外,在此视图中,我有选择字段,我需要选择用户和视频。

现在我想要检索当前登录的用户和当前视频。

评论模型:

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

视频模型:

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

添加动作CommentsController:

public function add() {
    if ($this->request->is('post')) {
        $this->Comment->create();
        if ($this->Comment->save($this->request->data)) {
            $this->Session->setFlash(__('The comment has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The comment could not be saved. Please, try again.'));
        }
    }
    $users = $this->Comment->User->find('list');
    $videos = $this->Comment->Video->find('list');
    $this->set(compact('users', 'videos'));
}

查看VideosController的动作:

public function view($id = null) {
    //Loading comment model
    $this->loadModel('Comment');

    //Using frontoffice layout
    $this->layout = 'default_front';

    //Getting the view of the video
    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));

    //Getting the timeline off the viewed video
    $tab = array('Video.Timeline' . $this->Video->Timeline->primaryKey => $id);
    //Getting the items of the Timeline
    $items = $this->Video->Timeline->Item->find('all', array('conditions' => array('Item.timeline_id' => $tab),'recursive'=>2));
    $this->set('items', $items);

    //Create comments
    if ($this->request->is('post')) {
        $this->Comment->create();
        if ($this->Comment->save($this->request->data)) {
            $this->Session->setFlash(__('The comment has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The comment could not be saved. Please, try again.'));
        }
    }
    //Here I want the currently logged in user
    $users = $this->Comment->User->find('list');
    //Here I want the current video
    $videos = $this->Comment->Video->find('list');
    $this->set(compact('users', 'videos'));
}

view.ctp of videos

<fieldset>
    <?php
        echo $this->Form->input('comment_body', array(
            'label' => '',
            'placeholder' => 'Share your thoughts',
        ));
        echo $this->Form->input('comment_created', array(
            'label' => 'Created',
            //'type' => 'hidden'
        ));
        echo $this->Form->input('user_id', array(
            //'type' => 'hidden'
        ));
        echo $this->Form->input('video_id', array(
            //'type' => 'hidden'
        ));
    ?>
</fieldset>

1 个答案:

答案 0 :(得分:0)

你可以随时使用,

$this->Auth->user('id'); 

在控制器中访问已登录用户的ID。

虽然$ id是视频的ID,但您的问题似乎已经解决了。