Lead.php(型号)
public $hasMany = array(
'LeadEmailDetail' => array(
'className' => 'LeadEmailDetail',
'foreignKey' => 'lead_id',
);
LeadEmailDetail.php(型号)
public $belongsTo = array(
'Lead' => array(
'className' => 'Lead',
'foreignKey' => 'lead_id'
),
'Country' => array(
'className' => 'Country',
'foreignKey' => 'county_id'
)
);
Country.php(模型)
public $hasMany = array(
'LeadEmailDetail' => array(
'className' => 'LeadEmailDetail',
'foreignKey' => 'country_id',
)
);
LeadsController.php(Controller.php这样)
public function edit($id = null)
{
if ($this->request->is(array('post', 'put'))) {
$this->Lead->id = $id;
if ($this->Lead->saveAll($this->request->data)) {
$this->LeadEmailDetail->saveAll($this->request->data);
$this->Session->setFlash(__('Your lead has been updated.'), 'flash/error');
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to update your lead.'), 'flash/error');
}
$lead = $this->Lead->find('first', array(
'conditions' => array('id' => $id),
'recursive' => 2
));
$Country = $this->Country->find('list');
$this->set(compact('lead','Country'));
}
信息\视图\信息\ edit.ctp
pr($lead);
Array
(
[Lead] => Array
(
[id] => 1
[website] => http://localhost/phpmyadmin
)
[LeadEmailDetail] => Array
(
[0] => Array
(
[id] => 7
[lead_id] => 1
[county_id] => 101
[name] => tyagi
[email] => main@gmail.com
[Country] => Array
(
[id] => 101
[name] => INDIA
)
)
[1] => Array
(
[id] => 8
[lead_id] => 1
[county_id] => 21
[name] => Vikass
[email] => vikastyagi7@gmail.com
[Country] => Array
(
[id] => 21
[nicename] => Andorra
)
)
)
)
<?php foreach($lead['LeadEmailDetail'] as $key=>$value) : ?>
<?php echo $this->Form->input('LeadEmailDetail.'.$key.'.id', array('type' => 'hidden',));?>
<?php echo $this->Form->input('LeadEmailDetail.'.$key.'.first_name', array('label' => 'First Name',));?>
<?php echo $this->Form->input('LeadEmailDetail.'.$key.'.email', array('label' => 'Email',));?>
<?php echo $this->Form->input('LeadEmailDetail.'.$key.'.country_id', array(
'type' => 'select',
'options' => $Country,
)
);
?>
<?php endforeach; ?>
在edit.ctp文件中未获取国家/地区默认值且未更新lead_email_details中的country_id。
部分代码无效
<?php echo $this->Form->input('LeadEmailDetail.'.$key.'.country_id', array(
'type' => 'select',
'options' => $Country,
)
);
?>
?>
提交编辑表单后,country_id字段不会在下面的数组中更新 -
[LeadEmailDetail] => Array
(
[0] => Array
(
[id] => 5
[first_name] => vikas
[email] => vikastyagi87@gmail.com
[country_id] => 5
)
)