简体   繁体   中英

Left Join Doctrine query builder

I am trying to understand joins without associations. Everything I am seeing where people are having success makes sense. When I apply it to my query, it errors.

$query_builder = $this->getEntityManager()->createQueryBuilder();
$query_builder->select('er', 'es')
        ->from('EmployeeRegister', 'er')
        ->leftJoin(
            'EmployeeStatus',
            'es',
            \Doctrine\ORM\Query\Expr\Join::ON,
            'er.eid = es.eid'
        )
        ->where('er.eid = :eid')
        ->setParameter('eid', '0999999');
    $result = $query_builder->getQuery()->getResult();

This results in an error:

PHP Fatal error:  Uncaught Doctrine\ORM\Query\QueryException: SELECT er, es FROM EmployeeRegister er LEFT JOIN EmployeeStatus es ON er.eid = es.eid WHERE er.eid = :eid in /Documents/PHPStormProjects/ax/_vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php:43
Stack trace:
#0 /Documents/PHPStormProjects/ax/_vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(491): Doctrine\ORM\Query\QueryException::dqlError('SELECT er, es F...')
#1 /Documents/PHPStormProjects/ax/_vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(991): Doctrine\ORM\Query\Parser->semanticalError('line 0, col 19 ...', Array)
#2 /Documents/PHPStormProjects/ax/_vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(1734): Doctrine\ORM\Query\Parser->validateAbstractSchemaName('EmployeeRegiste...')
#3 /Documents/PHPStormProjects/ax/_vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(1584): Doctrine\ORM\Query\Parser->RangeVariableDeclaration()
#4 /Us in /Documents/PHPStormProjects/ax/_vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php on line 65

Any suggestions would be super helpful. Still learning doctrine in general.

If someone could explain what or why this is in Doctrine 2. When I changed the join to use WITH instead of ON it works.

$query_builder->select('er', 'es')
    ->from('EmployeeRegister', 'er')
    ->leftJoin(
        'EmployeeStatus',
        'es',
        \Doctrine\ORM\Query\Expr\Join::WITH,
        'er.eid = es.eid'
    )
    ->where('er.eid = :eid')
    ->setParameter('eid', '0999999');

In what scenarios does ON work? I tried some random ones and nothing seems to work with ON, only WITH.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM