Query Builder SingleValuedAssociationField是预期的

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Query Builder SingleValuedAssociationField是预期的相关的知识,希望对你有一定的参考价值。

当有on或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关系,没有单独的表,当我尝试左连接条件我有错误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

我需要像在本机查询中一样更改它但是当我向左连接添加条件时我得到错误

查询条件

        $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'
            )
        )

以上是关于Query Builder SingleValuedAssociationField是预期的的主要内容,如果未能解决你的问题,请参考以下文章

方法 Illuminate\Database\Query\Builder::setContainer 不存在

BadMethodCallException : 方法 Illuminate\Database\Query\Builder::offers 不存在

未定义的属性:Illuminate\Database\Query\Builder::$sessionId

jquery query builder只适合单表查询吗

调用未定义的方法 Illuminate\Database\Query\Builder::when()

调用未定义的方法 Illuminate\Database\Query\Builder::unique()