<p>You need to use <code>newParentId</code>:</p>
<pre><code>$navigationItem = new Entry();
$navigationItem-&gt;typeId = 4;
$navigationItem-&gt;sectionId = $navigationSection-&gt;id;
$navigationItem-&gt;title = $entry-&gt;title;
$navigationItem-&gt;newParentId = $herbsNavigationEntry-&gt;id;

$navigationItem-&gt;setFieldValue('navLink', '/tags/' . $entry-&gt;slug);

if (!Craft::$app-&gt;elements-&gt;saveElement($navigationItem)) {
    Craft::error('Couldn’t save the entry &quot;' . $entry-&gt;title . '&quot;', __METHOD__);
}
</code></pre>

CC BY-SA 4.0
2020-09-26T17:22:27.600Z
That works fine. Anyway PHPStorm tells me: Field 'newParentId' is marked as @internal. 
But it works. Thank you.
Slowwie
CC BY-SA 4.0
2023-04-26T23:15:15.667Z
Should also mention `newParentId` was removed in Craft 4, so use `$navigationItem->setParentId($herbsNavigationEntry->id);`
crawf
CC BY-SA 4.0
2020-09-26T17:16:34.903Z
2020-09-26T17:16:34.903Z
Oli
<p>In this code I try to create a new Entry and set a Parent to it.
The sectionId and the typeId are actually the same.</p>
<pre><code>$navigationItem = new Entry();
$navigationItem-&gt;typeId = 4;
$navigationItem-&gt;sectionId = $navigationSection-&gt;id;
$navigationItem-&gt;title = $entry-&gt;title;
$navigationItem-&gt;setParent($herbsNavigationEntry);

$navigationItem-&gt;setFieldValue('navLink', '/tags/' . $entry-&gt;slug);

if (!Craft::$app-&gt;elements-&gt;saveElement($navigationItem)) {
    Craft::error('Couldn’t save the entry &quot;' . $entry-&gt;title . '&quot;', __METHOD__);
}
</code></pre>
<p>Before saving I get this:</p>
<p><a href="https://i.sstatic.net/x82w7.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/x82w7.png" alt="enter image description here" /></a></p>
<p>But after the <code>Craft::$app-&gt;elements-&gt;saveElement($navigationItem)</code>,
the parent disappears.</p>
<p>What did I wrong?</p>

<entries><structure><events><saveentry><parents>
Parent are not set anymore after saveElement()
# Parent are not set anymore after saveElement()

(Asked by: username_0 on 2020-09-26)

In this code I try to create a new Entry and set a Parent to it.
The sectionId and the typeId are actually the same.
$navigationItem = new Entry();
$navigationItem->typeId = 4;
$navigationItem->sectionId = $navigationSection->id;
$navigationItem->title = $entry->title;
$navigationItem->setParent($herbsNavigationEntry);

$navigationItem->setFieldValue('navLink', '/tags/' . $entry->slug);

if (!Craft::$app->elements->saveElement($navigationItem)) {
    Craft::error('Couldn’t save the entry "' . $entry->title . '"', __METHOD__);
}

Before saving I get this:

But after the Craft::$app->elements->saveElement($navigationItem),
the parent disappears.
What did I wrong?

**Tags:** entries, structure, events, saveentry, parents


---

## 1 answers

### Answer by username_1 on 2020-09-26. Score: 3 | Accepted answer

You need to use newParentId:
$navigationItem = new Entry();
$navigationItem->typeId = 4;
$navigationItem->sectionId = $navigationSection->id;
$navigationItem->title = $entry->title;
$navigationItem->newParentId = $herbsNavigationEntry->id;

$navigationItem->setFieldValue('navLink', '/tags/' . $entry->slug);

if (!Craft::$app->elements->saveElement($navigationItem)) {
    Craft::error('Couldn’t save the entry "' . $entry->title . '"', __METHOD__);
}

> **Comments:**
> **username_0**: That works fine. Anyway PHPStorm tells me: Field 'newParentId' is marked as @internal.  But it works. Thank you.  
> **username_2**: Should also mention `newParentId` was removed in Craft 4, so use `$navigationItem->setParentId($herbsNavigationEntry->id);`  

---

