Doctrine2只给出了相关对象的第一个实例

时间:2012-05-04 14:21:38

标签: symfony doctrine-orm one-to-many

我正在使用QueryBuilder来获得10个最大的城市。

$query = $em->createQueryBuilder()
            ->select('c','s')
            ->from('CitiesBundle:Cities', 'c')
            ->innerJoin('c.state', 's','WITH', 'c.state = s')
            ->orderBy('c.population', 'DESC')
            ->setMaxResults(10)
            ->getQuery();

生成SQL:

  

SELECT c0_.id AS id0,c0_.name AS name1,c0_.landarea AS landarea2,   c0_.density AS density3,c0_.population AS population4,s1_.id AS id5,   s1_.name AS name6,c0_.state_id AS state_id7 FROM cities c0_ INNER   JOIN状态s1_ ON c0_.state_id = s1_.id AND(c0_.state_id = s1_.id)   ORDER BY c0_.population DESC LIMIT 10

当我在PHPMyAdmin中查询该查询时,每个城市都有自己的状态,但在应用程序中,阵列中的所有城市都与第一个城市的状态相关联。

在这种情况下,有人能为我解释Doctrine2的行为吗?

[编辑]

架构:

XYZ\Bundle\CitiesBundle\Entity\Cities:
  type: entity
  table: cities
  fields:
  #fields
  oneToMany:
    state:
      targetEntity: States
      cascade: {  }
      mappedBy: null
      inversedBy: null
      joinColumns:
        state_id:
          referencedColumnName: id
      orphanRemoval: false
  lifecycleCallbacks: {  }


XYZ\Bundle\CitiesBundle\Entity\States:
  type: entity
  table: states
  fields:
    id:
      id: true
      type: boolean
      nullable: false
      generator:
        strategy: IDENTITY
    name:
      type: string
      length: 50
      fixed: false
      nullable: false
  lifecycleCallbacks: {  }

我尝试了不同的架构选项(ManyToOne等),但没有运气

来自PHPMyAdmin的屏幕截图,mazowieckie等是状态的名称。在我的应用程序中,所有10个城市都有州名>名称mazowieckie。

enter image description here

来自应用程序外观的屏幕截图:

enter image description here

我在循环中输出州名:{{city.state.name}}

0 个答案:

没有答案