Monday, December 15, 2014

cakephp 3.0 save new entity with new related data

Hi,

I want to save a new contact. The contacts has skills (belongsToMany). Skill is a text input and with jquery autocomplete I search for existing skills. If the user select existing skills their id is sent to to controller, if they create new skills the controller gets their names starting with a "~" char.

So my controller gets something like this:
/*debug($this->request->data);
  'name' => 'John Doe',
  'email' => 'jd@nowhere.com',
  'skills' => [
    '_ids' => [
      (int) 0 => '1',            //found in skills, this is the id
      (int) 1 => '~könyvelő'     //starts with "~" this is a new skill (or fast typer problem)
    ]
  ]
*/



If I have no new skill (a skill what starts with "~") I can do the following:

$contact = $this->Contacts->newEntity($this->request->data);
this->Contacts->save($contact);

But if I have new skill I should save it first, get the corresponding id, replace the name with the id and than I could save it. Is it the way? I did not find any way to get the last inserted id.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: