cviebrock/eloquent-taggable

Count of tagged items is incorrect

marketingsignals opened this issue · 2 comments

The SQL generated within TagService::getPopularTags compiles as this:

(If you do not limit by tag entity type and use 0 for both the limit and minCount)

SELECT t.*, COUNT(t.tag_id) AS taggable_count FROM taggable_tags t LEFT JOIN taggable_taggables tt ON tt.tag_id=t.tag_id GROUP BY t.tag_id, t.name, t.normalized, t.created_at, t.updated_at ORDER BY taggable_count DESC

If you have a tag that is not used by any taggable, then you get a count of 1 rather than 0.

Should it not be updated to:

SELECT t.*, COUNT(tt.tag_id) AS taggable_count FROM taggable_tags t LEFT JOIN taggable_taggables tt ON tt.tag_id=t.tag_id GROUP BY t.tag_id, t.name, t.normalized, t.created_at, t.updated_at ORDER BY taggable_count DESC

This means you get the correct value of 0 if you get all tags and some of them don't have a tagged relation.

@marketingsignals hmmm ... you may be right. To confirm, can you make a PR with a failing test?

Closing due to no response. Feel free to re-open or comment if you are still having issues.