首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据相关文章数量对标签实体排序

问根据相关文章数量对标签实体排序
EN

Stack Overflow用户
提问于 2018-08-29 18:14:19
回答 2查看 63关注 0票数 0

我想获取标签与他们的相关文章。然而,我们的目标是根据相关文章的数量对它们进行排序。如何使用QueryBuilder和repositiry类来实现这一点?我是symfony的新手,我尝试过通过DQL获取标签,但它不能获取整个实体。

标签和文章在@ManyToMany关系中:

代码语言:javascript
复制
<?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中使用的最后一个方法是:

代码语言:javascript
复制
public function findTagsByArticlesCount()
{
          return $this->createQueryBuilder('tag')
              ->leftJoin('tag.articles', 'article')
              ->orderBy('count(article.id)', 'DESC')
              ->getQuery()
          ->execute();
}
EN

回答 2

Stack Overflow用户

发布于 2018-08-29 18:50:36

我依稀记得两年前的一个类似案例,我通过HIDDEN专栏解决了这个问题:

代码语言:javascript
复制
public function findTagsByArticlesCount()
{
    return $this->createQueryBuilder('tag')
        ->leftJoin('tag.articles', 'article')
        ->addSelect('COUNT(article.id) as HIDDEN cnt')
        ->orderBy('cnt', 'DESC')
        ->getQuery()
        ->execute();
}

然而,我目前不在我的工作站测试这一点。你能试试吗?

希望这能帮上忙。

票数 0
EN

Stack Overflow用户

发布于 2018-08-29 21:07:46

统计文章中的标签时缺少groupBy:

代码语言:javascript
复制
$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,而不是选择没有相关文章的标签。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52074962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档