Doctrine2 one-to-one relation auto loads on query

前端 未结 6 1706
借酒劲吻你
借酒劲吻你 2020-12-23 12:53

I have a query that looks like this:

My user entity has a one-to-one relation that looks like this:

/**
 * @var UserProfile
 *
 * @ORM\\OneToOne(targ         


        
6条回答
  •  鱼传尺愫
    2020-12-23 13:31

    I spent a lot of time searching for a solution. For me, none of the options were satisfying enough, but maybe I can save someone some time with this list of workarounds:

    1) Change the owning side and inverse side http://developer.happyr.com/choose-owning-side-in-onetoone-relation - I don't think that's right from a DB design perspective every time.

    2) In functions like find, findAll, etc, the inverse side in OneToOne is joined automatically (it's always like fetch EAGER). But in DQL, it's not working like fetch EAGER and that costs the additional queries. Possible solution is every time to join with the inverse entity

    3) If an alternative result format (i.e. getArrayResult()) is sufficient for some use-cases, that could also avoid this problem.

    4) Change inverse side to be OneToMany - just looks wrong, maybe could be a temporary workaround.

    5) Force partial objects. No additional queries but also no lazy-loading: $query->setHint (Query::HINT_FORCE_PARTIAL_LOAD, true) - seams to me the only possible solution, but not without a price: Partial Objects are a little bit risky, because your entity behavior is not normal. For example if you not specify in ->select() all associations that you will user you can have an error because your object will not be full, all not specifically selected associations will be null

    6) Not mapping the inverse bi-directional OneToOne association and either use an explicit service or a more active record approach - https://github.com/doctrine/doctrine2/pull/970#issuecomment-38383961 - And it looks like Doctrine closed the issue

提交回复
热议问题