Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP Parent/Child Association: confusion in creating records at the same time?
    primarykey
    data
    text
    <p>I will do my best to explain my situation in the hopes it will be clear enough for someone to help me out:</p> <p>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). </p> <p>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:</p> <pre><code>var $belongsTo = array('Group' =&gt; array('className' =&gt; 'Group', 'foreignKey' =&gt; 'group_id')); </code></pre> <p>and created the group_id column in the 'requests' database table.</p> <p>In the 'Groups' controller, I put</p> <pre><code>var $hasMany = array('Requests' =&gt; array('className' =&gt; 'Request')); </code></pre> <p>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 </p> <pre><code>$this-&gt;Request-&gt;Group-&gt;create(); </code></pre> <p>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 <code>$this-&gt;Request-&gt;data['Group']['id']</code> or <code>$this-&gt;Request-&gt;Group-&gt;id</code>, CakePHP tells me this value is NULL. So I have no way to know if these associations are even working at all. </p> <p>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?"</p> <p>For the record - I'm using CakePHP 1.1, and unfortunately cannot upgrade. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload