Php 条令:从一对多关系中检索有序的单个实体
假设以下结构为数据库结构,即一个类别有许多文章,而一篇文章有许多注释:Php 条令:从一对多关系中检索有序的单个实体,php,symfony,doctrine-orm,doctrine,dql,Php,Symfony,Doctrine Orm,Doctrine,Dql,假设以下结构为数据库结构,即一个类别有许多文章,而一篇文章有许多注释: Category: - id Article: - id - category_id Comment: - id - created_at - article_id 使用条令,最佳/标准的方法是什么: 获取10篇文章,并立即加载每篇文章的最新评论 获取10个类别并加载每个类别中最新的注释 通过比较,在Laravel中,我可以在类别实体上建立一个关系,在文章中有一条注释,并按创建日期排序。然而,我不知道如何通过信条或DQ
Category:
- id
Article:
- id
- category_id
Comment:
- id
- created_at
- article_id
使用条令,最佳/标准的方法是什么:
- 创建CommentRepository类
- 在其中创建
和getMostRecentCommentsForCategories(数组$categories)
方法getmostrecentcommentsforcaticles(数组$articles)
- 它们可以返回按类别/文章ID分组的注释数组
- 创建CommentRepository类
- 在其中创建
和getMostRecentCommentsForCategories(数组$categories)
方法getmostrecentcommentsforcaticles(数组$articles)
- 它们可以返回按类别/文章ID分组的注释数组
@OrderBy
注释,例如
/**
* @var RiskMitigation[]|ArrayCollection
* @ORM\OneToMany(targetEntity="App\Entity\RiskMitigation", mappedBy="risk")
* @ORM\OrderBy({"effectiveDate" : "ASC"})
*/
private $mitigations;
但是,使用注释不可能急于只加载10个最新版本。对于订购,您可以使用
@OrderBy
注释,例如
/**
* @var RiskMitigation[]|ArrayCollection
* @ORM\OneToMany(targetEntity="App\Entity\RiskMitigation", mappedBy="risk")
* @ORM\OrderBy({"effectiveDate" : "ASC"})
*/
private $mitigations;
然而,您计划只急切地加载10个最新版本是不可能使用注释的