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 ?

图片转代码服务由CSDN问答提供 功能建议

我有两个实体,国家和省,我在省内建立了多对一的关系 实体:

实体/省

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

在这里,我使用省实体获得所有结果:

  $ cb = $ this-> getDoctrine()
  - &gt  ; getEntityManager()
  - > getRepository(省::类)
  - > createQueryBuilder('a'); 
   
 
 

但是,如果我运行 :

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

它返回:

  string(41)“SELECT a FROM AppBundle \ Entity \ Province a”
   
 
 

什么 我希望看到的是一个将Country实体连接到SQL中的省实体的查询。

我是什么意思 唱歌?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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()
    ;
    
    点赞 打赏 评论
  • dtyw10299 2017-08-16 11:31

    This might be because Doctrine has 2 types of joins. A normal join and a fetch join. In the fetch join, Doctrine will preform 2 different queries and then just hydrate the result of the second back into the result/data of the first.

    It's sometimes also referred to as a lazy join. Lazy joins are the default behavior.
    You can change this behavior by altering your relation with fetch="EAGER"

    /**
     * @var \AppBundle\Entity\Country
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Country", fetch="EAGER")
     * @ORM\JoinColumn(name="ubicacionpaisid", referencedColumnName="id")
     *
     */
    
    点赞 打赏 评论
  • doufu2496 2017-08-16 11:44

    As DarkMukke said...

    Did you try to fetch the data?

    Adding in your relation fetch="EAGER"

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

    This should fetch and load the associated entity as well.

    http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#manytoone

    点赞 打赏 评论

相关推荐 更多相似问题