<p>Just to clear up a potential source of confusion: For Craft, the only 'real' parent-child relationships are the hierarchies in <a href="https://craftcms.com/docs/3.x/entries.html#structures" rel="nofollow noreferrer">Structures</a> (a type of <a href="https://craftcms.com/docs/3.x/entries.html#section-types" rel="nofollow noreferrer">Section</a>). <a href="https://craftcms.com/docs/3.x/entries-fields.html" rel="nofollow noreferrer">Entry fields</a> and by extension <a href="https://craftcms.com/docs/3.x/relations.html" rel="nofollow noreferrer">relations</a> (including the <code>relatedTo</code> parameter) are technically many-to-many relations (even if you allow only one related entry in your <code>recommendedSystem</code> field).</p>
<p>The <code>relatedTo</code> parameter only exists for queries, you can't use it on an <a href="https://docs.craftcms.com/api/v3/craft-elements-entry.html" rel="nofollow noreferrer">Entry model</a>.</p>
<p>Your second approach is the correct one, you can programmatically set field values using <a href="https://docs.craftcms.com/api/v3/craft-base-element.html#method-setfieldvalue" rel="nofollow noreferrer">craft\elements\Entry::setFieldValue</a>. For entries fields, you need to pass an array of IDs. Also, make sure to save the element using the <a href="https://docs.craftcms.com/api/v3/craft-services-elements.html#method-saveelement" rel="nofollow noreferrer">craft\services\Elements::saveElement</a> to persist your changes in the database.</p>
<pre class="lang-php prettyprint-override"><code>$model-&gt;setFieldValue('recommendedSystem', [$entry-&gt;id]);
Craft::$app-&gt;elements-&gt;saveElement($model);
</code></pre>
<p>If it's still not working: The <code>saveElement</code> method will run validation on the passed element by default and return a boolean to indicate success or failure. Afterwards, you can access any validation errors on the model using <code>$model-&gt;getErrors()</code>. If the field is not getting saved and you're not sure why, try dumping the output to find any errors that prevent the model from saving:</p>
<pre class="lang-php prettyprint-override"><code>$success = Craft::$app-&gt;elements-&gt;saveElement($model);
if (!$success) {
    Craft::dd($model-&gt;getErrors());
}
</code></pre>
<p>If you really want to, you can also just save the entry without validation like this:</p>
<pre class="lang-php prettyprint-override"><code>Craft::$app-&gt;elements-&gt;saveElement($model, false);
</code></pre>
<p>As a sidenote, if <code>$entry</code> and <code>$model</code> are the same entry, make sure to check the following checkbox in the settings of the <code>recommendedSystem</code> field: <strong>Allow self relations</strong><br />
Without this option, elements aren't allowed to relate to themselves, so trying to relate an element to itself through an entries field will silently fail.</p>

CC BY-SA 4.0
2021-10-25T13:19:37.897Z
What a nice explanation !
I tried to put my id as an ARRAY just like suggested and it WORKS !
So:
`$model->setFieldValue('recommendedSystem', [$entry->id]);` does the job.
Sylvain Tremblay
CC BY-SA 4.0
2021-10-25T12:48:40.130Z
2021-10-25T13:03:35.533Z
2021-10-25T13:03:35.533Z
MoritzLost
<p>I'm new to Craft but long time back-end php programmer. I'm trying to assign a parent-child (a ENTRY type to another ENTRY type) relationship in php based on some business rules.</p>
<p>I've got my child model, I can retrieve the parent as well, but can't manage to connect them. I set my relationship, so in the admin panel I can manage to do it manually.
<a href="https://i.sstatic.net/jh4iq.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/jh4iq.png" alt="enter image description here" /></a></p>
<p>In the code, I've got</p>
<pre><code>$entry = Entry::find()
         -&gt;section('systems')
         -&gt;where(['content.field_systemtype' =&gt; ['=', $systemRecommended]])
         -&gt;one();
error_log(print_r($entry-&gt;id,true)); // To show entry is found.`
</code></pre>
<p><strong>I've tried many things without success like:</strong></p>
<pre><code>// $model-&gt;recommendedSystem-&gt;relatedTo($entry);

// $model-&gt;setFieldValue('recommendedSystem', $entry-&gt;id);
</code></pre>
<p>And maybe a few others last week.</p>
<p>Any help would be appreciated</p>

<php><relatedto>
Set parent-child relationship in PHP
# Set parent-child relationship in PHP

(Asked by: username_0 on 2021-10-25)

I'm new to Craft but long time back-end php programmer. I'm trying to assign a parent-child (a ENTRY type to another ENTRY type) relationship in php based on some business rules.
I've got my child model, I can retrieve the parent as well, but can't manage to connect them. I set my relationship, so in the admin panel I can manage to do it manually.

In the code, I've got
$entry = Entry::find()
         ->section('systems')
         ->where(['content.field_systemtype' => ['=', $systemRecommended]])
         ->one();
error_log(print_r($entry->id,true)); // To show entry is found.`

I've tried many things without success like:
// $model->recommendedSystem->relatedTo($entry);

// $model->setFieldValue('recommendedSystem', $entry->id);

And maybe a few others last week.
Any help would be appreciated

**Tags:** php, relatedto

---

## 1 answers

### Answer by username_1 on 2021-10-25. Score: 4 | Accepted answer

Just to clear up a potential source of confusion: For Craft, the only 'real' parent-child relationships are the hierarchies in Structures (a type of Section). Entry fields and by extension relations (including the relatedTo parameter) are technically many-to-many relations (even if you allow only one related entry in your recommendedSystem field).
The relatedTo parameter only exists for queries, you can't use it on an Entry model.
Your second approach is the correct one, you can programmatically set field values using craft\elements\Entry::setFieldValue. For entries fields, you need to pass an array of IDs. Also, make sure to save the element using the craft\services\Elements::saveElement to persist your changes in the database.
$model->setFieldValue('recommendedSystem', [$entry->id]);
Craft::$app->elements->saveElement($model);

If it's still not working: The saveElement method will run validation on the passed element by default and return a boolean to indicate success or failure. Afterwards, you can access any validation errors on the model using $model->getErrors(). If the field is not getting saved and you're not sure why, try dumping the output to find any errors that prevent the model from saving:
$success = Craft::$app->elements->saveElement($model);
if (!$success) {
    Craft::dd($model->getErrors());
}

If you really want to, you can also just save the entry without validation like this:
Craft::$app->elements->saveElement($model, false);

As a sidenote, if $entry and $model are the same entry, make sure to check the following checkbox in the settings of the recommendedSystem field: Allow self relations
Without this option, elements aren't allowed to relate to themselves, so trying to relate an element to itself through an entries field will silently fail.

> **Comments:**
> **username_0**: What a nice explanation ! I tried to put my id as an ARRAY just like suggested and it WORKS ! So: `$model->setFieldValue('recommendedSystem', [$entry->id]);` does the job.  

---

