Symfony2 - 使用tagweights为热门标签设置标签云

时间:2014-05-13 22:52:46

标签: symfony tags entity-relationship tag-cloud

我正在尝试设置加权标签云,当标签是博客实体中的字符串属性时,该标签云有效。

现在我将标签设置为自己的实体,并与博客相关,作为双向ManyToMany / ManyToMany关系。

毋庸置疑,它不起作用,我猜它是因为标签现在是它自己的对象。

我的问题:由于标签是自己的实体而不是来自博客实体的字符串属性,我在这里做错了什么?

当我从下面的控制器转储$ tagWeights时,我会看到类似这样的内容时出错:

array (size=78)

'Tag1' => float 1

'Tag2' => float 5

'Tag3' => float 2

错误:(这一行:

foreach ($tags as $tag) {

$tagWeights[$tag] = (isset($tagWeights[$tag])) ? $tagWeights[$tag] + 1 : 1;

}

ContextErrorException:警告:isvar中的非法偏移类型或/var /www/html/Satori/src/Symfony/AcmeBundle/Entity/TagRepository.php第34行中的空白

我在树枝上用以下方式调用标签:

枝条

{% for tag, weight in tags %}
    <span class="weight-{{ weight }}"><a href="">{{ tag }}</a></span>
{% else %}
    <p>There are no tags</p>
{% endfor %}

控制器(转储$ tags可以很好地显示所有标签)

public function footerAction()
{
    $em = $this->getDoctrine()->getManager();

    $tags = $em->getRepository('AcmeBundle:Tag')
        ->getTags();

    $tagWeights = $em->getRepository('AcmeBundle:Tag')
        ->getTagWeights($tags);
    var_dump($tagWeights); die();
    return array(
        'tags' => $tagWeights,

    );
}

这是getTags和getTagWeights:

public function getTags()
{
    $tags = $this->createQueryBuilder('t')
        ->select('t.tag')
        ->getQuery()
        ->getResult();

    return $tags;
}

public function getTagWeights($tags)
{
    $tagWeights = array();

    if (empty($tags))
        return $tagWeights;

    foreach ($tags as $tag)
    {
        $tagWeights[$tag] = (isset($tagWeights[$tag['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;
}

标记实体

class Tag
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="tag", type="string", length=255)
     */
    private $tag;

    /**
     * @ORM\ManyToMany(targetEntity="Blog", mappedBy="tags")
     */
    protected $blogs;

    public function __construct()
    {
        $this->blogs = new ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set tag
     *
     * @param string $tag
     * @return Tag
     */
    public function setTag($tag)
    {
        $this->tag = $tag;

        return $this;
    }

    /**
     * Get tag
     *
     * @return string 
     */
    public function getTag()
    {
        return $this->tag;
    }

    /**
     * Add blogs
     *
     * @param \AcmeBundle\Entity\Blog $blogs
     * @return Tag
     */
    public function addBlog(\AcmeBundle\Entity\Blog $blogs)
    {
        $this->blogs[] = $blogs;

        return $this;
    }

    /**
     * Remove blogs
     *
     * @param \AcmeBundle\Entity\Blog $blogs
     */
    public function removeBlog(\AcmeBundle\Entity\Blog $blogs)
    {
        $this->blogs->removeElement($blogs);
    }

    /**
     * Get blogs
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getBlogs()
    {
        return $this->blogs;
    }
}

提出的解决方案和结果

使用(isset($tagWeights[$tag['tag']])) 错误:

ContextErrorException:警告:/var/www/html/Satori/src/Symfony/AcmeBundle/Entity/TagRepository.php第34行中的非法偏移类型

使用(isset($tagWeights[$tag[$tag->getTag()])) 错误:

ContextErrorException:注意:尝试在/var/www/html/Satori/src/Symfony/AcmeBundle/Entity/TagRepository.php第34行获取非对象的属性

1 个答案:

答案 0 :(得分:0)

我想我知道这里发生了什么......

当您在footerAction

中拨打此电话时
$tags = $em->getRepository('AcmeBundle:Tag')
    ->getTags();

您的存储库会返回一个数组数组。所以当你说:

isset($tagWeights[$tag])

... $tag实际上是结果集的关联数组。我想你在这里说的是:

isset($tagWeights[$tag['tag']])

那就是说,我建议不要给你的Tag实体一个名为tag的财产。标签没有标签,它有名称或描述。而且这里说得更有意义

isset($tagWeights[$tag['name']])

...或者让存储库返回Tag实体本身(为什么你甚至需要覆盖内置的getXyz()方法?),然后执行:

isset($tagWeights[$tag->getName()])