我想获取标签与他们的相关文章。然而,我们的目标是根据相关文章的数量对它们进行排序。如何使用QueryBuilder和repositiry类来实现这一点?我是symfony的新手,我尝试过通过DQL获取标签,但它不能获取整个实体。
标签和文章在@ManyToMany关系中:
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\ArticleRepository")
*/
class Article
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="string", length=255)
*/
private $shortDescription;
/**
* @ORM\Column(type="text")
*/
private $content;
/**
* @ORM\Column(type="float")
*/
private $price;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="articles")
*/
private $category;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Tag", inversedBy="articles")
*/
private $tags;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Author", inversedBy="articles")
*/
private $authors;
public function __construct()
{
$this->tags = new ArrayCollection();
$this->authors = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getShortDescription(): ?string
{
return $this->shortDescription;
}
public function setShortDescription(string $shortDescription): self
{
$this->shortDescription = $shortDescription;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getPrice(): ?float
{
return $this->price;
}
public function setPrice(float $price): self
{
$this->price = $price;
return $this;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): self
{
$this->category = $category;
return $this;
}
/**
* @return Collection|Tag[]
*/
public function getTags(): Collection
{
return $this->tags;
}
public function addTag(Tag $tag): self
{
if (!$this->tags->contains($tag)) {
$this->tags[] = $tag;
}
return $this;
}
public function removeTag(Tag $tag): self
{
if ($this->tags->contains($tag)) {
$this->tags->removeElement($tag);
}
return $this;
}
/**
* @return Collection|Author[]
*/
public function getAuthors(): Collection
{
return $this->authors;
}
public function addAuthor(Author $author): self
{
if (!$this->authors->contains($author)) {
$this->authors[] = $author;
}
return $this;
}
public function removeAuthor(Author $author): self
{
if ($this->authors->contains($author)) {
$this->authors->removeElement($author);
}
return $this;
}
}
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\TagRepository")
*/
class Tag
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Article", mappedBy="tags")
*/
private $articles;
public function __construct()
{
$this->articles = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return Collection|Article[]
*/
public function getArticles(): Collection
{
return $this->articles;
}
public function addArticle(Article $article): self
{
if (!$this->articles->contains($article)) {
$this->articles[] = $article;
$article->addTag($this);
}
return $this;
}
public function removeArticle(Article $article): self
{
if ($this->articles->contains($article)) {
$this->articles->removeElement($article);
$article->removeTag($this);
}
return $this;
}
}我在TagRepository中使用的最后一个方法是:
public function findTagsByArticlesCount()
{
return $this->createQueryBuilder('tag')
->leftJoin('tag.articles', 'article')
->orderBy('count(article.id)', 'DESC')
->getQuery()
->execute();
}发布于 2018-08-29 18:50:36
我依稀记得两年前的一个类似案例,我通过HIDDEN专栏解决了这个问题:
public function findTagsByArticlesCount()
{
return $this->createQueryBuilder('tag')
->leftJoin('tag.articles', 'article')
->addSelect('COUNT(article.id) as HIDDEN cnt')
->orderBy('cnt', 'DESC')
->getQuery()
->execute();
}然而,我目前不在我的工作站测试这一点。你能试试吗?
希望这能帮上忙。
发布于 2018-08-29 21:07:46
统计文章中的标签时缺少groupBy:
$this->createQueryBuilder('tag')
->leftJoin('tag.articles', 'article')
->addSelect('COUNT(tag.id) AS tagcount')
->groupBy('tag.id')
->orderBy('tagcount', 'DESC')
->getQuery()
->execute();注意:在加入Articles时,您将收到number of tags * number of articles记录,您需要按标签分组,以统计相关文章中的标签数量。
注2:我更喜欢使用innerJoin而不是leftJoin,而不是选择没有相关文章的标签。
https://stackoverflow.com/questions/52074962
复制相似问题