Php 将原始sql查询与自连接转换为条令queryBuilder查询

Php 将原始sql查询与自连接转换为条令queryBuilder查询,php,sql,doctrine,symfony4,Php,Sql,Doctrine,Symfony4,您好,我有sql查询: select r.criterion_id, r.logical_state_id, r.created_at from( select acr.criterion_id, max(acr.created_at) as max_date from audit_company_result acr where acr.audit_company_id = 42 group by acr.criterion_id ) as x inner join aud

您好,我有sql查询:

select r.criterion_id, r.logical_state_id, r.created_at from(
select  acr.criterion_id, max(acr.created_at) as max_date  from audit_company_result acr
    where acr.audit_company_id = 42
    group by acr.criterion_id
) as x inner join audit_company_result as r on r.criterion_id = x.criterion_id and r.created_at = x.max_date
$qb = $this->getEntityManager()->getRepository(AuditCompanyResult::class)->createQueryBuilder('acr');
        $result = $qb->select('acr')
            ->addSelect('partial cr.{id}')
            ->addSelect($qb->expr()->max('acr.createdAt'))
            ->leftJoin('acr.criterion','cr')
            ->groupBy('cr.id')
            ->where('acr.auditCompany=42')
            ->getQuery()->getArrayResult();
我想翻译成doctrine QueryBuilder表单。我有一个愿望:

审计公司:

class AuditCompany
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="datetime")
     */
    private $createdAt;

    /**
     * @ORM\Column(type="datetime", nullable = true)
     */
    private $startedAt;

    /**
     * @ORM\Column(type="datetime")
     */
    private $plannedEnding;

    /**
     * @ORM\Column(type="datetime", nullable = true)
     */
    private $endingAt;

    /**
     * @ORM\Column(type="boolean")
     */
    private $isCertification;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="companyAudits")
     * @ORM\JoinColumn(nullable=false)
     */
    private $company;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Audit", inversedBy="companyAudits")
     * @ORM\JoinColumn(nullable=false)
     */
    private $audit;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\AuditCompanyResult", mappedBy="auditCompany")
     */
    private $auditCompanyResults;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Employee")
     * @ORM\JoinColumn(nullable=false)
     */
    private $responsibleEmployee;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $manualStartedAt;
标准:

class Criterion
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    private $info;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\Column(type="text")
     */
    private $algorithmInfo;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $comparableType;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\CategoryCriterion", inversedBy="criteria")
     * @ORM\JoinColumn(nullable=false)
     */
    private $categoryCriterion;

    /**
     * @ORM\Column(type="text")
     */
    private $logicalMeasureInfo;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\CriterionLogicalStateValue", mappedBy="criterion")
     */
    private $criterionLogicalStateValues;

    /**
     * @ORM\Column(type="boolean")
     */
    private $isAutomatic;
审计公司结果:

class AuditCompanyResult
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Criterion")
     * @ORM\JoinColumn(nullable=false)
     */
    private $criterion;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\AuditCompany", inversedBy="auditCompanyResults")
     * @ORM\JoinColumn(nullable=false)
     */
    private $auditCompany;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $comment;

    /**
     * @ORM\Column(type="boolean")
     */
    private $autoVerified;

    /**
     * @var datetime $created
     *
     * @ORM\Column(name="created_at", type="datetime", nullable = false)
     */
    private $createdAt;

    /**
     * @var datetime $updated
     *
     * @ORM\Column(name="updated_at", type="datetime", nullable = false)
     */
    private $updatedAt;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\CriterionLogicalStateValue")
     * @ORM\JoinColumn(nullable=false)
     *
     */
    private $logicalState;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $criterionValue;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $source;
标准逻辑状态值:

class CriterionLogicalStateValue
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\Column(type="boolean")
     */
    private $logicalState;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Criterion", inversedBy="criterionLogicalStateValues")
     * @ORM\JoinColumn(nullable=false)
     */
    private $criterion;
我只能从上面的sql查询中编写子查询:

select r.criterion_id, r.logical_state_id, r.created_at from(
select  acr.criterion_id, max(acr.created_at) as max_date  from audit_company_result acr
    where acr.audit_company_id = 42
    group by acr.criterion_id
) as x inner join audit_company_result as r on r.criterion_id = x.criterion_id and r.created_at = x.max_date
$qb = $this->getEntityManager()->getRepository(AuditCompanyResult::class)->createQueryBuilder('acr');
        $result = $qb->select('acr')
            ->addSelect('partial cr.{id}')
            ->addSelect($qb->expr()->max('acr.createdAt'))
            ->leftJoin('acr.criterion','cr')
            ->groupBy('cr.id')
            ->where('acr.auditCompany=42')
            ->getQuery()->getArrayResult();
我不知道如何在QueryBuilder中编写整个查询。有自我连接。与第条完全相同:

但如何将原始sql查询转换为QueryBuilder呢?我将非常感谢你的帮助。
最好的问候

懒惰的回答:创建一个存储函数而不读取它的全部内容,如果它只是一个自联接,那么假设您有
$entity\parent
,其中
\parent
属性是
$entity
的一个实例,您只需执行
->联接('entity.parent',p')
。这将告诉ORM在别名“p”下加入“parent”属性的实体。更多信息。(只要确保你的关系不是无限循环)好的,它可以工作。但问题是我什么时候要添加新结果。在保存新结果时,是否有任何方法可以设置与新记录相同的父\u id-like id?