简体   繁体   中英

Convert sql to doctrine orm in aend framework

I have a query which looks like below:

SELECT * 
FROM mydb.users
left join mydb.job on
users.id = job.userid;

Now I am using doctrine orm in querying database but I am newbie here. What I have done so far is below but it doesn't work as expected.

$em = $this->getEntityManager();
$qb = $em->createQueryBuilder();

$qb->select(array('a', 'c'))
        ->from('Admin\Entity\User', 'a')
        ->leftJoin('a.id', 'c');

$query = $qb->getQuery();
$results = $query->getResult();

return $results;

you do not need the from clause and array from clause select

$em = $this->getEntityManager();
$qb = $em->createQueryBuilder('u');

$qb->select('u', 'j')
   ->leftJoin('u.jobs', 'j')

$query = $qb->getQuery();
$results = $query->getResult();

return $results;

it is mandatory that your users entity contains a "one to many" jobs for the attribute.

trick to display the SQL output :

var_dump($qb->getQuery()->getSQL());

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