prajakta
prajakta

Reputation: 21

Multiple save in a for loop for cakephp

I'm using 2 databases. I fetch data from one database for a particular id from multiple tables and save it in another database. While I'm doing this, the data which requires a for loop returns the same data multiple times, instead of different data. Please help me in rectifying the error. Below is the code:

 $q5 = $this->Model->function($empcode[$j]);
                if (!empty($q5)) {
                    for ($i = 0; $i < count($q5); $i++) {
                    $name = $q5[0][0]['tf_rel_fname'] . ' ' . $q5[0]   [0]['tf_rel_mname'] . ' ' . $q5[0][0]['tf_rel_lname'];
                    $data5 = array('emp_cd' => $empcode[$j],
                        'ppo_number' => $empcode[$j],
                        'family_member_name' => $name,
                        'relationship' => $q5[0][0]['tf_rel_cd'],
                        'is_family_pensioner' => 'N',
                        'dob' => date('Y-m-d', strtotime($q5[0][0]['tf_rel_dob'])),
                        'disability_percentage' => $q5[0][0]['ph_disab_per'],
                        'physically_handicapped' => $q5[0][0]['ph_cd'],
                    );

                    $this->ppmfamily_details->create(false);
                    $this->ppmfamily_details->save($data5);
                    }

                }

Here the count is 3. Instead of giving the result as a,b,c it returns a,a,a.

Upvotes: 1

Views: 3633

Answers (2)

Tousif Ahmed
Tousif Ahmed

Reputation: 1082

saveMany should be helpful here.

Example:

$data = array(
array('title' => 'title 1', 'Assoc' => array('field' => 'value')),
array('title' => 'title 2'),
);
$data = array(
array(
    'Article' => array('title' => 'title 1'),
    'Assoc' => array('field' => 'value')
),
array('Article' => array('title' => 'title 2')),
);
$Model->saveMany($data, array('deep' => true));

Upvotes: 0

JadedCore
JadedCore

Reputation: 2011

When saving data in a loop you need to call

$this->YourModel->clear();

after every save.

$q5 = $this->Model->function($empcode[$j]);
  if (!empty($q5)) {
    for ($i = 0; $i < count($q5); $i++) {
      $name = $q5[0][0]['tf_rel_fname'] . ' ' . $q5[0][0]['tf_rel_mname'] . ' ' . $q5[0][0]['tf_rel_lname'];
      $data5 = array('emp_cd' => $empcode[$j],
        'ppo_number' => $empcode[$j],
        'family_member_name' => $name,
        'relationship' => $q5[0][0]['tf_rel_cd'],
        'is_family_pensioner' => 'N',
        'dob' => date('Y-m-d', strtotime($q5[0][0]['tf_rel_dob'])),
        'disability_percentage' => $q5[0][0]['ph_disab_per'],
        'physically_handicapped' => $q5[0][0]['ph_cd'],
      );
      $this->ppmfamily_details->clear();
      $this->ppmfamily_details->create(false);
      $this->ppmfamily_details->save($data5);
    }
  }

Here is more information: https://book.cakephp.org/2.0/en/models/saving-your-data.html#model-save-array-data-null-boolean-validate-true-array-fieldlist-array

Pay special attention to the info box about saving data in loops.

Upvotes: 1

Related Questions