ManyToMany DQL来自SQL,访问OneToMany

时间:2016-08-28 23:28:29

标签: php symfony doctrine-orm dql

正在运行的应用程序使用本机SQL查询从ManyToMany实体关系中获取项目。尝试翻译该查询最终会出现在无数错误消息中。 Household实体与Reason具有ManyToMany关系。两个实体和他们的关系是正确定义的。加入表( household_reason )未在... / Entity中定义。我想到的部分是如何加入联系人实体,家庭实体与该实体有OneToMany关系。

正在运行的SQL:

    select distinct r.reason
    from reason r
    join household_reason hr on hr.reason_id = r.id
    join household h on h.id = hr.household_id
    join contact c on c.household_id = h.id...

首先,这里是Reason存储库中查询构建器的开头:

$query = $this->createQueryBuilder('r')->select('distinct r.reason')
    ->innerJoin('r.households', 'h')

现在有复数households,如何指定与单一家庭的关系?

1 个答案:

答案 0 :(得分:0)

花了一段时间,但以下工作:

    $query = $this->createQueryBuilder('r')->select('distinct r.reason')
        ->innerJoin('r.households', 'h')
        ->innerJoin('TruckeeProjectmanaBundle:Contact', 'c', 'WITH c.household = h.household')
        ->where('c.contactDate >= :startDate')
        ->andWhere('c.contactDate <= :endDate')
        ->andWhere('r.enabled = TRUE')
        ->orderBy('r.id')
        ->setParameters($dateCriteria)
        ->getQuery()
        ->getResult();