2 years ago

#65791

test-img

Steve

Why Does SQL return the correct info but QueryBuilder / DQL returns NULL for joined table

I have 2 statements that I believe are identical:

SQL Version:

$em = $this->getDoctrine()->getManager();

$RAW_QUERY = 'SELECT *
              FROM licences
              JOIN users ON licences.user_id=users.id
              WHERE licences.type=:licenceType
              AND licences.created_at BETWEEN :startDate AND :endDate';

$statement = $em->getConnection()->prepare($RAW_QUERY);

// Set parameters
$statement->bindValue('licenceType', $licenceType);
$statement->bindValue('startDate', $startDate->format('Y-m-d H:i:s'));
$statement->bindValue('endDate', $endDate->format('Y-m-d H:i:s'));
$statement->execute();

$licences = $statement->fetchAll();

foreach ($licences as $licence) {
    dump($licence);
}
die;

DQL / Query Builder Version:

$licences = $this->get('em')->getRepository(Licence::class)->createQueryBuilder('l')
    ->join('l.user', 'u')
    ->where('l.type = :type')
    ->andwhere('l.createdAt BETWEEN :start AND :end')
    ->setParameter('type', $licenceType)
    ->setParameter('start', $startDate)
    ->setParameter('end', $endDate);

$licences = $licences->getQuery()->getResult();
foreach ($licences as $licence) {
    dump($licence);
}
die;

However the SQL version returns actual info from the user table whereas in the DQL version it is all NULL. I have tried experimenting with the DQl (especially the ->join but to no avail). Any help much appreciated!

sql

symfony

join

doctrine-orm

dql

0 Answers

Your Answer

Accepted video resources