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



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

我的过程是获得标签,然后对标签的受欢迎程度进行加权。标签被设置为带有Blog实体的manymany/manymany实体。

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

这是我如何访问标签在树枝:

{% 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 %}

来自文档:

数组和对象不能用作键。非法偏移类型

您试图在这里使用$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;

相关内容

  • 没有找到相关文章

最新更新