Symfony & Doctrine: Using table joins with unassociated entities

In Symfony using Doctrine, if no association is available for two entities, you can still join them and treat them like they have a relationship. To do so, you have to use the “Join: WITH” method. Here’s an example query to illustrate, notice the “a.user = u.id”.
public function getHistory($users) {
$qb = $this->entityManager->createQueryBuilder();
$qb ->select('a', 'u')
->from('Credit\Entity\UserHistory', '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');
return $qb->getQuery()->getResult();
}Some other helpful links on Stack Overflow:
http://stackoverflow.com/questions/15087933/how-to-do-left-join-in-doctrine
Here’s a great reference for advanced Doctrine queries: