Answers.Body: <p>This question is old, but the solution is: </p>
  
  <pre><code>// look up parent with a ElementCriteriaModel
  
  $parentCriteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
  
  $parentCriteria-&gt;search  = 'slug:'.$parentSlug; // whatever you have
  
  $parent = $parentCriteria-&gt;find()[0];
  
  $entry = new EntryModel();
  $entry-&gt;sectionId = $section;
  
  $entry-&gt;parentId = $parent-&gt;id; 
  
  ...
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-23T20:33:18.460Z
Answers.LastActivityDate: 2015-11-23T20:33:18.460Z
Answers.UserDisplayName: gromgull
Body: <p>I have Structure-type Section for entries and want to pragmatically populate a large list of multi-tiered entries.</p>
  
  <p>I see you can create an entry like:</p>
  
  <pre><code>$entry = new EntryModel();
  $entry-&gt;getContent()-&gt;title = 'New Page';
  $entry-&gt;getContent()-&gt;body = null;
  
  craft()-&gt;entries-&gt;saveEntry($entry);
  </code></pre>
  
  <p>What I want to do is add the parent(s) of the entry, something like:</p>
  
  <pre><code>$entry = new EntryModel();
  $entry-&gt;getContent()-&gt;title = 'New Page';
  $entry-&gt;getContent()-&gt;body = null;
  
  // this entry would be a child in order of Sports -&gt; Tennis -&gt; This Entry
  $entry-&gt;getContent()-&gt;parent = sports-&gt;tennis;
  
  craft()-&gt;entries-&gt;saveEntry($entry);
  </code></pre>
  
  <p>Is there a way to do something like that?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-26T03:17:18.050Z
Comments.Text: Anyone have any ideas??
Comments.UserDisplayName: user2056505
Tags: <plugin-development><entry><entry-type><php>
Title: Set Structure Parent when creating Entry in Plugin
ThreadText: # Set Structure Parent when creating Entry in Plugin
  
  (Asked by: username_0 on 2015-06-03)
  
  I have Structure-type Section for entries and want to pragmatically populate a large list of multi-tiered entries.
  
  I see you can create an entry like:
  
  ```
  $entry = new EntryModel();
  $entry->getContent()->title = 'New Page';
  $entry->getContent()->body = null;
  
  craft()->entries->saveEntry($entry);
  
  ```
  
  What I want to do is add the parent(s) of the entry, something like:
  
  ```
  $entry = new EntryModel();
  $entry->getContent()->title = 'New Page';
  $entry->getContent()->body = null;
  
  // this entry would be a child in order of Sports -> Tennis -> This Entry
  $entry->getContent()->parent = sports->tennis;
  
  craft()->entries->saveEntry($entry);
  
  ```
  
  Is there a way to do something like that?
  
  **Tags:** plugin-development, entry, entry-type, php
  
  > **Comments:**
  > **username_0**: Anyone have any ideas??  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-11-23. Score: 4
  
  This question is old, but the solution is:
  
  ```
  // look up parent with a ElementCriteriaModel
  
  $parentCriteria = craft()->elements->getCriteria(ElementType::Entry);
  
  $parentCriteria->search  = 'slug:'.$parentSlug; // whatever you have
  
  $parent = $parentCriteria->find()[0];
  
  $entry = new EntryModel();
  $entry->sectionId = $section;
  
  $entry->parentId = $parent->id; 
  
  ...
  
  ```
  
  
  ---
  
  