Answers.Body: <p>A Tag field is like every other relation field. Just insert an array of ids in the field and save the element</p>
  
  <pre><code>$element-&gt;setFieldValue('fieldHandle', [1,2,3,4,5,6]);
  Craft::$app-&gt;getElements()-&gt;saveElement($element);
  </code></pre>
  
  <p>In order to get the current ids and add one, just do</p>
  
  <pre><code>$ids = $element-&gt;getFieldValue('fieldHandle')-&gt;ids();
  $ids[] = 'yourNewTagId';
  $element-&gt;setFieldValue('fieldHandle', $ids);
  Craft::$app-&gt;getElements()-&gt;saveElement($element);
  </code></pre>
  
  <p>To Create a new Tag do</p>
  
  <pre><code>$tag = new Tag();
  $tag-&gt;title = "your new tag name";
  $tag-&gt;groupId = 1;
  Craft::$app-&gt;getElements()-&gt;saveElement($tag);
  
  // and add it to the element
  $element = Craft::$app-&gt;getElements()-&gt;getElementById(2);
  $ids = $element-&gt;getFieldValue('fieldHandle')-&gt;ids();
  $ids[] = $tag-&gt;id;
  $element-&gt;setFieldValue('fieldHandle', $ids);
  Craft::$app-&gt;getElements()-&gt;saveElement($element);
  </code></pre>
  
  <p>Edit: </p>
  
  <p>Note that I'm using the <code>setFieldValue</code> directly instead of the magic <code>__get</code> and <code>__set</code> methods for a better performance.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-06T22:22:48.500Z
Answers.Text: Strange, we have been tried this, and it was not working, Will doing the troubleshooting again, thx, will come back
Answers.UserDisplayName: Visky Máté
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-06T22:41:47.293Z
Answers.Text: What does not work? You know you can receive all errors with `$element->getErrors()` in order to see what is not correct
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-06T22:57:14.943Z
Answers.Text: We are trying to save the asset on the SAVE_ON event. But, even if we are not doing anything just simply using: Craft::$app->getElements()->saveElement($asset); it is going for a timeout, and we have to restart the webserver
  If we are trying to save the Entry it self, it is working like charm, but in case of the SAVE_ON event it is failing... (we would like to do the auto tagging at the SAVE_ON after the SAVE_ON event)
Answers.UserDisplayName: Visky Máté
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-06T23:05:47.633Z
Answers.Text: Well of course. The `saveElement` function triggers the `OnBeforeSave` event, and you call the `saveElement`..... That triggers the event again.... You see? You are producing an endless loop. You don't need to call the `saveElement` function since this function is going to run when you use the `BeforeElementSave` event
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-07T00:15:18.413Z
Answers.Text: Gosh... you right... thank you!!! We are checking it!
Answers.UserDisplayName: Visky Máté
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-07T00:52:45.453Z
Answers.Text: Anyhow, we need a workaround for this, because we are going to know the asset, only after the upload. So we are going to save the success date on a field, and avoid the infinite loop. but thx for the help!
Answers.UserDisplayName: Visky Máté
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-07T04:09:33.810Z
Answers.Text: I'm still not sure why you don't use the `BeforeElementSave` event and set your values there. The asset is already populated and should contain all the values you need. You can tag it at this moment. Another way is to check if the asset is new if it only should run once
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-07T04:12:32.810Z
Answers.Text: Because we are using cloudanary file system, with google auto tagging. So we have to wait back the results from the filesystem which is happening after the save not before
Answers.UserDisplayName: Visky Máté
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-07T04:18:23.673Z
Answers.Text: Maybe you should learn how to ask questions on this site. There is a help center too. Those information are necessary to offer proper advice. You'll receive more help when you do that. It's the same for all the other questions too
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-06T21:53:55.110Z
Answers.LastActivityDate: 2018-03-06T22:01:35.023Z
Answers.LastEditDate: 2018-03-06T22:01:35.023Z
Answers.UserDisplayName: Robin Schambach
Body: <p>We have to update the Asset after the SAVE_ON event, and the </p>
  
  <pre><code>Craft::$app-&gt;elements-&gt;saveElement($asset);
  </code></pre>
  
  <p>Not seems to be working (it is getting timed out). So what is the best pratice to update the Asset after the save, which event should we use for example auto-tagging?</p>
  
  <p>If we are trying to save the entry, it is working like charm, maybe we should query the asset parent first?</p>
  
  <p><strong>Update some additional findings:</strong>
  It's turned out it is not so easy to re-save the assets after the SAVE_ON. We need to add additional parameters - based on the class rules:</p>
  
  <pre><code>$rules[] = [['newLocation'], 'required', 'on' =&gt; [self::SCENARIO_CREATE, self::SCENARIO_FILEOPS]];
  $rules[] = [['tempFilePath'], 'required', 'on' =&gt; [self::SCENARIO_CREATE, self::SCENARIO_REPLACE]];
  </code></pre>
  
  <p>Obviously the goal is to just add some tags/category to the image (eg: getting back the tags data from cloudanary auto tagging interface via the filesystem, so we are not able to use the before save event).
  So if we are simply trying to save the <code>$asset</code> it is not going to work, because we do not have newLocation either the tempFilePath is old as well. Instead we have to change the scenario, and save the asset after that (the idea was coming after checking the <code>ElementController</code>)</p>
  
  <pre><code>$element-&gt;setScenario(Element::SCENARIO_LIVE);
  $element-&gt;setFieldValue('fieldHandle', $ids);
  Craft::$app-&gt;getElements()-&gt;saveElement($element);
  </code></pre>
  
  <p>I do not have better idea for Asset save (specially because the asset itself was saved, and only the fields has to be updated), but this solution is working like charm, and the same logic is applied on the CP. If only the fields are getting updated the <code>ElementController</code> is getting called otherwise the <code>AssetController</code>. Hopes it helps for future reference</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-03-11T21:03:27.993Z
Comments.Text: Looking back at the revision history of this question, it appears that you drastically changed the scope and meaning of this question on [Mar 6 at 23:02](https://craftcms.stackexchange.com/posts/24099/revisions). Please do not edit a question so drastically once it's been posted, as it may invalidate existing answers. If you need to ask a new question based on recent information, please [post an entirely new question](https://craftcms.stackexchange.com/questions/ask). You can link back to your previous question for reference.
Comments.UserDisplayName: Lindsey D
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-03-12T17:35:23.297Z
Comments.Text: Noted, but it was the same issue. Only our current understanding along craft has been changed and notnthe problem
Comments.UserDisplayName: Visky Máté
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-03-13T18:55:52.550Z
Comments.Text: Understood. The revision was still significant enough to change the meaning of the question. In the future, please consider opening a new question based on learned information.
Comments.UserDisplayName: Lindsey D
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-03-14T19:41:29.903Z
Comments.Text: As i said noted and i do my best next time! Appreciate your feedback!
Comments.UserDisplayName: Visky Máté
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-03-14T19:41:55.587Z
Comments.Text: Great, thank you!
Comments.UserDisplayName: Lindsey D
Tags: <assets><entry><tags><element>
Title: How can I add programmatically a Tag after SAVE_ON event in case of Assets types
ThreadText: # How can I add programmatically a Tag after SAVE_ON event in case of Assets types
  
  (Asked by: username_0 on 2018-03-06)
  
  We have to update the Asset after the SAVE\_ON event, and the
  
  ```
  Craft::$app->elements->saveElement($asset);
  
  ```
  
  Not seems to be working (it is getting timed out). So what is the best pratice to update the Asset after the save, which event should we use for example auto-tagging?
  
  If we are trying to save the entry, it is working like charm, maybe we should query the asset parent first?
  
  **Update some additional findings:**
  It's turned out it is not so easy to re-save the assets after the SAVE\_ON. We need to add additional parameters - based on the class rules:
  
  ```
  $rules[] = [['newLocation'], 'required', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_FILEOPS]];
  $rules[] = [['tempFilePath'], 'required', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_REPLACE]];
  
  ```
  
  Obviously the goal is to just add some tags/category to the image (eg: getting back the tags data from cloudanary auto tagging interface via the filesystem, so we are not able to use the before save event).
  So if we are simply trying to save the `$asset` it is not going to work, because we do not have newLocation either the tempFilePath is old as well. Instead we have to change the scenario, and save the asset after that (the idea was coming after checking the `ElementController`)
  
  ```
  $element->setScenario(Element::SCENARIO_LIVE);
  $element->setFieldValue('fieldHandle', $ids);
  Craft::$app->getElements()->saveElement($element);
  
  ```
  
  I do not have better idea for Asset save (specially because the asset itself was saved, and only the fields has to be updated), but this solution is working like charm, and the same logic is applied on the CP. If only the fields are getting updated the `ElementController` is getting called otherwise the `AssetController`. Hopes it helps for future reference
  
  **Tags:** assets, entry, tags, element
  
  > **Comments:**
  > **username_1**: Looking back at the revision history of this question, it appears that you drastically changed the scope and meaning of this question on [Mar 6 at 23:02](https://craftcms.stackexchange.com/posts/24099/revisions). Please do not edit a question so drastically once it's been posted, as it may invalidate existing answers. If you need to ask a new question based on recent information, please [post an entirely new question](https://craftcms.stackexchange.com/questions/ask). You can link back to your previous question for reference.  
  > **username_0**: Noted, but it was the same issue. Only our current understanding along craft has been changed and notnthe problem  
  > **username_1**: Understood. The revision was still significant enough to change the meaning of the question. In the future, please consider opening a new question based on learned information.  
  > **username_0**: As i said noted and i do my best next time! Appreciate your feedback!  
  > **username_1**: Great, thank you!  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2018-03-06. Score: 3 | Accepted answer
  
  A Tag field is like every other relation field. Just insert an array of ids in the field and save the element
  
  ```
  $element->setFieldValue('fieldHandle', [1,2,3,4,5,6]);
  Craft::$app->getElements()->saveElement($element);
  
  ```
  
  In order to get the current ids and add one, just do
  
  ```
  $ids = $element->getFieldValue('fieldHandle')->ids();
  $ids[] = 'yourNewTagId';
  $element->setFieldValue('fieldHandle', $ids);
  Craft::$app->getElements()->saveElement($element);
  
  ```
  
  To Create a new Tag do
  
  ```
  $tag = new Tag();
  $tag->title = "your new tag name";
  $tag->groupId = 1;
  Craft::$app->getElements()->saveElement($tag);
  
  // and add it to the element
  $element = Craft::$app->getElements()->getElementById(2);
  $ids = $element->getFieldValue('fieldHandle')->ids();
  $ids[] = $tag->id;
  $element->setFieldValue('fieldHandle', $ids);
  Craft::$app->getElements()->saveElement($element);
  
  ```
  
  Edit:
  
  Note that I'm using the `setFieldValue` directly instead of the magic `__get` and `__set` methods for a better performance.
  
  > **Comments:**
  > **username_0**: Strange, we have been tried this, and it was not working, Will doing the troubleshooting again, thx, will come back  
  > **username_2**: What does not work? You know you can receive all errors with `$element->getErrors()` in order to see what is not correct  
  > **username_0**: We are trying to save the asset on the SAVE_ON event. But, even if we are not doing anything just simply using: Craft::$app->getElements()->saveElement($asset); it is going for a timeout, and we have to restart the webserver If we are trying to save the Entry it self, it is working like charm, but in case of the SAVE_ON event it is failing... (we would like to do the auto tagging at the SAVE_ON after the SAVE_ON event)  
  > **username_2**: Well of course. The `saveElement` function triggers the `OnBeforeSave` event, and you call the `saveElement`..... That triggers the event again.... You see? You are producing an endless loop. You don't need to call the `saveElement` function since this function is going to run when you use the `BeforeElementSave` event  
  > **username_0**: Gosh... you right... thank you!!! We are checking it!  
  > **username_0**: Anyhow, we need a workaround for this, because we are going to know the asset, only after the upload. So we are going to save the success date on a field, and avoid the infinite loop. but thx for the help!  
  > **username_2**: I'm still not sure why you don't use the `BeforeElementSave` event and set your values there. The asset is already populated and should contain all the values you need. You can tag it at this moment. Another way is to check if the asset is new if it only should run once  
  > **username_0**: Because we are using cloudanary file system, with google auto tagging. So we have to wait back the results from the filesystem which is happening after the save not before  
  > **username_2**: Maybe you should learn how to ask questions on this site. There is a help center too. Those information are necessary to offer proper advice. You'll receive more help when you do that. It's the same for all the other questions too  
  
  ---
  
  