現在の投稿と共有されているタグに基づいて関連性の高い順に8つの投稿を表示しようとしています。作成日...CakePHP - メインの投稿と日付と共有されたタグとの関連性によって投稿を注文しようとしています
モデル:
Tag habtm Post
Post habtm Tag
DB:コントローラのアクション内
posts(id, slug, ...)
tags(id, tag, ...)
posts_tags(post_id, tag_id)
:
$post = $this->Post->find('first', array('conditions' => array('slug' => $slug)));
$this->set('post', $post);
$tags = $post['Tag'];
$relOrd = '';
foreach($tags as $tag){
$tagId = $tag['id'];
$relOrd .= " + (CASE WHEN PostsTag.tag_id = ".$tagId." THEN 1 ELSE 0 END)";
}
$relOrd = '(' . substr($relOrd, 3) . ') AS Relevance';
$morePosts = $this->Post->find('all', array(
'joins' => array(
array(
'table' => 'posts_tags',
'alias' => 'PostsTag',
'type' => 'LEFT',
'conditions' => array(
'PostsTag.post_id = Post.id',
)
)
),
'group' => 'Post.id',
'fields' => array($relOrd, 'Post.*'),
'order' => array('Relevance' => 'DESC', 'Post.created' => 'DESC'),
'limit' => 8,
));
$this->log($morePosts);
$this->set('morePosts', $morePosts);
関連性の値は、各投稿に1つのタグ(0または1のみ)があるかのように扱われていますが、ほとんど動作しています。したがって、各タグの関連性値は、すべてのタグに基づいて累積されるのではなく、LASTタグの投稿に応じて0または1のいずれかをとっているようです。