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.