我有兩個表,questions
和tags
,它們有一個HABTM關係。添加問題時,我希望能夠爲問題指定一個標籤(這只是第一個標籤,稍後可以添加更多標籤)。標籤從桌子上拉出來。如何配置我的應用程序,以便在添加問題並指定標記時,連接將反映在連接表(questions_tags
)中?CakePHP HABTM表單提交
這裏是我的問題添加動作代碼:
function add() {
$tags = $this->Question->Tag->find('all');
$this->set('tags',$tags);
if (!empty($this->data)) {
$this->Question->create();
if ($this->Question->save($this->data)) {
$this->Session->setFlash(__('The question has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The question could not be saved. Please, try again.', true));
}
}
$users = $this->Question->User->find('list');
$tags = $this->Question->Tag->find('list');
$this->set(compact('users', 'tags'));
}
,這裏是我的問題添加視圖代碼:
<?php
echo $this->Form->create('Question');
echo $this->Form->input('user_id',array('type' => 'hidden', 'value' => $this->Session->read('Auth.User.id')));
echo $this->Form->input('title');
echo $this->Form->input('details',array('type' => 'textarea'));
echo $this->Form->input('tag_id');
echo $this->Form->end(__('Submit', true));
?>
您的代碼看起來像是設置了一個問題屬於標記設置。看看這個食譜條目:http://book.cakephp.org/view/1034/Saving-Related-Model-Data-HABTM – JohnP 2011-04-18 07:28:57