Answers.Body: <p>The Tags field <code>classifiedTags</code> needs an array of tag IDs, not slugs/titles. Also, if this is an existing entry and you want to retain tags already added to the field, you'll need to pull the existing tag IDs using <code>$entry-&gt;classifiedTags-&gt;ids()</code> and merge with those.</p>
  
  <pre><code>$tag = craft()-&gt;elements-&gt;getCriteria(ElementType::Tag, [
      'groupId' =&gt; 1,
      'slug' =&gt; 'tagtest',
  ])-&gt;first();
  if ($tag) {
    $existingTags = $entry-&gt;classifiedTags-&gt;ids();
    $entry-&gt;getContent()-&gt;setAttribute('classifiedTags', array_merge($existingTags, [$tag-&gt;id]));
  }
  </code></pre>
  
  <p>Note that if the tag doesn't already exist, you'll need to create it using the <a href="https://craftcms.com/classreference/services/TagsService">TagsService</a>:</p>
  
  <pre><code>$tag-&gt;groupId = 1;
  $tag-&gt;getContent()-&gt;title = 'tagtest';
  $success = craft()-&gt;tags-&gt;saveTag($tag);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-15T07:03:24.067Z
Answers.LastActivityDate: 2016-07-15T07:25:32.377Z
Answers.LastEditDate: 2016-07-15T07:25:32.377Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>I am updating an entry using the entries service, I want to add a tag value into a tag field type (classifiedTags).</p>
  
  <pre><code>$entry = craft()-&gt;entries-&gt;getEntryById(120);
  $entry-&gt;getContent()-&gt;classifiedTags = 'tagtest';
  $entry-&gt;title = 'Updating title to this';
  $entry-&gt;getContent()-&gt;description = 'this is the new description';
  craft()-&gt;entries-&gt;saveEntry($entry);
  </code></pre>
  
  <p>It runs and updates everything except the tag field (classifiedTags).</p>
  
  <p>If I change it from a string to array like below:</p>
  
  <pre><code>$entry-&gt;getContent()-&gt;classifiedTags = array('tagtest');
  </code></pre>
  
  <p>It gives me this error:</p>
  
  <blockquote>
    <p>CDbCommand failed to execute the SQL statement: SQLSTATE[23000]:
    Integrity constraint violation: 1452 Cannot add or update a child row:
    a foreign key constraint fails (<code>ggmg_craft</code>.<code>craft_relations</code>,
    CONSTRAINT <code>craft_relations_targetId_fk</code> FOREIGN KEY (<code>targetId</code>)
    REFERENCES <code>craft_elements</code> (<code>id</code>) ON DELETE CASCADE). The SQL
    statement executed was: INSERT INTO <code>craft_relations</code> (<code>fieldId</code>,
    <code>sourceId</code>, <code>sourceLocale</code>, <code>targetId</code>, <code>sortOrder</code>, <code>dateCreated</code>,
    <code>dateUpdated</code>, <code>uid</code>) VALUES (:row0_col0, :row0_col1, NULL,
    :row0_col3, :row0_col4, :row0_col5, :row0_col6, :row0_col7). Bound
    with :row0_col0='43', :row0_col1='31823', :row0_col3='tagtest',
    :row0_col4=1, :row0_col5='2016-07-14 21:16:04', :row0_col6='2016-07-14
    21:16:04', :row0_col7='d2228ed9-cc60-478d-929c-98efd776b63c'</p>
  </blockquote>
  
  <p>Which leads me to believe I am getting warmer.</p>
  
  <p>Craft CMS v 2.6.2796
  PHP v 5.6.13</p>
  
Tags: <plugin-development><entries><tags><fieldtypes><elementtype>
Title: How do I add a value to a tag field type using the entries service saveEntry method?
ThreadText: # How do I add a value to a tag field type using the entries service saveEntry method?
  
  (Asked by: username_0 on 2016-07-14)
  
  I am updating an entry using the entries service, I want to add a tag value into a tag field type (classifiedTags).
  
  ```
  $entry = craft()->entries->getEntryById(120);
  $entry->getContent()->classifiedTags = 'tagtest';
  $entry->title = 'Updating title to this';
  $entry->getContent()->description = 'this is the new description';
  craft()->entries->saveEntry($entry);
  
  ```
  
  It runs and updates everything except the tag field (classifiedTags).
  
  If I change it from a string to array like below:
  
  ```
  $entry->getContent()->classifiedTags = array('tagtest');
  
  ```
  
  It gives me this error:
  
  > CDbCommand failed to execute the SQL statement: SQLSTATE[23000]:
  > Integrity constraint violation: 1452 Cannot add or update a child row:
  > a foreign key constraint fails (`ggmg_craft`.`craft_relations`,
  > CONSTRAINT `craft_relations_targetId_fk` FOREIGN KEY (`targetId`)
  > REFERENCES `craft_elements` (`id`) ON DELETE CASCADE). The SQL
  > statement executed was: INSERT INTO `craft_relations` (`fieldId`,
  > `sourceId`, `sourceLocale`, `targetId`, `sortOrder`, `dateCreated`,
  > `dateUpdated`, `uid`) VALUES (:row0\_col0, :row0\_col1, NULL,
  > :row0\_col3, :row0\_col4, :row0\_col5, :row0\_col6, :row0\_col7). Bound
  > with :row0\_col0='43', :row0\_col1='31823', :row0\_col3='tagtest',
  > :row0\_col4=1, :row0\_col5='2016-07-14 21:16:04', :row0\_col6='2016-07-14
  > 21:16:04', :row0\_col7='d2228ed9-cc60-478d-929c-98efd776b63c'
  
  Which leads me to believe I am getting warmer.
  
  Craft CMS v 2.6.2796
  PHP v 5.6.13
  
  **Tags:** plugin-development, entries, tags, fieldtypes, elementtype
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-07-15. Score: 6 | Accepted answer
  
  The Tags field `classifiedTags` needs an array of tag IDs, not slugs/titles. Also, if this is an existing entry and you want to retain tags already added to the field, you'll need to pull the existing tag IDs using `$entry->classifiedTags->ids()` and merge with those.
  
  ```
  $tag = craft()->elements->getCriteria(ElementType::Tag, [
      'groupId' => 1,
      'slug' => 'tagtest',
  ])->first();
  if ($tag) {
    $existingTags = $entry->classifiedTags->ids();
    $entry->getContent()->setAttribute('classifiedTags', array_merge($existingTags, [$tag->id]));
  }
  
  ```
  
  Note that if the tag doesn't already exist, you'll need to create it using the [TagsService](https://craftcms.com/classreference/services/TagsService):
  
  ```
  $tag->groupId = 1;
  $tag->getContent()->title = 'tagtest';
  $success = craft()->tags->saveTag($tag);
  
  ```
  
  
  ---
  
  