<p>You can also do it like this. This way is especially useful when creating a new entry with a required relationship field:</p>

<pre><code>    public function createWorldConfig(UserModel $user, EntryModel $world)
    {
        $section    = craft()-&gt;sections-&gt;getSectionByHandle('yourSectionName');
        $entryTypes = craft()-&gt;sections-&gt;getEntryTypesByHandle('yourEntryTypeName');
        $entryType  = reset($entryTypes);

        $entry            = new EntryModel();
        $entry-&gt;sectionId = $section-&gt;id;
        $entry-&gt;typeId    = $entryType-&gt;id;
        $entry-&gt;authorId  = $user-&gt;id;
        $entry-&gt;postDate  = date('Y-m-d');

        // Build the relationship using and ID array.
        $entry-&gt;content-&gt;relatedEntryField = array($anotherEntry-&gt;id);

        $wasSaved = craft()-&gt;entries-&gt;saveEntry($entry);

        if (!$wasSaved) {
            return null;
        }

        return $entry;
    }
</code></pre>

CC BY-SA 3.0
2016-01-31T19:08:25.500Z
2016-01-31T19:08:25.500Z
Jay Haase
<p>Figured it out, trying to use the Entry as the source was wrong as the 'stores' relationship field actually belongs to the matrix field.</p>

<p>So by setting the matrix field as the source this is now working just fine!</p>

<pre><code>craft()-&gt;relations-&gt;saveRelations($field , $matrixBlock, $relations);
</code></pre>

CC BY-SA 3.0
2015-05-13T12:51:49.170Z
2015-05-13T12:51:49.170Z
Alec Ritson
<p>I was hoping someone could shed some light on this issue I'm having. I'm trying to update a relationship field through a plugin, without much success, the entry looks like this:</p>

<p><img src="https://i.sstatic.net/tkV0g.jpg" alt="Entry page"></p>

<p>I saw a post on here regarding something similar here:  <a href="https://craftcms.stackexchange.com/questions/4962/relation-row-insertion-in-db-plugin">Relation row insertion in DB (plugin)</a> so going from that my code at the moment looks like this:</p>

<pre><code>// The id of the entry i want to attach to the relation
$storeId = $formData['storeId'];

// The entry as shown above
$source = craft()-&gt;elements-&gt;getElementById($entryId);

foreach($blocks as $blockId =&gt; $value)
{
 // The matrix block with the relation
 $matrixBlock = craft()-&gt;matrix-&gt;getBlockById($blockId);

 // Get the 'stores' field
 $fieldId = $matrixBlock-&gt;stores-&gt;relatedTo['field'];
 $field = craft()-&gt;fields-&gt;getFieldById($fieldId);

 // Save the relationship
 craft()-&gt;relations-&gt;saveRelations($field , $source, array($storeId));

}
</code></pre>

<p><code>$blocks</code> is just an array like: <code>array('1092' =&gt; 0)</code> and <code>$value</code> will just be determining whether to set or unset the relation.</p>

<p>When I run the above through a controller, no errors appear but no relation actually takes place on the <code>stores</code> field. Are there any glaring mistakes i'm making? any help would be much appreciated!</p>

<p>Thank you.</p>

<plugin-development><matrix><relations>
Inserting Relationship Into Entries Field - Plugin Not Saving Relation
# Inserting Relationship Into Entries Field - Plugin Not Saving Relation

(Asked by: username_0 on 2015-05-13)

I was hoping someone could shed some light on this issue I'm having. I'm trying to update a relationship field through a plugin, without much success, the entry looks like this:

I saw a post on here regarding something similar here:  Relation row insertion in DB (plugin) so going from that my code at the moment looks like this:
// The id of the entry i want to attach to the relation
$storeId = $formData['storeId'];

// The entry as shown above
$source = craft()->elements->getElementById($entryId);

foreach($blocks as $blockId => $value)
{
 // The matrix block with the relation
 $matrixBlock = craft()->matrix->getBlockById($blockId);

 // Get the 'stores' field
 $fieldId = $matrixBlock->stores->relatedTo['field'];
 $field = craft()->fields->getFieldById($fieldId);

 // Save the relationship
 craft()->relations->saveRelations($field , $source, array($storeId));

}

$blocks is just an array like: array('1092' => 0) and $value will just be determining whether to set or unset the relation.
When I run the above through a controller, no errors appear but no relation actually takes place on the stores field. Are there any glaring mistakes i'm making? any help would be much appreciated!
Thank you.

**Tags:** plugin-development, matrix, relations

---

## 2 answers

### Answer by username_0 on 2015-05-13. Score: 7 | Accepted answer

Figured it out, trying to use the Entry as the source was wrong as the 'stores' relationship field actually belongs to the matrix field.
So by setting the matrix field as the source this is now working just fine!
craft()->relations->saveRelations($field , $matrixBlock, $relations);


---

### Answer by username_1 on 2016-01-31. Score: 3

You can also do it like this. This way is especially useful when creating a new entry with a required relationship field:
    public function createWorldConfig(UserModel $user, EntryModel $world)
    {
        $section    = craft()->sections->getSectionByHandle('yourSectionName');
        $entryTypes = craft()->sections->getEntryTypesByHandle('yourEntryTypeName');
        $entryType  = reset($entryTypes);

        $entry            = new EntryModel();
        $entry->sectionId = $section->id;
        $entry->typeId    = $entryType->id;
        $entry->authorId  = $user->id;
        $entry->postDate  = date('Y-m-d');

        // Build the relationship using and ID array.
        $entry->content->relatedEntryField = array($anotherEntry->id);

        $wasSaved = craft()->entries->saveEntry($entry);

        if (!$wasSaved) {
            return null;
        }

        return $entry;
    }


---

