2010-12-09 60 views
2

我得到了一個名爲Posts的控制器,這個名爲Content的模型與其他模型(如Category和Location)正確鏈接。在cakephp中以編輯模式重新填充關係

在我的添加「內容」視圖中,我成功填充了多個選擇列表,其中包含要與帖子關聯的類別和位置。保存完美的作品。

現在在編輯/更新模式下,我可以再次填充多個選項的類別和位置,但不會選擇與當前文章相關的選擇。在數據庫中查看時,有些類別和位置已成功實現到當前帖子。
這是我在我的控制器有:

$this->data = $this->Content->read(); 
$this->set('locations',$this->Content->Location->find('list',array('fields' => array('id','location')))); 
$this->set('categories',$this->Content->Category->find('list',array('fields' => array('id','category')))); 

這是我在我的觀點了:

echo $this->Form->input('Location', array('type' => 'select','multiple' => 'true','options' => $locations)); 
echo $this->Form->input('Category', array('type' => 'select','multiple' => 'true','options' => $categories)); 

缺少什麼我在這裏?我如何獲得已經相關的位置和類別,請在多選列表中選擇?

(非關係數據的填充,將重新填充文本框等只是完美)

感謝您的幫助!

傑森

回答

0

,而不是

$this->data = $this->Content->read() 

嘗試

$params['conditions'] = array(
    'Content.id' => $id 
); 
$params['contain'] = array(
    'Category', 
    'Location' 
); 
$this->data = $this->Content->find('first', $params); 

您將需要Containable Behaviour

+0

工作,謝謝harpax! – Jason 2010-12-09 16:04:07

0

使用此:

echo $this->Form->input('Location', array( 
      'label' => 'Location', 
      'type' => 'select', 
      'options' => $LocationArray, 
      'selected'=> 12(Selected Value) 
      ); 
相關問題