繁体   English   中英

Symfony2-在侧栏中设置标签云

[英]Symfony2 - Setting up a tag cloud in the side bar

我希望重新创建通常在博客中看到的标签云侧边栏,在博客中用户在标签上进行选择,它会弹出具有该特定标签的所有帖子。

现在,我在确定如何设置查询时遇到了麻烦。

我有查询来拉所有标签:

$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;
}

与您的朋友相同的答案( Symfony2-需要帮助以建立用于查找标签的学说查询

您应该使用原则查询。

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();
}

希望这可以帮助其他人从此花费大量时间。

public function getPostsByTags($tag)
{
    $query = $this->createQueryBuilder('b')
        ->where('b.tags like :tag')
        ->setParameter('tag', '%'.$tag.'%');

    return $query->getQuery()->getResult();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM