Answers.Body: <p>It might not be implemented completely yet. This bug report is slightly different (join from tag to contact) but suggests it's maybe not complete: <a href="https://github.com/civicrm/org.civicrm.api4/issues/128" rel="nofollow noreferrer">https://github.com/civicrm/org.civicrm.api4/issues/128</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-20T22:43:01.440Z
Answers.Text: ah ha!  That's a good catch, thank you.  I should always, always check the github issues.  For now, I'll use the API3 version and write a note about my particular case on that github ticket.
Answers.UserDisplayName: Sean Võ Kirkpatrick
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-20T21:15:07.580Z
Answers.LastActivityDate: 2019-11-20T21:15:07.580Z
Answers.UserDisplayName: Demerit
Body: <p>I am creating some custom directory screens to display a table of contacts with associated email addresses and any tags (just a simplified directory for non-admin staff to view/sort basic CRM data).  I am using the API version 4 and can't figure out the correct way to grab both the contact AND related tag data.</p>
  
  <p>When using the API4 query builder, only the "tags" table (Join to Tag) is present, and adding "tags.name" to the query does not retrieve anything:</p>
  
  <pre><code>$contacts = \Civi\Api4\Contact::get()
    -&gt;setSelect([
      'id', 
      'first_name', 
      'last_name', 
      'job_title', 
      'display_name', 
      'tags.name',
    ])
    -&gt;addWhere('id', '=', 865)
    -&gt;setLimit(25)
    -&gt;execute();
  </code></pre>
  
  <p>yields (though this user definitely has a tag):</p>
  
  <pre><code>[
    {
      "id": "865",
      "first_name": "Sean",
      "last_name": "Kirkpatrick",
      "job_title": null,
      "display_name": "Sean Kirkpatrick",
      "tags": []
    }
  ]
  </code></pre>
  
  <p>This seems to be pulling info just from the table civicrm_tag instead of both civicrm_tag and civicrm_entity_tag.  In SQL, I can get the data I want with this query:</p>
  
  <pre><code>SELECT b.id, b.display_name, c.name
  FROM civicrm_entity_tag a 
    join civicrm_contact b on a.entity_id = b.id
    join civicrm_tag c on a.tag_id = c.id
  </code></pre>
  
  <p>In API3, getting this data is fairly simple:</p>
  
  <pre><code>$result = civicrm_api3('Contact', 'get', [
    'sequential' =&gt; 1,
    'return' =&gt; ["id", "first_name", "last_name", "tag"],
  ]);
  </code></pre>
  
  <p>yields:</p>
  
  <pre><code>{
      "is_error": 0,
      "version": 3,
      "count": 25,
      "values": [
          {
              "contact_id": "1",
              "first_name": "",
              "last_name": "",
              "contact_is_deleted": "0",
              "tags": "",
              "id": "1"
          },
          {
              "contact_id": "2",
              "first_name": "Sean",
              "last_name": "Kirkpatrick",
              "contact_is_deleted": "0",
              "tags": "Individual Artist",
              "id": "2"
          },
          {
              "contact_id": "3",
              "first_name": "Test",
              "last_name": "Subject",
              "contact_is_deleted": "0",
              "tags": "Volunteer,Individual Artist,Newsletter",
              "id": "3"
          },...
  </code></pre>
  
  <p>Any ideas on what I'm doing wrong with API4?  I'd rather not have a random v3 call among all of the other v4 calls, but maybe that doesn't matter.</p>
  
Tags: <api><tags>
Title: Retrieve Contacts with Tags in API4
ThreadText: # Retrieve Contacts with Tags in API4
  
  (Asked by: username_0 on 2019-11-20)
  
  I am creating some custom directory screens to display a table of contacts with associated email addresses and any tags (just a simplified directory for non-admin staff to view/sort basic CRM data).  I am using the API version 4 and can't figure out the correct way to grab both the contact AND related tag data.
  When using the API4 query builder, only the "tags" table (Join to Tag) is present, and adding "tags.name" to the query does not retrieve anything:
  $contacts = \Civi\Api4\Contact::get()
    ->setSelect([
      'id', 
      'first_name', 
      'last_name', 
      'job_title', 
      'display_name', 
      'tags.name',
    ])
    ->addWhere('id', '=', 865)
    ->setLimit(25)
    ->execute();
  
  yields (though this user definitely has a tag):
  [
    {
      "id": "865",
      "first_name": "Sean",
      "last_name": "Kirkpatrick",
      "job_title": null,
      "display_name": "Sean Kirkpatrick",
      "tags": []
    }
  ]
  
  This seems to be pulling info just from the table civicrm_tag instead of both civicrm_tag and civicrm_entity_tag.  In SQL, I can get the data I want with this query:
  SELECT b.id, b.display_name, c.name
  FROM civicrm_entity_tag a 
    join civicrm_contact b on a.entity_id = b.id
    join civicrm_tag c on a.tag_id = c.id
  
  In API3, getting this data is fairly simple:
  $result = civicrm_api3('Contact', 'get', [
    'sequential' => 1,
    'return' => ["id", "first_name", "last_name", "tag"],
  ]);
  
  yields:
  {
      "is_error": 0,
      "version": 3,
      "count": 25,
      "values": [
          {
              "contact_id": "1",
              "first_name": "",
              "last_name": "",
              "contact_is_deleted": "0",
              "tags": "",
              "id": "1"
          },
          {
              "contact_id": "2",
              "first_name": "Sean",
              "last_name": "Kirkpatrick",
              "contact_is_deleted": "0",
              "tags": "Individual Artist",
              "id": "2"
          },
          {
              "contact_id": "3",
              "first_name": "Test",
              "last_name": "Subject",
              "contact_is_deleted": "0",
              "tags": "Volunteer,Individual Artist,Newsletter",
              "id": "3"
          },...
  
  Any ideas on what I'm doing wrong with API4?  I'd rather not have a random v3 call among all of the other v4 calls, but maybe that doesn't matter.
  
  **Tags:** api, tags
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-11-20. Score: 2 | Accepted answer
  
  It might not be implemented completely yet. This bug report is slightly different (join from tag to contact) but suggests it's maybe not complete: <https://github.com/civicrm/org.civicrm.api4/issues/128>
  
  > **Comments:**
  > **username_0**: ah ha!  That's a good catch, thank you.  I should always, always check the github issues.  For now, I'll use the API3 version and write a note about my particular case on that github ticket.  
  
  ---
  
  