SaveMany for update multiple records at a same time in cakePHP not working

6.3k Views Asked by At

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

1

There are 1 best solutions below

1
On BEST ANSWER

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.