<p>The trick here is that Tags are also elements, so you'll need to save them or look them up to assign them. Something like this:</p>

<pre><code>    public function createOrGetTag($keyword, $groupId) {
        $tagQuery = new TagQuery(Tag::class);
        $tagQuery-&gt;groupId = $groupId;
        $tagQuery-&gt;title = $keyword;
        $tag = $tagQuery-&gt;one();

        if (!$tag) {
            $tag = new Tag();
            $tag-&gt;groupId = $groupId;
            $tag-&gt;title = $keyword;
            Craft::$app-&gt;elements-&gt;saveElement($tag);
        }

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

<p>And then in your field setting, you'd do something like this:</p>

<pre><code>$product-&gt;businessTag-&gt;id[] = $tag-&gt;id;
</code></pre>

CC BY-SA 4.0
2019-02-01T16:50:54.647Z
Sorry about all the comments and deleted comments. I had done something wrong and messed up the code when I put it in and it didn't work, then found a solution I thought worked but only made a mess. I still am though, stuck on the last line `$product->businessTag->id[] = $tag->id;` it successfully updates the product with the tag but it overwrites all other tags.
Darryl Hardin
CC BY-SA 4.0
2019-02-01T12:55:43.853Z
2019-02-06T22:30:52.207Z
2019-02-06T22:30:52.207Z
Ransom Roberson
<p>I had an issue with Ransom Roberson code <code>$product-&gt;businessTag-&gt;id[] = $tag-&gt;id;</code> but otherwise everything else worked. The problem is the snippet above will update the tag and then overwrite every tag the entry/product has. So I wrote the code below. Ransom's code works perfectly aside from the last part, so putting this with it should help anyone in the future who is might struggle with this.</p>

<pre><code>$existingTags = $product-&gt;businessTag-&gt;ids();

    foreach ($existingTags as $eTag)
    {
        $product-&gt;businessTag-&gt;id[] = $eTag;
    }

    $product-&gt;businessTag-&gt;id[] = $tag-&gt;id
</code></pre>

CC BY-SA 4.0
2019-02-01T18:16:07.967Z
2019-02-01T18:16:07.967Z
Darryl Hardin
<p>I am working on a plugin and I have the following code</p>

<pre><code>private function _tagProducts(array $products, array $names)
{
    foreach ($products as $productId)
    {
        $product = Craft::$app-&gt;getElements()-&gt;getElementById($productId, Product::class, null);
        $product['businessTag'] = ['something'];
        $product['purchasedWithVoucher'] = 'no';
        Craft::$app-&gt;getElements()-&gt;saveElement($product);
    }
}
</code></pre>

<p>If I remove/comment out the line <code>$product['businessTag'] = ['something'];</code> this snippet runs just fine and updates the field <code>purchasedWithVoucher</code> just fine. But, if I keep the <code>businessTag</code> line in I get the following error.</p>

<p><code>(Integrity constraint violation – yii\db\IntegrityException)</code></p>

<p>I have tried updating the tag field as <code>$product['businessTag'] = 'something';</code> which at least throws no errors, but the product is not updated with the new tag, however the <code>$product['purchasedWithVoucher'];</code> line does update so I know that simply saving the changes isn't the issue.</p>

<plugin-development><craft3>
How can I save a tag value in an entry in Craft 3
# How can I save a tag value in an entry in Craft 3

(Asked by: username_0 on 2019-01-31)

I am working on a plugin and I have the following code
private function _tagProducts(array $products, array $names)
{
    foreach ($products as $productId)
    {
        $product = Craft::$app->getElements()->getElementById($productId, Product::class, null);
        $product['businessTag'] = ['something'];
        $product['purchasedWithVoucher'] = 'no';
        Craft::$app->getElements()->saveElement($product);
    }
}

If I remove/comment out the line $product['businessTag'] = ['something']; this snippet runs just fine and updates the field purchasedWithVoucher just fine. But, if I keep the businessTag line in I get the following error.
(Integrity constraint violation – yii\db\IntegrityException)
I have tried updating the tag field as $product['businessTag'] = 'something'; which at least throws no errors, but the product is not updated with the new tag, however the $product['purchasedWithVoucher']; line does update so I know that simply saving the changes isn't the issue.

**Tags:** plugin-development, craft3

---

## 2 answers

### Answer by username_1 on 2019-02-01. Score: 3 | Accepted answer

The trick here is that Tags are also elements, so you'll need to save them or look them up to assign them. Something like this:
    public function createOrGetTag($keyword, $groupId) {
        $tagQuery = new TagQuery(Tag::class);
        $tagQuery->groupId = $groupId;
        $tagQuery->title = $keyword;
        $tag = $tagQuery->one();

        if (!$tag) {
            $tag = new Tag();
            $tag->groupId = $groupId;
            $tag->title = $keyword;
            Craft::$app->elements->saveElement($tag);
        }

        return $tag;
    }

And then in your field setting, you'd do something like this:
$product->businessTag->id[] = $tag->id;

> **Comments:**
> **username_0**: Sorry about all the comments and deleted comments. I had done something wrong and messed up the code when I put it in and it didn't work, then found a solution I thought worked but only made a mess. I still am though, stuck on the last line `$product->businessTag->id[] = $tag->id;` it successfully updates the product with the tag but it overwrites all other tags.  

---

### Answer by username_0 on 2019-02-01. Score: 1

I had an issue with Ransom Roberson code $product->businessTag->id[] = $tag->id; but otherwise everything else worked. The problem is the snippet above will update the tag and then overwrite every tag the entry/product has. So I wrote the code below. Ransom's code works perfectly aside from the last part, so putting this with it should help anyone in the future who is might struggle with this.
$existingTags = $product->businessTag->ids();

    foreach ($existingTags as $eTag)
    {
        $product->businessTag->id[] = $eTag;
    }

    $product->businessTag->id[] = $tag->id


---

