Sunday, October 26, 2008

Re: Filter HABTM associations

On Oct 26, 1:45 am, lazyeye <lazyey...@gmail.com> wrote:
> 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 athttp://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM.
>
> This technique requires two "hasOne" relationships in the Post model,
one "hasOne" given your description. There's no need for 2 joins, if
you are going to be filtering by id. Use PostsTag.tag_id = $x;

where $x = an int, "this one"
or $x = an array or ids "these ones"

> 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".  

Add a DISTINCT to your fields array, and don't include any of
FilterTag (or, PostTag) in the fields you wnat returned.

AD
--~--~---------~--~----~------------~-------~--~----~
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: