我希望重新创建您通常在博客中看到的标签云边栏,用户在标签上选择该边栏,并显示具有该特定标签的所有帖子。
目前我无法确定如何设置查询。
我有查询来拉取所有标签:
$blogTags = $this->createQueryBuilder('b')
->select('b.tags')
->getQuery()
->getResult();
return $blogTags;
但是,如何将其设置为只提取用户从侧边栏中的一组标签中选择的标记的帖子?
我有代码存储标记并将其加权到侧边栏中,我正在寻找将标记链接到特定帖子的下一步。
getsTags()
public function getTags()
{
$blogTags = $this->createQueryBuilder('b')
->select('b.tags')
->getQuery()
->getResult();
$tags = array();
foreach ($blogTags as $blogTag) {
$tags = array_merge(explode(",", $blogTag['tags']), $tags);
}
foreach ($tags as $tag) {
$tag = trim($tag);
}
return $tags;
}
getTagWeights()
public function getTagWeights($tags)
{
$tagWeights = array();
if (empty($tags))
return $tagWeights;
foreach ($tags as $tag)
{
$tagWeights[$tag] = (isset($tagWeights[$tag])) ? $tagWeights[$tag] + 1 : 1;
}
// Shuffle the tags
uksort($tagWeights, function() {
return rand() > rand();
});
$max = max($tagWeights);
// Max of 5 weights
$multiplier = ($max > 5) ? 5 / $max : 1;
foreach ($tagWeights as &$tag)
{
$tag = ceil($tag * $multiplier);
}
return $tagWeights;
}
答案 0 :(得分:0)
与您的朋友(Symfony2 - Need help setting up a doctrine query for finding tags)
相同的答案您应该使用学说查询。
<强> PostRepository.php 强>
public function findByTagName($tagName)
{
$qb = $this->createQueryBuilder('post');
$qb->select('post')
->join('post.tags', 'tag')
->where('tag.name LIKE ?', '%'.$tagName.'%');
return $qb->getQuery()->getResult();
}
答案 1 :(得分:0)
希望这有助于其他人花费时间和小时。
public function getPostsByTags($tag)
{
$query = $this->createQueryBuilder('b')
->where('b.tags like :tag')
->setParameter('tag', '%'.$tag.'%');
return $query->getQuery()->getResult();
}