开发者

Refill relationships in form in cakephp in edit mode

I got a controller named Posts, a model called Content which is properly linked with other models such as Category and Location.

In my view for add 'Content' i successfully populate the multi select lists with categories and locations to pick to relate to the post. Saving it all works perfectly.

Now in edit/update mode, I can once again fill the multi selects with categories and locations, but it will not select the ones related to the current post. When looking in the database, there are categories and locations successfully realted to the current post.

This is what I got in my controller:

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

And this is what I got in my view:

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

What am i missing here? How do i get the already related locations and categories, select in the multi select lists?

(filling of non relationship data, will repopulate textfields etc just perfectly)

Grateful for any help!

Jason


instead of

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

try

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

You will need the Containable Behaviour for that


Use this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜