SaveMany 用于在 cakePHP 中同时更新多个记录不起作用

2021-12-21 00:00:00 php cakephp cakephp-2.5

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

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

  • 应聘者有许多应聘者雇主
  • 应聘者雇主属于应聘者
  • Candidates hasMany CandidatesEmployer
  • CandidatesEmployer belongsTo Candidates

Candidate.php 中的模型关联:

Model associations in 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;

请求数据 这里

推荐答案

这是因为您定义表单域的方式.

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

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

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

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

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.

相关文章