为了最好地解释,他们是4个实体:
Person [id, address, :person_name_id]
PersonName [id, text_value]
Trait [id, :person_id, :traitname_id]
TraitName [id, text_value]
我能够获得我想要的数组结果的唯一方法是使用php循环并更新各个部分。 我真的想用doctrine / mysql来实现这个目的吗?
我已尝试选择'job_name.text_value as name'
和'trait_name.text_value as name'
(或特征名称),但这会导致只返回一个特征,并且人名不会放在现有的根元素中数组。
[
id => 1,
address => 'xyz lane',
name => 'bob',
traits => [
[
id => 1,
name => 'eats bananas'
],
[
id => 2,
name => 'likes fishing'
]
]
]
[
id => 1,
address => 'xyz lane',
person_name => [
id => 1,
text_value => 'bob'
]
traits => [
[
id => 1,
trait_name => [
Id => 1,
text_value => 'eats bananas'
]
],
[
id => 2,
trait_name => [
person_name => [
id => 2,
text_value => 'likes fishing'
]
]
]
]
$q = $this->createQueryBuilder('person')
->select(
'person',
'person_name',
'person_traits',
'trait_name'
)
->from('AppBundle:Person','person')
->innerJoin('person.name','person_name')
->leftJoin('person.traits','person_traits')
->leftJoin('person_traits.trait_name', 'trait_name')
->where('person.id = :id')
->setParameter('id', 1);
return $q->getQuery()->getSingleResult(Query::HYDRATE_ARRAY);