How to Perform Left Joins in Doctrine
In your function getHistory(), you're attempting to retrieve the credit history of a user. However, the initial syntax in your join clause resulted in an error.
To perform a left join in Doctrine, you can use the following syntax:
$qb
->select('a', 'u')
->from('Credit\Entity\UserCreditHistory', 'a')
->leftJoin('a.user', 'u')
->where('u = :user')
->setParameter('user', $users)
->orderBy('a.created_at', 'DESC');
Here, a represents the alias for the UserCreditHistory entity, and u represents the alias for the joined User entity. By using the leftJoin() method, you're specifying that you want to include rows from the User table that are not matched in the UserCreditHistory table.
Alternatively, if you don't have an association between the two entities, you can use the following syntax:
$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');
````
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3