在编辑模式下以cakephp重新填充表单中的关系

时间:2010-12-09 15:38:23

标签: cakephp

我有一个名为Posts的控制器,一个名为Content的模型,它与其他模型(如Category和Location)正确链接。

在我添加'内容'的视图中,我成功填充了多选列表,其中包含要选择与帖子相关的类别和位置。保存一切都很完美。

现在处于编辑/更新模式,我可以再次使用类别和位置填充多项选择,但不会选择与当前帖子相关的选项。查看数据库时,会有成功实现当前帖子的类别和位置 这是我在我的控制器中得到的:

$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));

我在这里缺少什么?如何获取已经相关的位置和类别,在多选列表中选择?

(填写非关系数据,将完全重新填充文本字段等)

感谢任何帮助!

杰森

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

使用此:

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