0

This code:

foreach ($arrays as $array){
    $this->Model->set('bla', $array['key1']);
    $this->Model->set('alb', $array['key2']);
    $this->Model->save();
    echo $this->Model->getLastInsertID() . ",";
}

returns:

2,2

should return:

1,2

I tried using

foreach ($arrays as $array){
    $this->Model->query('insert....');
    $this->Model->query('select last_insert_id()')
    }

but that returned "1,1"... So what I'm doing wrong??

uldo
  • 19
  • 2
  • 8

1 Answers1

2

When saving inside a loop, you need to explicitly call Model::create() first.

foreach ($arrays as $array){
    $this->Model->create();
    $this->Model->set('bla', $array['key1']);
    $this->Model->set('alb', $array['key2']);
    $this->Model->save();
    echo $this->Model->getLastInsertID() . ",";
}
Guillermo Mansilla
  • 3,779
  • 2
  • 29
  • 34
  • Somehow I didnt think of it :D ... Thanks... Should have asked first... I would save a lot of time and keyboards. – uldo Nov 24 '13 at 21:25
  • Thanks for the create() tip, I was struggling a lot and I was finally able to solve the problem thanks to that method. – Vito Aug 04 '15 at 23:22