Answers.Body: <p>The section ID was incorrect - set correctly and this function works.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-02T20:00:40.557Z
Answers.LastActivityDate: 2017-12-02T20:00:40.557Z
Answers.UserDisplayName: Graham Winston
Body: <p>This question is a follow up of a previous question see: (<a href="https://craftcms.stackexchange.com/questions/22749/entry-form-multiple-entries-entry-type-parent">Entry Form - Multiple Entries &amp; Entry Type &amp; Parent</a>)</p>
  
  <p>I am trying to add multiple children to a pre-existing multi level structure:</p>
  
  <pre><code>Level 1
  --Level 2 (The parent where I am adding the children)
  ----Child 1
  ----Child 2
  ..etc 
  </code></pre>
  
  <p>My Controller: </p>
  
  <pre><code>public function actionSaveAnimals(){
  $this-&gt;requirePostRequest();
  
  if($rodentsData = craft()-&gt;request-&gt;getParam('rodent')){
    $rodentErrors = [];
    foreach ($rodentsData as $rodentData){
        if (array_key_exists('entryId', $rodentData) &amp;&amp; $rodentId = $rodentData['entryId']) {
            if (!$rodent = craft()-&gt;elements-&gt;getElementById($rodentId)) {
                throw new Exception('Could not find rodent with the given Id');
            }
        } else {
            $rodent = new EntryModel();
            $rodent-&gt;typeId = 6; // change your entryType Id here
            $rodent-&gt;sectionId = craft()-&gt;request-&gt;getParam('sectionId');
            $rodent-&gt;parentId = craft()-&gt;request-&gt;getParam('parentId');
            $rodent-&gt;authorId = craft()-&gt;request-&gt;getParam('authorId');
        }
  
        $rodent-&gt;getContent()-&gt;setAttributes($rodentData['fields']);
  
        if (!craft()-&gt;entries-&gt;saveEntry($rodent)) {
            $rodentErrors[] = $rodent-&gt;getErrors();
        }else{
            $rodentErrors = [];
        }
    }
    // include errors of each item
    craft()-&gt;urlManager-&gt;setRouteVariables(array(
        'rodentErrors' =&gt; $rodentErrors
    ));
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And my form: </p>
  
  <pre><code>&lt;form method="post" accept-charset="UTF-8"&gt;
  {{ getCsrfInput() }}
  {# your plugin name and function name #}
  &lt;input type="hidden" name="action" value="experiment/SaveAnimals"&gt;
  {# the id of your section for me its 4 you need to change this value to your section Id #}
  &lt;input type="hidden" name="sectionId" value="4"&gt;
  {# enable the entry #}
  &lt;input type="hidden" name="enabled" value="1"&gt;
  {# id of your entry leave it blank to insert a new entry, insert a id to change an existing entry#}
  &lt;input type="hidden" name="entryId" value=""&gt;
  &lt;input type="hidden" name="parentId" value="{{ entry.id }}"&gt;
  &lt;input type="hidden" name="authorId" value="{{ currentUser.id }}"&gt;
  
  &lt;div class="fields"&gt;
    &lt;div class="field mt3"&gt;
      &lt;label class="f5 lh-copy pb2" for="title"&gt;Amount of Animals&lt;/label&gt;
      {#&lt;input class="pa3 input-reset ba bw0 br2 w-100" type="number" id="Cage__Amount" placeholder="5" max="10" required&gt;#}
      &lt;input type="text" name="rodent[0][fields][title]" value="Add Identifier"&gt;
      &lt;input type="text" name="rodent[1][fields][title]" value="Add Identifier"&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="field submit mv4"&gt;
    &lt;button class="bg-dark-blue white ph3 pv2 input-reset pointer ba bw0 f5 dib lh-copy w-100" type="submit"&gt;Create experiment&lt;/button&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>I am currently getting this error: </p>
  
  <p>Trying to get property of non-object </p>
  
  <p>From EntriesService.php(468) </p>
  
  <pre><code>if ($entry-&gt;getSection()-&gt;type != SectionType::Structure)
  </code></pre>
  
  <p>Any help much appreciated!</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-12-02T19:01:30.217Z
Comments.Text: Is the ID of your entry type 6? Because that was my ID - you did not even remove my hint to change it. If yours is not 6 then you know why this error occurs
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-12-02T19:05:22.057Z
Comments.Text: Hi Robin, yes the my entry type is 6, thanks! -Graham
Comments.UserDisplayName: Graham Winston
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-12-02T19:21:38.893Z
Comments.Text: Hi Graham, this is really creepy because when I copy your code into my craft (I did not delete the structure I created the last time for you) Your script adds 2 children to the cage. https://imgur.com/a/eoHzH would you please do `var_dump($rodent->getSection());die();` right before you save the entry and tell me if your result is something like `object(Craft\SectionModel)` if not then please just include a `$section = craft()->sections->getSectionById(4)` right at the start of your controller, and check if `$section->handle` is your correct handle. My guess is: your ids are wrong
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-12-02T19:29:03.270Z
Comments.Text: Hi Robin - my section id was wrong, it has changed recently! Debugging is something I need a better grasp of. Thanks a bunch for the quick replies again!
Comments.UserDisplayName: Graham Winston
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-12-02T19:34:11.203Z
Comments.Text: You are welcome. Since I have not done very much you could answer your question and mark it. Otherwise this topic will be pumped up from time to time.
Comments.UserDisplayName: Robin Schambach
Tags: <structure><controller><saveentry>
Title: Posting children to a Structure
ThreadText: # Posting children to a Structure
  
  (Asked by: username_0 on 2017-12-02)
  
  This question is a follow up of a previous question see: ([Entry Form - Multiple Entries & Entry Type & Parent](https://craftcms.stackexchange.com/questions/22749/entry-form-multiple-entries-entry-type-parent))
  
  I am trying to add multiple children to a pre-existing multi level structure:
  
  ```
  Level 1
  --Level 2 (The parent where I am adding the children)
  ----Child 1
  ----Child 2
  ..etc 
  
  ```
  
  My Controller:
  
  ```
  public function actionSaveAnimals(){
  $this->requirePostRequest();
  
  if($rodentsData = craft()->request->getParam('rodent')){
    $rodentErrors = [];
    foreach ($rodentsData as $rodentData){
        if (array_key_exists('entryId', $rodentData) && $rodentId = $rodentData['entryId']) {
            if (!$rodent = craft()->elements->getElementById($rodentId)) {
                throw new Exception('Could not find rodent with the given Id');
            }
        } else {
            $rodent = new EntryModel();
            $rodent->typeId = 6; // change your entryType Id here
            $rodent->sectionId = craft()->request->getParam('sectionId');
            $rodent->parentId = craft()->request->getParam('parentId');
            $rodent->authorId = craft()->request->getParam('authorId');
        }
  
        $rodent->getContent()->setAttributes($rodentData['fields']);
  
        if (!craft()->entries->saveEntry($rodent)) {
            $rodentErrors[] = $rodent->getErrors();
        }else{
            $rodentErrors = [];
        }
    }
    // include errors of each item
    craft()->urlManager->setRouteVariables(array(
        'rodentErrors' => $rodentErrors
    ));
  }
  
  ```
  
  }
  
  And my form:
  
  ```
  <form method="post" accept-charset="UTF-8">
  {{ getCsrfInput() }}
  {# your plugin name and function name #}
  <input type="hidden" name="action" value="experiment/SaveAnimals">
  {# the id of your section for me its 4 you need to change this value to your section Id #}
  <input type="hidden" name="sectionId" value="4">
  {# enable the entry #}
  <input type="hidden" name="enabled" value="1">
  {# id of your entry leave it blank to insert a new entry, insert a id to change an existing entry#}
  <input type="hidden" name="entryId" value="">
  <input type="hidden" name="parentId" value="{{ entry.id }}">
  <input type="hidden" name="authorId" value="{{ currentUser.id }}">
  
  <div class="fields">
    <div class="field mt3">
      <label class="f5 lh-copy pb2" for="title">Amount of Animals</label>
      {#<input class="pa3 input-reset ba bw0 br2 w-100" type="number" id="Cage__Amount" placeholder="5" max="10" required>#}
      <input type="text" name="rodent[0][fields][title]" value="Add Identifier">
      <input type="text" name="rodent[1][fields][title]" value="Add Identifier">
    </div>
  </div>
  
  <div class="field submit mv4">
    <button class="bg-dark-blue white ph3 pv2 input-reset pointer ba bw0 f5 dib lh-copy w-100" type="submit">Create experiment</button>
  </div>
  
  ```
  
  I am currently getting this error:
  
  Trying to get property of non-object
  
  From EntriesService.php(468)
  
  ```
  if ($entry->getSection()->type != SectionType::Structure)
  
  ```
  
  Any help much appreciated!
  
  **Tags:** structure, controller, saveentry
  
  > **Comments:**
  > **username_1**: Is the ID of your entry type 6? Because that was my ID - you did not even remove my hint to change it. If yours is not 6 then you know why this error occurs  
  > **username_0**: Hi username_1, yes the my entry type is 6, thanks! -username_0  
  > **username_1**: Hi username_0, this is really creepy because when I copy your code into my craft (I did not delete the structure I created the last time for you) Your script adds 2 children to the cage. https://imgur.com/a/eoHzH would you please do `var_dump($rodent->getSection());die();` right before you save the entry and tell me if your result is something like `object(Craft\SectionModel)` if not then please just include a `$section = craft()->sections->getSectionById(4)` right at the start of your controller, and check if `$section->handle` is your correct handle. My guess is: your ids are wrong  
  > **username_0**: Hi username_1 - my section id was wrong, it has changed recently! Debugging is something I need a better grasp of. Thanks a bunch for the quick replies again!  
  > **username_1**: You are welcome. Since I have not done very much you could answer your question and mark it. Otherwise this topic will be pumped up from time to time.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2017-12-02. Score: 1 | Accepted answer
  
  The section ID was incorrect - set correctly and this function works.
  
  
  ---
  
  