Saturday, October 25, 2008

Filter HABTM associations

Here is my table setup:

posts
id
name
...

tags
id
parent_id
name
...

posts_tags
id
post_id
tag_id

I want to be able to filter posts by their associated "parent tag". So
for example I want to be able to request all posts with an associated
tag whose parent tag has an id of 1.

Currently I am able to filter posts by tag but not "parent tag" using
the technique described in the manual at http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM.

This technique requires two "hasOne" relationships in the Post model,
one for the "PostsTag" model and one for the "Tag" model under an
alias of "FilterTag" with no foreign key and a condition of
FilterTag.id = QuestionsTag.tag_id. You can then filter by tag by
specifying a FilterTag.id in your search conditions.

The problem with this technique is that if a post has more than one
tag with the same parent_id that posts will show up in the results
twice.

Besides removing duplicate posts after the request using PHP is there
any way to filter by "parent tag". This problem has been bugging me
for a couple of days now and I would greatly appreciate any help in
this matter.

Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: