Sunday, December 16, 2012

Need some HABTM help retrieving related posts

Okei, so I have a HABTM setup:
- posts
- tags
- posts_tags

When creating posts => select multiple tags: example: Tag1, Tag2, Tag3

Each selected tag is saved to posts_tags: id:1, post_id: 44, tag_id: 3  (and so on).

Now, displaying my posts, and listing the selected tags, is no problem. Everything is working flawlessly. 

However, I would like to have widget "You might also like these posts" => list 5 posts with similar content (using the same tags as the post beeing read).

So here I came across some difficulty finding similar posts according to selected tags.

Example: Is Post.id = 44  (beeing read) have Tag.id = 1, 3 ,4 assigned

How can I retrieve 5 other posts that have the same Tag.id assigned, and exclude the Post.id beeing read from the find??

I guess I will need to use some kind of join/grouping, but I am a total noob regarding those things, so any help on this will be GREATLY appreciated!!

Thanks for your time!

-Tom

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
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.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: