问题
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