Home > Backend Development > PHP Tutorial > How to Resolve the \'Expected Doctrine\\ORM\\Query\\Lexer::T_WITH, got \'ON\'\' Error During Left Joins in Doctrine?

How to Resolve the \'Expected Doctrine\\ORM\\Query\\Lexer::T_WITH, got \'ON\'\' Error During Left Joins in Doctrine?

Susan Sarandon
Release: 2024-10-29 04:24:02
Original
528 people have browsed it

How to Resolve the

How to Perform Left Joins in Doctrine

When working with complex data models, it becomes necessary to retrieve data from multiple tables by establishing relationships between them. Left joins allow you to fetch all rows from one table and only the matching rows from the other table.

Issue Encountered

A common error that may arise when attempting a left join in Doctrine is receiving the following syntax error:

[Syntax Error] line 0, col 98: Error: Expected DoctrineORMQueryLexer::T_WITH, got 'ON'

This error occurs when "ON" is used in the join clause instead of "WITH." To resolve this, replace "ON" with "WITH" as shown below:

<code class="php">$qb->leftJoin('User\Entity\User', 'u', \Doctrine\ORM\Query\Expr\Join::WITH, 'a.user = u.id')</code>
Copy after login

Implementing Left Joins

To perform a left join in Doctrine, there are two approaches:

With an Association:

If your entity has an association with the table you want to join, you can use the following syntax:

<code class="php">$qb
    ->select('a', 'u')
    ->from('Credit\Entity\UserCreditHistory', 'a')
    ->leftJoin('a.user', 'u')
    ->where('u = :user')
    ->setParameter('user', $users)
    ->orderBy('a.created_at', 'DESC');</code>
Copy after login

In this case, "CreditEntityUserCreditHistory#user" represents the association between the two entities.

Without an Association:

If no association exists, you can use the following syntax:

<code class="php">$qb
    ->select('a', 'u')
    ->from('Credit\Entity\UserCreditHistory', 'a')
    ->leftJoin(
        'User\Entity\User',
        'u',
        \Doctrine\ORM\Query\Expr\Join::WITH,
        'a.user = u.id'
    )
    ->where('u = :user')
    ->setParameter('user', $users)
    ->orderBy('a.created_at', 'DESC');</code>
Copy after login

This query retrieves records from both tables and returns a result set containing arrays of the following format:

<code class="php">array(
    array(
        0 => UserCreditHistory instance,
        1 => Userinstance,
    ),
    array(
        0 => UserCreditHistory instance,
        1 => Userinstance,
    ),
    // ...
)</code>
Copy after login

The above is the detailed content of How to Resolve the \'Expected Doctrine\\ORM\\Query\\Lexer::T_WITH, got \'ON\'\' Error During Left Joins in Doctrine?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template