2014-09-02 37 views
-4

我有一个评论控制器。在这个评论控制器中,我有一个添加操作来为视频添加评论。添加评论时,我需要选择发布评论的用户,并且我需要选择获取评论的视频。看到这是我的后台。cakePHP:获取当前用户和当前视频

我有一个VideosController,我想在视图操作中执行的操作是加载CommentsController的添加操作。评论是由用户创建的,而视频有评论。同样在这个视图中,我有选择字段,我需要选择一个用户和视频。

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

评价模型:

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')); 
} 

的视频

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

回答

0

您可以随时使用view.ctp,

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

在控制器访问记录用户的ID。

虽然$ id是视频的ID,所以你的问题似乎解决了。

+0

任何人都可以解释投反对票背后的原因 – Abhishek 2014-09-02 18:31:31

+3

我没有降票(实际上我不能倒票),我确实为您提供了正确的解决方案 – Abhishek 2014-09-03 04:20:37