Python
Python

Reputation: 169

Doctrine query error on leftJoin

I wrote this query.

$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();

I've got this error in return :

[Syntax Error] line 0, col 28: Error: Expected IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression | "(" Subselect ")" | CaseExpression, got 'member'

In fact I try to hydrate my result collection with members in it. Member is manyToOne property in my Subscription entity. It is defined like this :

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

Upvotes: 1

Views: 234

Answers (1)

Timo Hermans
Timo Hermans

Reputation: 165

So I ran into the exact same error when trying to join a category group for a category entity. See the code below:

    $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();

This resulted in the exact same error as the OP, only with 'group'. My guess is that you cannot use database reserved words in the query, like group and member.

For me, changing the alias to something like cg worked just fine.

Upvotes: 1

Related Questions