我在使用saveMany同时更新多条记录时遇到问题,我有这样的关联:
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('Y-m-d 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('Y-m-d 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;
请求数据 here
答案 0 :(得分:2)
这是因为您定义了表单字段的方式。
saveMany
的Cake Book声明数据应该是“数字索引而不是文章键”。
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'])
这样只传递数字索引。