SaveMany同时更新多个记录在cakePHP不工作 [英] SaveMany for update multiple records at a same time in cakePHP not working

查看:174
本文介绍了SaveMany同时更新多个记录在cakePHP不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用saveMany同时更新多个记录时遇到问题,我有以下关联:




  • 候选人 hasMany CandidatesEmployer

  • Candidates雇员 belongsTo 候选人



Candidate.php中的关联:

  public $ hasMany = array(
'CandidatesEmployer'=> array $ b'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->是('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'=>'candidate',
'action'=>'jbseeker_dashboard'

else:
$ this-> Session-> setFlash('You Employers details'not been been'
。'successfully successfully,please try again later !!');
endif;

endif;
}

jbseeker_empdetails.ctp
$ b

 < h1>输入雇主详情< / 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 '),1),range(1950,date('Y'),1))
));

echo $ this-> Form-> input('CandidatesEmployer.0.from_month',array(
'options'=> array_combine(range,1950,date '),1),range(1950,date('Y'),1))
));

echo $ this-> Form-> input('CandidatesEmployer.0.to_year',array(
'options'=> array_combine(range(1950,date '),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>添加更多< / button>

<! - 在结束脚本 - >
< 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',
' =>'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 $ b'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'] [雇主']));

echo $ this-> Form-> input('CandidatesEmployer。'。$ count。'.from_year',array(
'options'=> array_combine ,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 ,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 ,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 ,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'] ['标题']));

echo $ this-> Form-> input('CandidatesEmployer。'。$ count。'.designation',array('value'=> $ employer ['CandidatesEmployer'] [指定']));

echo $ this-> Form-> input('CandidatesEmployer。'。$ count。'.role');

echo $ this-> Form-> input('CandidatesEmployer。'。$ count。'.annual_salary_lakhs',array(
'options'=> array_combine ,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 ,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'] ['通知期']));

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;

请求数据 这里

解决方案

这是因为您定义表单字段。



saveMany 的Cake Book说明数据应该在数字索引而不是文章键。
http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-savemany-array-data-null-array-options-array p>

因此,您的数据是一个数字索引,但在CandidatesEmployer键下,因此 saveMany 函数没有看到任何数据



将您的保存行更改为:

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

这样只有数字索引被传递。


I am facing problem with updating multiple record at a same time with saveMany, I have association like:

  • Candidates hasMany CandidatesEmployer
  • CandidatesEmployer belongsTo Candidates

Model associations in Candidate.php:

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

here is the method in 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;

request data here

解决方案

It's because of the way you have defined the form fields.

The Cake Book for saveMany states that data should be in "numerical index instead of article key". http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-savemany-array-data-null-array-options-array

So, your data is a numerical index but under the CandidatesEmployer key, so the saveMany function isn't seeing any data to save, hence no errors.

Changing your save line to this should work:

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

This way only the numerical index is passed.

这篇关于SaveMany同时更新多个记录在cakePHP不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆