Symfony2 - 访问具有值的标记数组会产生错误

时间:2014-05-14 05:15:53

标签: arrays symfony tags tag-cloud

我正在尝试将一个数组数组的getTags()数组用于另一个方法GetTagWeights($ tags),但在使用此行时遇到错误:

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

我收到以下错误:

ContextErrorException: Warning: Illegal offset type in /var/www/html/Satori/src/Symfony/AcmeBundle/Entity/TagRepository.php line 34

问题:我在这里做错了什么,我已经转储了getTags()并且有数据?

我的流程是获取标签,然后加权标签以便流行。 Tag被设置为具有Blog实体的ManyToMany / ManyToMany实体。

getTags和getTagWeight方法(从getTags()转储$ tags返回一个数组数组)

array (size=6)
  0 => 
array (size=1)
  'tag' => string 'Tag 1' (length=5)
  1 => 
array (size=1)
  'tag' => string 'Tag 2' (length=5)
  2 => 
array (size=1)
  'tag' => string 'Tag 3' (length=5)


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['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;
}

控制器

$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,
);

标记实体

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

这就是我在树枝中访问标签的方式:

{% for tag, weight in tags %}
    <span class="weight-{{ weight }}"><a href="{{ path('AcmeBundle_tag', { 'tag': tag.tag }) }}">{{ tag.tag }}</a></span>
{% else %}
    <p>There are no tags</p>
{% endfor %}

1 个答案:

答案 0 :(得分:0)

来自the documentation

  

数组和对象不能用作键。这样做会导致警告:非法偏移类型

您正在尝试使用$tag作为关键字:

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

但是因为$tag是一个数组,所以会出错。

我想你想这样做:

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