Symfony2 - 访问具有值的标签数组会出现错误

问题描述 投票:0回答:1

我正在尝试使用 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() 并且有数据?

我的流程是获取标签,然后对标签的受欢迎程度进行加权。标签设置为带有博客实体的 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 %}
php arrays symfony tags tag-cloud
1个回答
0
投票

来自文档

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

您正在尝试使用

$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;
© www.soinside.com 2019 - 2024. All rights reserved.