开发者

Save inside loop

I created 开发者_运维问答a function that has a infinite loop and a delay at the end to make sure it will only execute one time per second.

This function is in a shell class and its goal is to save/update records in three different tables and I invoque it from the console line using the command 'cake className'

My problem is:

I stop the loop at the console (Ctrl + C) and only the last record is saved in the database.

I don't know if there is some problems with transaction, i tried to use begin() before save and commit after, but the problem subsisted.

The code is something like this:

$this->T1->begin();
$this->T2->begin();
$this->T3->begin();

 if ($this->T1->save((array (
    'Field1' => $val1,
    'Field2' => $val2,
    'Field3' => $val3))) 
    && $this->T2->save(array (
      'Field1' => $val4,
      'Field2' => $val5,
      'Field3' => $val6))) 
    && $this->T3->saveField('Field1', $val7)) 
 {
      $this->T1->commit();
      $this->T2->commit();
      $this->T3->commit();

      echo 'success message';
 }


It could be because the id is still present in each of the models which often happens when saving in a loop because the data gets merged.

Try the following to reset the models so they don't load in the previous data.

$this->Sale->create(false);
$this->Bidagent->create(false);
$this->Licitation->create(false);

From your code snippet though i'm not sure what T1, T2 and T3 are... if they are models then they need the same $this->Model->create(false);

Reference:

  • http://api.cakephp.org/2.3/class-Model.html#_create
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜