Monday, December 22, 2014

Re: 3.0: Saving new entity with belongsToMany data

Sorry this took several days to respond, i was sick all weekend, didn't get anywhere near my computer.

i changed the names of the inputs from listingsattrs >> listings_attrs as you recommended (no clue how i missed that to begin with). My POST data now looks exactly as it did before, except for the name of that particular item. However, the log output for the $listing entity is still missing the associated ListingsAttrs data.

The ListingsAttrs Entity didn't have a $_accessible array, so i added a default:
$_accessible = [
 
'*' => true
];

The controller code looks like:
$listing = $this->Listings->newEntity($this->request->data);

/* i've also tried:
$listing = $this->Listings->newEntity($this->request->data, ['associated' => ['ListingsAttrs']]);
$listing = $this->Listings->newEntity($this->request->data, ['associated' => ['ItemAttrs']]);
$listing = $this->Listings->newEntity($this->request->data, ['associated' => ['ListingsAttrs.ItemAttrs']]);
$listing = $this->Listings->newEntity($this->request->data, ['associated' => ['ItemAttrs.ListingsAttrs']]);
*/


if ($this->Listings->save($listing)) {
 
// etc.
}
else {
 
throw new Exception...
}

What else can i try? i appreciate the help.
-joe

On Friday, 19 December 2014 11:29:12 UTC-5, José Lorenzo wrote:
You should post a property called listings_attrs, check your entity $_accessible array to make sure the property is also writable with request data.

On Friday, December 19, 2014 6:29:43 AM UTC+1, Joe T. wrote:
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: