查询构建器singlevaluedAssociationField预期



我有本机SQL查询,左JOIN在有或条件时,如何在查询构建器中表示它?

 $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关系,没有单独的表,当我尝试左JOIN时,我有错误SingleValuedAssociationField expected.

用户实体

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

CostObject实体

class CostObject
{
    /**
 * @var CostObject
 *
 * @ORMManyToOne(targetEntity="CostObject", inversedBy="children")
 * @ORMJoinColumns({
 *   @ORMJoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
 * })
 */
private $parent;
    /**
 * @var ArrayCollection
 *
 * @ORMManyToMany(targetEntity="User", inversedBy="costObjects")
 * @ORMJoinTable(name="cost_object_managers",
 *      joinColumns={@ORMJoinColumn(name="cost_object_id", referencedColumnName="id", onDelete="CASCADE")},
 *      inverseJoinColumns={@ORMJoinColumn(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

我需要像在nativbe查询中更改它,但是当我添加条件时,我会发现错误

与条件查询

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

和错误

[Semantical Error] line 0, col 121 near 'costObjects =': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.

我尝试添加 IDENTITY,就像在答案中重新介绍了我

        ->leftJoin('t.costObject', 'co')
        ->leftJoin(
            'co.users',
            'com',
            Join::WITH,
            $qb->expr()->orX
                (
                    'IDENTITY(com.costObjects) = co.id',
                    'IDENTITY(com.costObjects) = co.parent'
                )
            )

但仍然有错误

[Semantical Error] line 0, col 130 near 'costObjects)': Error: Invalid PathExpression. Must be a SingleValuedAssociationField.

尝试以下:

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

相关内容

  • 没有找到相关文章

最新更新