leftJoin上的Doctrine查询错误

时间:2016-11-04 09:35:14

标签: symfony doctrine-orm

我写了这个查询。

$query = $this->getEntityManager()->createQueryBuilder();
    $query->select(['cart','subscriptions', 'member'])
    ->from('ComitiUserBundle:Cart', 'cart')
    ->leftJoin('cart.subscriptions', 'subscriptions')
    ->leftJoin('subscriptions.member', 'member')
    ->where('cart.club = :club_id')
    ->andWhere('subscriptions.clubSeason = :season_id')
    ->setParameter('club_id', $club)
    ->setParameter('season_id', $season);
    if($section != null && !is_array($section)){
        $query->andWhere('subscriptions.section = :section_id')
        ->setParameter('section_id', $section);
    } elseif($section != null && is_array($section)){
        $query->andWhere('subscriptions.section IN :section_ids')
        ->setParameter('section_ids', $section);
    }
    $query->orderBy('cart.transaction_date','DESC');

    return $query->getQuery()->getResult();

我得到了这个错误:

  

[语法错误]第0行,第28行:错误:预期的IdentificationVariable | ScalarExpression | AggregateExpression |功能声明| PartialObjectExpression | “(”Subselect“)”| CaseExpression,获得'成员'

事实上,我尝试用其中的成员来保湿我的结果集合。成员是我的Subscription实体中的manyToOne属性。它的定义如下:

/**
 * @ORM\ManyToOne(targetEntity="Comiti\UserBundle\Entity\UserComiti", inversedBy="subscriptions", cascade={"persist"})
 * @ORM\JoinColumn(name="member_id", referencedColumnName="id")
 */
protected $member;

1 个答案:

答案 0 :(得分:0)

因此,在尝试加入类别实体的类别组时遇到了完全相同的错误。请参见下面的代码:

    $qb = $this->entityManager->createQueryBuilder()
            ->select('c')
            ->from(Category::class, 'c');

        if (isset($fields['categoryGroup'])) {
            $qb = $qb->join('c.categoryGroup', 'group')
                ->addSelect('group');
        }

        $qb = $qb->getQuery()
            ->getArrayResult();

这导致了与OP完全相同的错误,只是带有'group'。我的猜测是,您无法在查询中使用数据库保留字,例如groupmember

对我来说,将别名更改为cg之类的东西就可以了。