i have same problem, i can't save all my associated data. so for now i have to do it manualy save my associated one model by one model. José Lorenzo i hope cookbook give us more clear example hove to save associated models.
On Sunday, February 22, 2015 at 9:01:16 AM UTC+7, Joe T. wrote: > > So, i had a thread going here: > https://groups.google.com/forum/#!topic/cake-php/PMtHkEvIURo > > Never got anywhere on solving this. Decided a new thread was called for > because tables have changed & documentation has been updated, but i'm > having the same problem. > > i'll try to explain as clearly as i can... > > i have two B2M tables: *li**stings* and *attributes*. The pivot table is > *listings_attributes*: > listings_attributes: > id > listing_id -FK-> listings.id > attribute_id -FK-> attributes.id > value > > The Table connections look like this: > *ListingsTable::initialize* > $this->addAssociations([ > 'belongsTo' => [ /* some other tables */ ], > 'hasMany' => [ /* another table */ ], > > 'belongsToMany' => [ > 'Attributes' => [ > 'through' => 'ListingsAttributes' > ] > ] > ]); > > > *AttributesTable::initialize* > $this->belongsToMany('Listings', [ > 'through' => 'ListingsAttributes' > ]); > > > *ListingsAttributesTable::initialize* > $this->belongsTo('Listings'); > $this->belongsTo('Attributes'); > > > The Attributes are already established. i'm not adding new Attributes when > i save a Listing. The documentation's example is Articles & Tags, and > implies new Tags are created with a new Article, while the _joinData > "starred" is saved to the pivot table *articles_tags*. > > So my form inputs look like this (following the example from documentation > <http://book.cakephp.org/3.0/en/views/helpers/form.html#associated-form-inputs> > ): > <input name="attributes[0][id]" value="3" id="attributes-0-id" type= > "checkbox"> Bathrooms > <input name="attributes[0][_joinData][value]" id= > "attributes-0-joindata-value" type="text"> > > <input name="attributes[1][id]" value="2" id="attributes-1-id" type= > "checkbox"> Bedrooms > <input name="attributes[1][_joinData][value]" id= > "attributes-1-joindata-value" type="text"> > > <input name="attributes[2][id]" value="4" id="attributes-2-id" type= > "checkbox"> Laundry > <input name="attributes[2][_joinData][value]" id= > "attributes-2-joindata-value" type="text"> > > <input name="attributes[3][id]" value="1" id="attributes-3-id" type= > "checkbox"> Parking > <input name="attributes[3][_joinData][value]" id= > "attributes-3-joindata-value" type="text"> > > > The idea is, an Attribute is checked, and a value entered that goes to the > *listings_attributes* table. In my test, i'm checking "Parking" (ID 1, > index 3), and entering "Off-street" as its value. > > My response data looks like (from Log::debug): > Array > ( > // Various fields about the listing, and this section for the > attributes: > [attributes] => Array > ( > [3] => Array // Index for this Attribute in the form's inputs. > ( > [id] => 1 // Attribute record ID for "Parking" > [_joinData] => Array // Data intended for the > ListingsAttributes table > ( > [value] => Off-street > ) > ) > ) > ) > > To me, this looks much like the documentation example for Students / > Courses: > $data = [ > 'first_name' => 'Sally', > 'last_name' => 'Parker', > 'courses' => [ > [ > 'id' => 10, > '_joinData' => [ > 'grade' => 80.12, > 'days_attended' => 30 > ] > ], > // Other courses. > ] > ]; > $student = $this->Students->newEntity($data, [ > 'associated' => ['Courses._joinData'] > ]); > > In my controller, i create the Entity thus (just like the example above): > $listing = $this->Listings->newEntity($this->request->data, ['associated' > => ['Attributes._joinData']]); > > When i call *$this->Listings->save($listing)* i'd expect the *listings* > record to be saved, then the ID for that added to the associated data for > *listings_attributes*: > listings_id : 1 (newly generated) > attributes_id: 1 (selected from the form) > value : 'Off-street' (set in the form) > > When i log *$listing->jsonSerialize()*, it looks like this: > Array > ( > // Various fields about the listing, and this section for the > attributes: > [attributes] => Array > ( > [0] => Array > ( > ) > ) > ) > > The result of this is the *listings* record is not saved, nor the > *listings_attributes*. > > So, where did the Attribute data go? Is something wrong with the Table > classes? Can anyone tell me what's wrong here? If you need more > information, please let me know. > > Thank you. > -joe > -- 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.
