Tuesday, July 9, 2013

Re: Parent/Child Association: confusion in creating records at the same time?

The Model::create() method does not actually create a record. It's more like init() really. It's especially important when saving multiple records in a loop.

Anyway, what you need to do is save the Group record first:

$this->Request->Group->create();
$this->Request->Group->set($this->data);
$this->Request->Group->save();

$this->data['Request']['group_id'] = $this->Request->Group->id;

I'm pretty sure the above will work with 1.1 but ymmv.


On Tue, Jul 9, 2013 at 6:18 AM, David Carr <dcarr@scienceprousa.com> wrote:
I will do my best to explain my situation in the hopes it will be clear enough for someone to help me out:

I currently have a 'Requests' model.  This consists of a web form and a controller that saves one or more records based on the users selection on the form (for example, if they make a bunch of selections in a multi-select box, multiple records are generated and saved).  

This works fine, but I want all of these 'Requests' to be grouped together for easy reference, since they are related.  I created a new model called 'Groups.'  In my 'Requests' model, I put:

    var $belongsTo = array('Group' => array('className' => 'Group', 'foreignKey' => 'group_id'));

and created the group_id column in the 'requests' database table.

In the 'Groups' controller, I put

    var $hasMany = array('Requests' => array('className' => 'Request'));

Now, in my 'Requests' controller, where I do all of the processing of the form input and create the records, I tried creating a new 'Group' with 

    $this->Request->Group->create();

This works, and the 'Group' database record is made (along with the 'Requests' as appropriate), but the 'group_id' field in the 'Requests' record is not set properly.  When I try to do it manually, like setting it to `$this->Request->data['Group']['id']` or `$this->Request->Group->id`, CakePHP tells me this value is NULL.  So I have no way to know if these associations are even working at all.  

Clearly I'm doing something wrong - do I have to move all of the creating and saving logic from my 'Requests' controller to my 'Groups' controller?  Seems like this would be a pain.  Is there a problem with creating a "Parent" and a bunch of "Children" at once from the controller of the "Children?"

For the record - I'm using CakePHP 1.1, and unfortunately cannot upgrade.  

--
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/groups/opt_out.
 
 

--
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/groups/opt_out.
 
 

No comments: