1
votes

I have two entities - BlacklistedUsers and UserAccounts. I am trying to build a DQL that allows me to join those two entities together and get BlacklistedUsers#id, UserAccounts#name and BlacklistedUsers#reason.

My query builder code is

$qb = $this->createQueryBuilder('u')
                    ->join(UserAccounts::class, 'a');   

And regardless of how simple it is the following code still manages to fail

Fatal error: Uncaught exception 'Doctrine\ORM\Query\QueryException' with message 'SELECT u FROM Orm\Entity\BlacklistedUsers u INNER JOIN Orm\Entity\UserAccounts a ORDER BY u.reason asc' in ***

I double checked the generated query agains docs ( http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html ) and compared it to their example

<?php
$query = $em->createQuery('SELECT a FROM CmsArticle a JOIN a.user u ORDER BY u.name ASC');
$articles = $query->getResult(); // array of CmsArticle objects

I do not see any difference between the query they have and what my query builder generates.

Below is the class diagram to make things easier

class diagram

Thanks for your help

1

1 Answers

0
votes

It looks like your class isn't correctly mapped. Try to create queryBuilder from class repository:

$repo = $this->getDoctrine()->getRepository('AppBundle:BlacklistedUsers');

$query = $repo->createQueryBuilder('b')
    ->select('b.id, u.name, b.reason')
    ->join('b.userAccount', 'u')
    ->orderBy('b.reason', 'ASC')
    ->getQuery();

$products = $query->getResult();