我在使用with或condition时有左连接的本机sql查询,如何在查询生成器中表示它?

 $query = "  SELECT  te.id
            FROM    task_executions AS te
            INNER JOIN tasks AS t ON t.id = te.task_id
            LEFT JOIN cost_objects AS co ON co.id = t.cost_object_id
            LEFT JOIN cost_object_managers AS com ON com.cost_object_id = co.id OR com.cost_object_id = co.parent_id


我需要在查询生成器中表示它。但是在User实体中我有ManyToMany关系,没有单独的表,当我尝试左连接WITH条件时,这与我需要的不一样。我需要ON的更改关系

LEFT JOIN cost_object_managers AS com ON com.cost_object_id = co.id OR com.cost_object_id = co.parent_id


用户实体

class User
{
...
/**
 * @ORM\ManyToMany(targetEntity="CostObject", mappedBy="users")
 */
private $costObjects;
}


CostObject实体

class CostObject
{
    /**
 * @var CostObject
 *
 * @ORM\ManyToOne(targetEntity="CostObject", inversedBy="children")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
 * })
 */
private $parent;

    /**
 * @var ArrayCollection
 *
 * @ORM\ManyToMany(targetEntity="User", inversedBy="costObjects")
 * @ORM\JoinTable(name="cost_object_managers",
 *      joinColumns={@ORM\JoinColumn(name="cost_object_id", referencedColumnName="id", onDelete="CASCADE")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")}
 * )
 */
private $users;


和我的查询生成器无条件

        $qb->select('te')
        ->from('AppBundle:TaskExecution', 'te')
        ->innerJoin('te.task', 't')

        ->leftJoin('t.costObject', 'co')
        ->leftJoin('co.users', 'com')


这是$query->getSQL()

SELECT some_name FROM task_executions t0_ INNER JOIN tasks t1_ ON t0_.task_id = t1_.id LEFT JOIN cost_objects c2_ ON t1_.cost_object_id = c2_.id LEFT JOIN cost_object_managers c4_ ON c2_.id = c4_.cost_object_id LEFT JOIN users u3_ ON u3_.id = c4_.user_id ORDER BY t0_.execution_start DESC


在此示例中,我看到ON关系条件LEFT JOIN users u3_ ON u3_.id = c4_.user_id。并且需要像在本地sql中一样更改它

我现在有

        $qb->select('te')
        ->from('AppBundle:TaskExecution', 'te')
        ->innerJoin('te.task', 't')
        ->leftJoin('t.costObject', 'co')
        ->leftJoin(
            'co.users',
            'com',
            Join::ON,
            $qb->expr()->orX(
                'co = com.costObjects',
                'co.parent = com.costObjects'
            )
        )


但是有错误

[Syntax Error] line 0, col 112: Error: Expected end of string, got 'ON'


如果我使用了WITH条件,则在我的sql中代表我仍然具有id关系,我不需要

            ->leftJoin(
            'co.users',
            'com',
            Join::WITH,
            $qb->expr()->orX(
                'co MEMBER OF com.costObjects',
                'co.parent MEMBER OF com.costObjects'
            )
        )

LEFT JOIN users u3_ ON u3_.id = c4_.user_id AND (EXISTS (SELECT 1 FROM cost_object_managers c5_ INNER JOIN cost_objects c6_ ON c5_.cost_object_id = c6_.id WHERE c5_.user_id = u3_.id AND c6_.id IN (c2_.id)) OR EXISTS (SELECT 1 FROM cost_object_managers c5_ INNER JOIN cost_objects c6_ ON c5_.cost_object_id = c6_.id WHERE c5_.user_id = u3_.id AND c6_.id IN (c2_.parent_id)))


我的意思是users u3_ ON u3_.id = c4_.user_id AND,但是在本机查询中,我们只有LEFT JOIN cost_object_managers AS com ON com.cost_object_id = co.id OR com.cost_object_id = co.parent_id

如何在具有ON条件类型的查询生成器中重现它?

最佳答案

如果您有查询并且该查询适合您,则无需完成所有工作即可将其转换为DQL或QueryBuilder编程语法。您可以只使用Doctrine的本机查询,然后(如果需要)将结果映射到您的对象。只需创建一个自定义存储库,并在其中创建一个大致如下所示的新方法:

public function findTaskExecutionBy...()
{
    $query = $this->entityManager->createNativeQuery('SELECT te.id FROM ...');

    return $query->getSingleScalarResult(); // If it's just one id you expect
}


如果您希望返回多个ID,也可以使用$query->getResult()。如果需要整个Task对象,请使用ResultSetMapping:

$rsm = new ResultSetMappingBuilder($this->entityManager);
$rsm->addRootEntityFromClassMetadata('App\Entity\Task', 'te');

$query = $this->entityManager->createNativeQuery(
    'SELECT te.* FROM ...',


您也可以查看Doctrine文档以获得更详细的解释和更多示例:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/native-sql.html
        $ rsm
    );

关于php - 教义QueryBuilder需要为leftJoin更改ON条件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48021552/

10-15 22:26