教义对于一对多的关系提出了许多疑问

教义对于一对多的关系提出了许多疑问

本文介绍了教义对于一对多的关系提出了许多疑问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Symfony2应用程序与Doctrine作为ORM。在我的应用程序中,我有两个具有一对多关系的实体。一个实体是用户,许多实体是项目。他们的关系配置如下:



用户:

  oneToMany:
项目:
targetEntity:App \Bundle\ItemBundle\Entity\Item
fetch:EAGER
mappedBy:user
/ pre>

项目:

  manyToOne:
用户:
targetEntity:App \Bundle\UserBundle\Entity\User
fetch:EAGER
inversedBy:items
joinColumn:
name:user_id
referencedColumnName:id

问题是当我试图获取用户及其项目,Doctrine为每个用户创建单独的查询来获取它的项目。



查询本身很简单:

  $ entities = $ em-> getRepository('AppUserBundle:User' ) - > findBy([],[] ,30); 

是否可以使用Doctrine自动在一个查询中为用户提取所有相关项?



我使用的是SF 2.7.6和doctrine libs版本:

  doctrine / annotations v1.2.7 Docblock注释解析器
doctrine / cache v1.5.1缓存库提供一个ob ...
doctrine / collections v1.3.0集合抽象库
doctrine / common v2.5.1常见图书馆的教条...
doctrine / dbal v2.5.2数据库抽象层
doctrine / doctrine-bundle 1.6.0 Symfony DoctrineBundle
doctrine / doctrine-cache-bundle 1.2.1 Symfony Bundle for Doctrine Cache
doctrine / doctrine-migrations-bundle 1.1.1 Symfony DoctrineMigrationsBundle
doctrine / inflector v1.0.1常用字符串操作...
doctrine / instantiator 1.0.5一个小的,轻量级的实用程序...
doctrine / lexer v1.0.1用于词法分析器的基本库...
doctrine / migrations v1.1.0数据库模式迁移使用...
原则/ orm v2.5.1 PHP的对象关系映射器


解决方案

我不认为设置急切的提取是一个好主意,但是tbh。



这样的东西应该可以正常工作:

  $ users = $ this-> getEntityManager()
- > createQueryBuilder() - > select('u,i')//键是选择两个实体
- > from('YourBundle:User','u')
- > join('u.item','i')
- > getQuery() - >的getResult();


I have a Symfony2 app with Doctrine as ORM. In my app i have two entities with one-to-many relations. The one entity is User and the many entity is Item. Their relation config goes below:

User:

oneToMany:
            items:
                targetEntity: App\Bundle\ItemBundle\Entity\Item
                fetch: EAGER
                mappedBy: user

Item:

manyToOne:
        user:
            targetEntity: App\Bundle\UserBundle\Entity\User
            fetch: EAGER
            inversedBy: items
            joinColumn:
                name: user_id
                referencedColumnName: id

The problem is when i'm trying to get the list of Users and their Items, Doctrine creates separate query for each user to fetch it's items.

The query itself is quite simple:

    $entities = $em->getRepository('AppUserBundle:User')->findBy([], [], 30);

Is it possible to fetch all related items for users in one query automatically using Doctrine?

I'm using SF 2.7.6 and doctrine libs with versions:

doctrine/annotations                 v1.2.7  Docblock Annotations Parser
doctrine/cache                       v1.5.1  Caching library offering an ob...
doctrine/collections                 v1.3.0  Collections Abstraction library
doctrine/common                      v2.5.1  Common Library for Doctrine pr...
doctrine/dbal                        v2.5.2  Database Abstraction Layer
doctrine/doctrine-bundle             1.6.0   Symfony DoctrineBundle
doctrine/doctrine-cache-bundle       1.2.1   Symfony Bundle for Doctrine Cache
doctrine/doctrine-migrations-bundle  1.1.1   Symfony DoctrineMigrationsBundle
doctrine/inflector                   v1.0.1  Common String Manipulations wi...
doctrine/instantiator                1.0.5   A small, lightweight utility t...
doctrine/lexer                       v1.0.1  Base library for a lexer that ...
doctrine/migrations                  v1.1.0  Database Schema migrations usi...
doctrine/orm                         v2.5.1  Object-Relational-Mapper for PHP
解决方案

I don't think setting eager fetch is a great idea anyway tbh.

Something like this should work fine:

$users = $this->getEntityManager()
    ->createQueryBuilder()->select('u, i') // key is to select both entities
    ->from('YourBundle:User', 'u')
    ->join('u.item', 'i')
    ->getQuery()->getResult();

这篇关于教义对于一对多的关系提出了许多疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:48