Thursday, January 22, 2015

Re: 3.0: Saving new entity with belongsToMany data

Yes, i agree i left that pretty open-ended. My apologies. And no offense intended toward you for not being able to follow up sooner. i've been busy too & just haven't been able to keep beating on this on my own.

In App/Controller/Admin/ListingsController.php, the Entity is created ~line 124. Just inside the try{} i have 3 Log::debug lines, and according to the Debug plugin, those now have the data i expect (which is in a previous message on this thread). However, while the save now succeeds for the parent table (Listings), it still isn't saving anything to the associated child (ListingsAttrs).

i don't know if it's a problem with the way i'm setting up the fields & thus that data is going into the POST:

// App/Template/Element/Admin/Form/listing-attributes.ctp produces this part of the POST data:
[listings_attrs] => Array (
   
[4] => Array (
       
[attr_id] => 4
   
)
   
[1] => Array (
       
[attr_id] => 1
       
[value] => Off-street
   
)
)

Or if it's something wrong with the Tables (App/Model/Table/ListingsTable.php & App/Model/Table/ListingsAttrsTable.php)
Or it's a problem with my Entity prep in the Controller.
Or something else i'm missing completely.

i didn't go into a lot of detail before as i hope it's just something simple that would jump out as wrong since you know how things should work.

As i noted before, being able to see this in action is behind a login i'd rather provide privately if you can take a little time to see it happening. If you don't have the time, i understand. Free advice & help has its limitations. You guys are already very generous helping me & the rest of the group. :)

Oh, hey, i just discovered the private reply menu item... i'll send the info that way.

Thanks again.
-joe


On Thursday, 22 January 2015 04:03:38 UTC-5, José Lorenzo wrote:
No, I have not had the time to check the code. I was expecting something more manageable for me to review and not a full application. Or at least instructions for getting your code to run and where in your code the problem is.

On Thursday, January 22, 2015 at 5:39:21 AM UTC+1, Joe T. wrote:
Did i get lost in the shuffle? i confess i've been busy with other things myself, but have been checking here to see if there was any response with some ideas what i'm doing wrong...

Any help? Please? :)

Always appreciated.
-joe


On Tuesday, 13 January 2015 08:04:37 UTC-5, Joe T. wrote:
https://bitbucket.org/cautionbug/kodiak-investments

The stuff i'm having trouble with is behind an admin login. If you need access, i'd prefer to send that privately.

Thanks for responding & being willing to take a look.

-joe


On Tuesday, 13 January 2015 03:38:36 UTC-5, José Lorenzo wrote:
I think you should put your code somewhere so we can take a look, otherwise trying to guess what the problem is will take a while.

On Tuesday, January 13, 2015 at 4:02:56 AM UTC+1, Joe T. wrote:
Wondering if someone can help me take a closer look at this... i'm still not having any luck & can't really get much forward progress until i understand this.

If you need more information to determine suggestions, let me know. Any suggestions are appreciated.

-joe


On Tuesday, 30 December 2014 01:52:54 UTC-5, Joe T. wrote:
i figured out part of my trouble, but the purpose of the original post still remains.

The parent record in Listings wasn't saving correctly because i didn't have an `active` field in the form (facepalm). Corrected that. Still having trouble with the date field, kinda frustrated there. But to the point...

When i save my data, i'm now getting a record in Listings. Yay! But the associated data in ListingsAttrs is still not there. Boo!

Current structure of ListingsAttrs POST data:
[listings_attrs] => Array (
   
[4] => Array (
       
[attr_id] => 4
   
)
   
[1] => Array (
       
[attr_id] => 1
       
[value] => Off-street
   
)
)

Which now gives me this in the $listing Entity:
"listings_attrs": {
   
"4": {
       
"attr_id": "4"
   
},
   
"1": {
       
"attr_id": "1",
       
"value": "Off-street"
   
}
}

This is progress! The associated data wasn't in previous debug data.

So what am i still missing that it won't save that data? Remember, `value` is optional to the data record, it's controlled by the UI. So both of those records should be valid. Why then is neither of them being saved?

Any additional help here is appreciated.
-joe t.


On Monday, 22 December 2014 22:54:12 UTC-5, Joe T. wrote:
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: