我有一个方法设置来衡量最常用的标签。我使用这里的示例作为学习练习:http://tutorial.symblog.co.uk/docs/customising-the-view-more-with-twig.html(请参阅标签云)我正在这样做略有不同,因为我使用标签作为实体,而不是基于链接的博客实体的字符串属性。
标签设置为博客实体的多对多/多对多,请参见下文。
我遇到的问题是标签正在按预期检索,但显然没有设置,因此所有标签都以整数 1 返回。我该如何纠正这个问题?我是否错误地将标签调用到没有考虑到哪些标签被多次使用的地方?
$tagWeights[$tag['tag']] = (isset($tagWeights[$tag['tag']])) ? $tagWeights[$tag['tag']] + 1 : 1;
getTagWeights(var 转储 $tagWeights[$tag['tag']] 返回 int 1)
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['tag']] = (isset($tagWeights[$tag['tag']])) ? $tagWeights[$tag['tag']] + 1 : 1;
var_dump($tagWeights[$tag['tag']]);
}
// 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;
}
}
博客实体
/**
* @ORM\ManyToMany(targetEntity="Tag", inversedBy="blogs")
* @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
*/
protected $tags;
public function __construct()
{
$this->tags = new ArrayCollection();
}
/**
* Add tags
*
* @param \AcmeBundle\Entity\Tag $tags
* @return Blog
*/
public function addTag(\AcmeBundle\Entity\Tag $tags)
{
$this->tags[] = $tags;
return $this;
}
/**
* Remove tags
*
* @param \AcmeBundle\Entity\Tag $tags
*/
public function removeTag(\AcmeBundle\Entity\Tag $tags)
{
$this->tags->removeElement($tags);
}
/**
* Get tags
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTags()
{
return $this->tags;
}
你的函数总是返回 1 的问题是因为你做了以下事情
foreach ($tags as $tag)
{
$tagWeights[$tag['tag']] = (isset($tagWeights[$tag['tag']])) ? $tagWeights[$tag['tag']] + 1 : 1;
var_dump($tagWeights[$tag['tag']]);
}
并且您为每个标签迭代一次,因此每个标签不会多次访问 $tagWeights[$tag['tag']] ,因此始终具有值 1。
执行此操作的一种方法是使用博客而不是标签,因此您不必更改太多代码:
foreach ($blogs as $blog)
{
foreach($blog->getTags() as $tag){
$tagWeights[$tag->getTag()] = (isset($tagWeights[$tag->getTag()])) ? $tagWeights[$tag->getTag()] + 1 : 1;
var_dump($tagWeights[$tag->getTag()]);
}
}
您需要将 $blogs 变量传递给函数,而不是 $tags
public function getTagWeights($blogs)
{
...
}
上面的代码未经测试,但它会是类似的东西。
我认为另一个问题是,在您的 Tag 实体中,您没有使用标记的唯一注释,这样,可能存在一些名称完全相同的标记,这不是该实体的预期行为标签云。你应该做这样的事情:
/**
* @var string
*
* @ORM\Column(name="tag", type="string", length=255, unique=true)
*/
private $tag;