Doctrine2 gives me only first instance of related objects

爷,独闯天下 提交于 2019-12-25 04:05:31

问题


I am using QueryBuilder to get 10 biggest cities with states.

$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();

generated 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 states s1_ ON c0_.state_id = s1_.id AND (c0_.state_id = s1_.id) ORDER BY c0_.population DESC LIMIT 10

When I testiing that query in PHPMyAdmin every city has own state, but in app all cities in the array has association with state of first city.

Could somebody explain for me Doctrine2 behaviour in this case?

[EDIT]

Schema:

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: {  }

I try different schema option (ManyToOne etc) but with no luck

Screenshoot from PHPMyAdmin, mazowieckie etc are names of states. In my app all 10 cities has state->name mazowieckie.

Screenshoots from app look:

I outputting state name in loop: {{ city.state.name }}

来源:https://stackoverflow.com/questions/10450707/doctrine2-gives-me-only-first-instance-of-related-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!