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'));
}
Leads \ View \ Ведет \ 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] => [email protected]
[Country] => Array
(
[id] => 101
[name] => INDIA
)
)
[1] => Array
(
[id] => 8
[lead_id] => 1
[county_id] => 21
[name] => Vikass
[email] => [email protected]
[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 не попадает значение страны по умолчанию и не обновляется country_id в lead_email_details.
Код детали ниже не работает
<?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] => [email protected]
[country_id] => 5
)
)
Задача ещё не решена.
Других решений пока нет …