Primary key of owning side as a join column

后端 未结 4 722
借酒劲吻你
借酒劲吻你 2020-12-02 15:06

NOTE: Topic is lengthy but detailed and may come in handy if you use Doctrine2 and oneToOne relationships.

Recently I came across a problem in Doctrine:

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-02 15:35

    I came across this same problem and remember that the symblog tutorial gave an example of how to reduce the lazy loading by explicitly add left joins on the tables that you do not need. It seems strange to include tables on a join when you do not even want that data at all, but this way you will reduce all of those extra queries down to 1 and it does run faster.

    Search for lazy loading - about 1/5 of the way down http://tutorial.symblog.co.uk/docs/customising-the-view-more-with-twig.html

    To fix this for the user/userdata issue try adding this to the user repository and use to whenever you need to get all users even if you do not want userdata. It can be further enhanced by selecting partial: ->select('partial p.{user_id,name,}')

       public function getAll($limit = 500) {
           $qb = $this->createQueryBuilder('u')
                ->select('u', 'd')
                ->leftJoin('p.userdata', 'd')
           if (false === is_null($limit))
               $qb->setMaxResults($limit);
         return $qb->getQuery()->getResult();
        }
    

提交回复
热议问题