Post model:
public $hasMany = array(
'RelatedPost' => array(
'className' => 'Post',
'foreignKey' => false,
'limit' => 5,
'order' => array('RelatedPost.created' => 'DESC'),
'finderQuery' => 'SELECT RelatedPost.id, RelatedPost.title
FROM posts AS RelatedPost
INNER JOIN posts_tags AS PostTag
ON PostTag.post_id = RelatedPost.id
WHERE PostTag.tag_id IN
(SELECT tag_id FROM posts_tags WHERE post_id = {$__cakeID__$})
AND RelatedPost.id != {$__cakeID__$}'
)
);
public function fetch($id)
{
return $this->find(
'first',
array(
'conditions' => array(
$this->alias.'.id' => $id
)
)
);
}
Add RelatedPost.slug to the query if, like me, you prefer to pass
slugs. And change the fetch method, of course.
I included the order just to show that it can be.
On Sun, Dec 16, 2012 at 3:20 PM, MetZ <metz80@gmail.com> wrote:
> 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.
>
>
--
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.
Monday, December 17, 2012
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment