此时我可以在选定的组合框中显示人物的姓名,但我想将person_id存储在数据库中而不是人名。我怎样才能做到这一点。
控制器
公共职能指数() {
$this->Relation->save($this->request->data);
$this->loadModel('Profile');
$person= $this->Profile->find('all');
if($this->request->is('post')){
}
$temp = array();
$temp1 = array();
foreach($person as $person_name)
{
$temp[ $person_name['Profile']['id']] = $person_name['Profile']['id'];
$temp1[ $person_name['Profile']['first_name']] = $person_name['Profile']['first_name'];
}
$this->set('person_name', $temp);
$this->set('person_id', $temp1);
}
查看index.ctp
<?php echo $this->Form->input('person_id',array('multiple'=>true,'label'=>false,'type'=>'select','options'=>$person_name,'selected'=>$person_id)); ?>
答案 0 :(得分:1)
在您的视图中查看index.ctp
$data = array_combine($person_id,$person_name);
如果您将$ data打印为pr($ data);
Array
(
[54af7764-dcf8-4355-bcc7-3980c2f436a7] => sandeep
[54af77bb-fe28-4634-a3f9-3980c2f436a7] => prakash
)
所以你选择的组合框将是
<?php echo $this->Form->input('person_id',array('label'=>false,'type'=>'select','options'=>$data)); ?>