SaveMany для обновления нескольких записей одновременно в CakePHP не работает

Я столкнулся с проблемой обновления нескольких записей одновременно с saveMany, у меня есть ассоциация вроде:

  • Кандидаты имеют много кандидатов
  • КандидатыEmployer принадлежит кандидатам

Модельные ассоциации в Candidate.php:

public $hasMany = array( 'CandidatesEmployer' => array( 'className' => 'CandidatesEmployer', 'foreignKey' => 'candidate_id' ) } 

вот метод в CandidatesController :

 public function jbseeker_empdetails() { $this->layout = 'front_common'; $Employers = $this->Candidate->CandidatesEmployer->find('all', array( 'conditions' => array( 'candidate_id = ' => $this->Auth->user('id') ), 'recursive' => -1 )); $this->set('Employers', $Employers); $this->set('data', $this->request->data); if ($this->request->is('post') && !empty($this->request->data)): if ($this->Candidate->CandidatesEmployer->saveMany($this->request->data)): $this->Session->setFlash('You Employers details has been successfully updated'); return $this->redirect(array( 'controller' => 'candidates', 'action' => 'jbseeker_dashboard' )); else: $this->Session->setFlash('You Employers details has not been ' . 'updated successfully, please try again later!!'); endif; endif; } 

jbseeker_empdetails.ctp

 <h1>Enter the Employers details</h1> <?php if (empty($Employers)): echo $this->Form->create('Candidate', array('class' => 'dynamic_field_form')); echo $this->Form->input('CandidatesEmployer.0.candidate_id', array( 'type' => 'hidden', 'value' => $userId )); echo $this->Form->input('CandidatesEmployer.0.employer'); echo $this->Form->input('CandidatesEmployer.0.from_year', array( 'options' => array_combine(range(1950, date('Y'), 1), range(1950, date('Y'), 1)) )); echo $this->Form->input('CandidatesEmployer.0.from_month', array( 'options' => array_combine(range(1950, date('Y'), 1), range(1950, date('Y'), 1)) )); echo $this->Form->input('CandidatesEmployer.0.to_year', array( 'options' => array_combine(range(1950, date('Y'), 1), range(1950, date('Y'), 1)) )); echo $this->Form->input('CandidatesEmployer.0.to_month', array( 'options' => array_combine(range(1950, date('Y'), 1), range(1950, date('Y'), 1)) )); echo $this->Form->input('CandidatesEmployer.0.position_type'); echo $this->Form->input('CandidatesEmployer.0.headline'); echo $this->Form->input('CandidatesEmployer.0.designation'); echo $this->Form->input('CandidatesEmployer.0.role'); echo $this->Form->input('CandidatesEmployer.0.annual_salary_lakhs'); echo $this->Form->input('CandidatesEmployer.0.annual_salary_thousands'); echo $this->Form->input('CandidatesEmployer.0.position_summary'); echo $this->Form->input('CandidatesEmployer.0.notice_period'); echo $this->Form->input('CandidatesEmployer.0.job_profile'); echo $this->Form->input('CandidatesEmployer.0.created_on', array( 'type' => 'hidden', 'value' => date('Ymd H:i:s') )); echo $this->Form->input('CandidatesEmployer.0.created_ip', array( 'type' => 'hidden', 'value' => $clientIp )); echo $this->Form->button('Submit', array('type' => 'submit', 'class' => 'submit_button')); echo $this->Form->end(); ?> <button class="add_more">Add more</button> <!-- At the end script --> <script type="text/javascript"> var i = 1; $(".add_more").click(function () { $.ajax({ url: " <?php echo $this->Html->url(array( $prefixUsed => FALSE, 'jbseeker' => TRUE, 'controller' => 'candidates', 'action' => 'jbseeker_generate_emp_form')) ?> / " + i, type: 'GET', success: function (result) { $('.dynamic_field_form').append(result); }}); i++; }); </script> <?php else: ?> <?php echo $this->Form->create('Candidate', array('class' => 'dynamic_field_form')); $count = 0; foreach ($Employers as $employer): echo $this->Form->input('CandidatesEmployer.' . $count . '.id', array( 'type' => 'hidden', 'value' => $employer['CandidatesEmployer']['id'] )); echo $this->Form->input('CandidatesEmployer.' . $count . '.candidate_id', array( 'type' => 'hidden', 'value' => $userId )); echo $this->Form->input('CandidatesEmployer.' . $count . '.employer', array('value' => $employer['CandidatesEmployer']['employer'])); echo $this->Form->input('CandidatesEmployer.' . $count . '.from_year', array( 'options' => array_combine(range(1950, date('Y'), 1), range(1950, date('Y'), 1)), 'default' => $employer['CandidatesEmployer']['from_year'] )); echo $this->Form->input('CandidatesEmployer.' . $count . '.from_month', array( 'options' => array_combine(range(1950, date('Y'), 1), range(1950, date('Y'), 1)), 'default' => $employer['CandidatesEmployer']['from_month'] )); echo $this->Form->input('CandidatesEmployer.' . $count . '.to_year', array( 'options' => array_combine(range(1950, date('Y'), 1), range(1950, date('Y'), 1)), 'default' => $employer['CandidatesEmployer']['to_year'] )); echo $this->Form->input('CandidatesEmployer.' . $count . '.to_month', array( 'options' => array_combine(range(1950, date('Y'), 1), range(1950, date('Y'), 1)), 'default' => $employer['CandidatesEmployer']['to_month'] )); echo $this->Form->input('CandidatesEmployer.' . $count . '.position_type', array('value' => $employer['CandidatesEmployer']['position_type'])); echo $this->Form->input('CandidatesEmployer.' . $count . '.headline', array('value' => $employer['CandidatesEmployer']['headline'])); echo $this->Form->input('CandidatesEmployer.' . $count . '.designation', array('value' => $employer['CandidatesEmployer']['designation'])); echo $this->Form->input('CandidatesEmployer.' . $count . '.role'); echo $this->Form->input('CandidatesEmployer.' . $count . '.annual_salary_lakhs', array( 'options' => array_combine(range(10, 90, 10), range(10, 90, 10)), 'default' => $employer['CandidatesEmployer']['annual_salary_lakhs'] )); echo $this->Form->input('CandidatesEmployer.' . $count . '.annual_salary_thousands', array( 'options' => array_combine(range(10, 90, 10), range(10, 90, 10)), 'default' => $employer['CandidatesEmployer']['annual_salary_thousands'] )); echo $this->Form->input('CandidatesEmployer.' . $count . '.position_summary', array('value' => $employer['CandidatesEmployer']['position_summary'])); echo $this->Form->input('CandidatesEmployer.' . $count . '.notice_period', array('value' => $employer['CandidatesEmployer']['notice_period'])); echo $this->Form->input('CandidatesEmployer.' . $count . '.job_profile', array('value' => $employer['CandidatesEmployer']['job_profile'])); echo $this->Form->input('CandidatesEmployer.' . $count . '.updated_on', array( 'type' => 'hidden', 'value' => date('Ymd H:i:s') )); echo $this->Form->input('CandidatesEmployer.' . $count . '.updated_ip', array( 'type' => 'hidden', 'value' => $clientIp )); echo "<hr>"; ?> <?php $count++; endforeach; ?> <?php echo $this->Form->button('Submit', array('type' => 'submit', 'class' => 'submit_button')); echo $this->Form->end(); ?> <br> <?php echo $this->Html->link('Add another Employer', array( 'controller' => 'candidates', 'action' => 'jbseeker_addemployer' )); ?> <?php endif; 

запросить данные здесь

Это связано с тем, как вы определили поля формы.

В Cake Book для saveMany указано, что данные должны быть в «числовом индексе вместо ключевого слова статьи». http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-savemany-array-data-null-array-options-array

Таким образом, ваши данные являются числовым индексом, но под ключом CandidatesEmployer, поэтому функция saveMany не видит никаких данных для сохранения, следовательно, никаких ошибок.

Изменение вашей строки сохранения должно работать:

 $this->Candidate->CandidatesEmployer->saveMany($this->request->data['CandidatesEmployer']) 

Таким образом передается только числовой индекс.