我有实体帖子和点,它们是通过oneToMany关系连接起来的。我想要的make方法,将返回对象与大多数相关的评论计数。有可能吗?请帮帮忙,我一点也不知道。
https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/tutorials/ordered-associations.html -我应该使用这个吗?
实体: post:
/**
* @ORM\Entity(repositoryClass="App\Repository\PostRepository")
*/
class Post
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var Points
* @ORM\OneToMany(targetEntity="Points", mappedBy="post", fetch="EAGER")
*/
private $points;
/**
* @return Collection|Points[]
*/
public function getPoints(): Collection {
return $this->points;
}
...积分
/**
* @ORM\Entity(repositoryClass="App\Repository\PointsRepository")
*/
class Points
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var Post
* @ORM\ManyToOne(targetEntity="Post", inversedBy="points", fetch="EAGER")
*/
private $post;
public function getPost(): Post {
return $this->post;
}
public function setPost(Post $post ){
$this->post = $post;
}
...发布于 2019-02-02 06:08:17
假设你已经能够返回一个带有分数的帖子,你可以尝试这样做:
在App\Repository\PostRepository中:
public function postsByPoints() {
return $this->getEntityManager()->createQueryBuilder()
->select('p.post, count(pt.points) N)
->from('App:Points', 'pt')
->join('pt.post', 'p')
->where('some where clause') <- delete this if you're not selecting a subset
->groupBy('p.post')
->orderBy('N')
->getQuery()->getResult();
}在某些控制器中:
$em = $this->getDoctrine()->getManager();
$postsByPoints = $em->getRepository('App:Post')->postsByPoints();注意:未测试
发布于 2019-02-02 20:35:08
这是一个(对我来说)有效的代码
return $this->createQueryBuilder('p')
->innerJoin('p.user', 'c')
->innerJoin('p.points', 'pp')
->andWhere("p.date > '".$now->format("Y-m-d H:i:s")."'")
->setMaxResults($max)
->groupBy('pp.post')
->orderBy('pp.post','DESC')
->getQuery()
->getResult();https://stackoverflow.com/questions/54486577
复制相似问题