I have 3 entities Expertise, Person and Tag,
related through a join entity, AssociatedTag
which has a composite primary key (expertise_id, tag_id, person_id). This works correctly.
Because more than one Person can add the same Tag to a particular Expertise, simply displaying each tag string can result in duplicates. If I weren't working in Symfony, I'd just do an aggregate query similar to this:
SELECT
t.tag, a.tag_id
a.expertise_id,
COUNT(a.person_id) AS tagcount,
SUM(CASE
WHEN a.person_id = ? THEN 1
ELSE 0
END) AS has_curuser
FROM
expertise_tags a
JOIN
tag t ON (a.tag_id = t.id)
WHERE
a.expertise_id = ? #397
GROUP BY t.tag , a.expertise_id;
which would produce data like this:
foobe 9 397 1 0
neato 7 397 2 1
pita 8 397 1 0
and then output with a simple loop and conditions like has_curuser == TRUE
and tagcount > 1
to control the output options.
I get the impression with Symfony, that there ought to be a class for holding the aggregate results or perhaps there should be a data transformer or Twig extension that takes unaggregated results and aggregates them (though that seems inefficient).
What is the correct Symfony approach to this situation?