<p>So it seems what I was doing wrong was passing in a single $artistId rather than an array. With the change shown below it works as expected:</p>

<pre class="lang-php prettyprint-override"><code>foreach ($assets as $asset)
{
    $artistFieldModel = craft()-&gt;fields-&gt;getFieldByHandle('artist');
    craft()-&gt;relations-&gt;saveRelations($artistFieldModel, $asset, [$artistId]);
}
</code></pre>

CC BY-SA 3.0
2016-03-01T16:35:03.593Z
2016-03-01T16:35:03.593Z
Emyr Thomas
<p>I'm following the <a href="https://craftcms.com/docs/plugins/element-actions" rel="nofollow">Element Actions example</a> from the official Craft docs.</p>

<p>I've defined an Artist entry type, and have defined an entries field on my Assets to relate an Artist to each Asset. To avoid assigning Artists one-by-one, I'm attempting to create a Element Action so that I can select multiple Assets in the Asset Index and relate them to an Artist entry in bulk.</p>

<p>I'm almost there. I can select an Artist (entry) from a dropdown I've created on the Asset Index page in the CP, but I'm struggling to save the relationship in the performAction function within my plugin. I assume I should be using the relations service <a href="https://craftcms.com/classreference/services/RelationsService#saveRelations-detail" rel="nofollow">saveRelations()</a> method to accomplish this?</p>

<p>I'm new to Craft and not sure where I'm going wrong. I'm sure the fact that I'm doing this within an ElementAction is irrelevant, it's just that I'm doing something wrong while trying to save the relation. Here's the relevant part of the code as it currently stands:</p>

<pre class="lang-php prettyprint-override"><code>public function performAction(ElementCriteriaModel $criteria)
{
    // Get the selected artist
    $artistId = $this-&gt;getParams()-&gt;artist;
    $artist = craft()-&gt;entries-&gt;getEntryById($artistId);

    // Make sure it's a valid one
    if (!$artist)
    {
        $this-&gt;setMessage(Craft::t('The selected artist could not be found.'));
        return false;
    }

    // Assign the artist to the selected elements
    $assets = $criteria-&gt;find();

    foreach ($assets as $asset)
    {
        $artistFieldModel = craft()-&gt;fields-&gt;getFieldByHandle('artist');
        craft()-&gt;relations-&gt;saveRelations($artistFieldModel, $asset, $artistId);
    }

    // Success!
    $this-&gt;setMessage(Craft::t('Artist successfully assigned.'));
    return true;
}
</code></pre>

<p>Any pointers gratefully appreciated.</p>

<plugin-development><relations><entries-field><elementaction>
Relating entries to assets within an ElementAction
# Relating entries to assets within an ElementAction

(Asked by: username_0 on 2016-03-01)

I'm following the Element Actions example from the official Craft docs.
I've defined an Artist entry type, and have defined an entries field on my Assets to relate an Artist to each Asset. To avoid assigning Artists one-by-one, I'm attempting to create a Element Action so that I can select multiple Assets in the Asset Index and relate them to an Artist entry in bulk.
I'm almost there. I can select an Artist (entry) from a dropdown I've created on the Asset Index page in the CP, but I'm struggling to save the relationship in the performAction function within my plugin. I assume I should be using the relations service saveRelations() method to accomplish this?
I'm new to Craft and not sure where I'm going wrong. I'm sure the fact that I'm doing this within an ElementAction is irrelevant, it's just that I'm doing something wrong while trying to save the relation. Here's the relevant part of the code as it currently stands:
public function performAction(ElementCriteriaModel $criteria)
{
    // Get the selected artist
    $artistId = $this->getParams()->artist;
    $artist = craft()->entries->getEntryById($artistId);

    // Make sure it's a valid one
    if (!$artist)
    {
        $this->setMessage(Craft::t('The selected artist could not be found.'));
        return false;
    }

    // Assign the artist to the selected elements
    $assets = $criteria->find();

    foreach ($assets as $asset)
    {
        $artistFieldModel = craft()->fields->getFieldByHandle('artist');
        craft()->relations->saveRelations($artistFieldModel, $asset, $artistId);
    }

    // Success!
    $this->setMessage(Craft::t('Artist successfully assigned.'));
    return true;
}

Any pointers gratefully appreciated.

**Tags:** plugin-development, relations, entries-field, elementaction


---

## 1 answers

### Answer by username_0 on 2016-03-01. Score: 2 | Accepted answer

So it seems what I was doing wrong was passing in a single $artistId rather than an array. With the change shown below it works as expected:
foreach ($assets as $asset)
{
    $artistFieldModel = craft()->fields->getFieldByHandle('artist');
    craft()->relations->saveRelations($artistFieldModel, $asset, [$artistId]);
}


---

