douhe1002 2017-08-16 11:24
浏览 78
已采纳

Doctrine和ManyToOne实体

I have two entities, country and Province, and I have set up a many-to-one relation in the Province entity:

Entity/Province

/**
 * @var \AppBundle\Entity\Country
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Country")
 * @ORM\JoinColumn(name="ubicacionpaisid", referencedColumnName="id")
 *
 */
private $ubicacionpaisid;

Here I get all results using the Province entity:

 $cb = $this->getDoctrine()
        ->getEntityManager()
        ->getRepository(Province::class)
        ->createQueryBuilder('a');

However, if I run:

 var_dump($cb->getQuery()->getDQL());

it returns:

string(41) "SELECT a FROM AppBundle\Entity\Province a"

What I expected to see was a query that joins the Country entity, into the Province entity in the SQL.

What am I missing ?

  • 写回答

3条回答 默认 最新

  • dongyang1518 2017-08-16 13:37
    关注

    I realize 2 people gave answers that just say to use EAGER loading of the association. But I can't recommend that. This makes so many assumptions about your project and how you'd always want to join that association no matter what, and can even cause issues with forms and creating unexpected behavior.

    Plus, the user already is showing that they're using a custom QueryBuilder call to grab the data, so why not explicitly use the join?

    For example:

    $cb = $this->getDoctrine()->getEntityManager()->getRepository(Province::class)
        ->createQueryBuilder('p')
        ->select('p, c')
        ->join('p.ubicacionpaisid', 'c')
        ->getQuery()
        ->getResult()
    ;
    

    or better yet, from with a ProvinceRepository:

    return $this->createQueryBuilder('p')
        ->select('p, c')
        ->join('p.ubicacionpaisid', 'c')
        ->getQuery()
        ->getResult()
    ;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探