As marshaller accepts existing related data OR new related data, but NOT
both I had do like this.
$contact = $this->Contacts->newEntity($this->request->data);
if($this->request->data){
//debug($this->request->data);
/*'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)
]]
*/
foreach($this->request->data['skills']['_ids'] as $i => $skill){
if(mb_substr($skill, 0,1) == '~'){
$skill = ltrim($skill, '~');
$contact['skills'][] = $this->Contacts->Skills->newEntity([
'name' => $skill]);
}
}
}
And now it works like a charm. :)
CakePHP is great :)
2014. december 16., kedd 9:40:22 UTC+1 időpontban José Lorenzo a következőt
írta:
>
> _ids only works with sending existing ids, you will need to send data
> formatted correctly so the marshaller creates entities of the desired type.
> Similar to what you would do for creating belongsTo or hasMany associations.
>
> On Monday, December 15, 2014 7:13:35 PM UTC+1, Radharadhya Dasa wrote:
>>
>> 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' => '[email protected] <javascript:>',
>> '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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.