Thursday, December 18, 2014

3.0: Saving new entity with belongsToMany data

i've seen a couple similar threads about this, but not my exact problem.

i'm trying to follow the guide here: http://book.cakephp.org/3.0/en/orm/saving-data.html#converting-request-data-into-entities and coming up short. i'm not getting any errors (except a date field that constantly fails if it has a value, separate issue)...so i have no idea what's happening to the data i submit, or why it isn't saved to the DB.

My raw response data logged to Cake Debug is:

Array (
 
[title] => '123 Main St'
 
[street] => '123 Main St'
 
[lot_no] => 1
 
[lat] =>
 
[lng] =>
 
[city] => 'Hometown'
 
[county] =>
 
[state] => 'MI'
 
[zip] => '49000'
 
[area] => 900    
 
[bedrooms] => 2    
 
[bathrooms] => 1    
 
[price] => 525    
 
[ready_date] =>
 
[listingsattrs] => Array (
   
[4] => Array (
     
[id] => 4
   
)
   
[1] => Array (
     
[id] => 1
     
[value] => 'Off-street'
   
)
 
)
 
[park_id] => 1
 
[house_condition_id] => 3
 
[house_style_id] => 2
 
[term_id] => 2
 
[info_body] => '<p>Live here. It&#39;s great!</p>'
)

i have tables Listings, ItemAttrs, and ListingsAttrs. In ListingsTable, the relationship is defined as:
$this->addAssociations([
 
'belongsToMany' => [
   
'ItemAttrs' => [
     
'targetForeignKey' => 'attr_id',
     
'through'          => 'ListingsAttrs',
     
'saveStrategy'     => 'replace',
 
]
])'

Some attributes are required to have a value when assigned to a Listing (or other parent object they can be assigned to with a similar relationship). When a requires-value attribute is selected for the (in this case) Listing, a text field is shown & marked as required. The value is stored in the ListingsAttrs record with the Listing ID and ItemAttr ID.

When i submit the form, the correct attribute data is there. But it gets lost when i generate the Entity:

$listing = $this->Listings->newEntity($this->request->data, [
 
'associated' => ['ListingsAttrs']
]);


Log of $listing:
{
 
"title": "123 Main St",
 
"street": "123 Main St",
 
"lot_no": "1",
 
"lat": null,
 
"lng": null,
 
"city": "Hometown",
 
"county": null,
 
"state": "MI",
 
"zip": "49000",
 
"area": 900,
 
"bedrooms": 2,
 
"bathrooms": 1,
 
"price": 525,
 
"ready_date": null,
 
"park_id": 1,
 
"house_condition_id": 3,
 
"house_style_id": 2,
 
"term_id": 2,
 
"info_body": "<p>Live here. It&#39;s great!<\/p>\r\n"
}

i've tried creating the Listings entity without 'associated' at all, as 'associated' => 'ListingsAttrs', and 'associated' => 'ItemAttrs'

i'm trying to understand this ORM, because it looks like it should be very automated & easy to use. (By comparison, has anyone here used ZF1's ORM? Yikes.) But i don't know what i'm doing wrong here. It seems like i'm following the guide, but i'm obviously mixing something up.


--
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: