Symfony2 -使用标签权重为流行标签设置标签云



我正在尝试设置一个加权标签云,当标签是博客实体中的字符串属性时,它可以工作。

现在我已经设置了标签作为它自己的实体,并与博客作为双向的manymany/manymany关系。

不用说它不工作,我猜这是因为标签现在是它自己的对象。

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

当我从下面的控制器转储$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;
}

contextrorexception:警告:isset中的非法偏移类型或/var/web/html/satory/src/symfony/acmebundle/entity/tagrepository .php第34行

我在twig中以以下方式调用标签:

树枝> ontroller (dump $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
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var string
     *
     * @ORMColumn(name="tag", type="string", length=255)
     */
    private $tag;
    /**
     * @ORMManyToMany(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 AcmeBundleEntityBlog $blogs
     * @return Tag
     */
    public function addBlog(AcmeBundleEntityBlog $blogs)
    {
        $this->blogs[] = $blogs;
        return $this;
    }
    /**
     * Remove blogs
     *
     * @param AcmeBundleEntityBlog $blogs
     */
    public function removeBlog(AcmeBundleEntityBlog $blogs)
    {
        $this->blogs->removeElement($blogs);
    }
    /**
     * Get blogs
     *
     * @return DoctrineCommonCollectionsCollection 
     */
    public function getBlogs()
    {
        return $this->blogs;
    }
}

建议解决方案及结果

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

错误:

contextrorexcexception: Warning:非法偏移类型在/var/http:///html/satori/src/symfony/acmebundle/entity/tagrepository .php第34行

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

错误:

contextrorexception:注意:试图获得非对象的属性在/var/ww/html/satori/src/symfony/acmebundle/entity/tagrepository .php行34

我想我知道这是怎么回事了…

当您在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()])

相关内容

  • 没有找到相关文章

最新更新