-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>
任何人都可以解釋投反對票背後的原因 – Abhishek 2014-09-02 18:31:31
我沒有降票(實際上我不能倒票),我確實爲您提供了正確的解決方案 – Abhishek 2014-09-03 04:20:37