简体   繁体   中英

Doctrine Query builder get field of foreign entity

I have a query like :

    $qb = $this->createQueryBuilder('m');
    $qb->select('m.id', 'IDENTITY(m.home)');
    return $qb->getQuery()->getResult();

m.home field is a foreign key to another table (This other table has a field ID and another name).

With this I get Id of m and id of the foreign key.

How can I get the name field of my other instead of id ?

You can fetch the data of the associated entity with a join:

SELECT m.id, home.name
FROM YourEntity AS m
JOIN m.home AS home

Use a leftJoin to get data from a relationship:

$qb = $this->createQueryBuilder('m');
$qb = $qb->leftJoin('AppBundle\Entity\Home', 'h');
$qb->select('m.id', 'IDENTITY(m.home)', 'h.name');
return $qb->getQuery()->getResult();

Replace 'AppBundle\\Entity\\Home' by the namespaced class of your entity "home".

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