Symfony2:Relationship中右DQL的方法是递归的
数据结构的简单视图: 我的\ExampleBundle\Entity\Parent:Symfony2:Relationship中右DQL的方法是递归的,symfony,doctrine-orm,symfony-2.3,dql,Symfony,Doctrine Orm,Symfony 2.3,Dql,数据结构的简单视图: 我的\ExampleBundle\Entity\Parent: oneToMany: children: targetEntity: Children mappedBy : parent cascade : ["persist", "remove"] manyToMany: friends: targetEntity : Friend inversedBy : pa
oneToMany:
children:
targetEntity: Children
mappedBy : parent
cascade : ["persist", "remove"]
manyToMany:
friends:
targetEntity : Friend
inversedBy : parents
cascade : ["persist"]
joinTable :
name : my_parents_and_friends
joinColumns :
joinColum:
name : parent_id
referencedColumnName: id
onDelete : CASCADE
inverseJoinColumns:
joinColum:
name : friend_id
referencedColumnName: id
onDelete : CASCADE
我的\ExampleBundle\Entity\Children:
manyToOne:
parent:
targetEntity: Parent
inversedBy : children
joinColumn :
name : parent_id
referencedColumnName: id
manyToMany:
friends:
targetEntity : Friend
inversedBy : children
cascade : ["persist"]
joinTable :
name : my_children_and_friends
joinColumns :
joinColum:
name : children_id
referencedColumnName: id
onDelete : CASCADE
inverseJoinColumns:
joinColum:
name : friends_id
referencedColumnName: id
onDelete : CASCADE
我的\ExampleBundle\Entity\Friend:
manyToOne:
school:
targetEntity: My\SchoolBundle\Entity\School
inversedBy : friends
joinColumn :
name : school
referencedColumnName: id
manyToMany:
parents:
targetEntity: Parent
mappedBy : friends
children:
targetEntity: Children
mappedBy : friends
我的\SchoolBundle\Entity\School:
oneToMany:
friends:
targetEntity: My\ExampleBundle\Entity\Friend
mappedBy : school
cascade : ["persist", "remove"]
所以这是我通常的提问方式
public function findOneParent($id)
{
$query = $this->getEntityManager()->createQuery('
SELECT
p,
pf, pfg, pfsch,
ch, chg,
chf, chfg, chfsch
FROM MyExampleBundle:Parent p
LEFT JOIN p.friends pf
WITH //...
LEFT JOIN pf.groups pfg
LEFT JOIN pf.school pfsch
LEFT JOIN p.children ch
WITH //...
LEFT JOIN ch.groups chg
LEFT JOIN ch.friends chf
WITH //...
LEFT JOIN chf.groups chfg
LEFT JOIN chf.school chfsch
WHERE p.id = :id
')
->setParameter('id', $id);
return $this->try_catch($query);
}
_
_
使用关系获取父AAA的数据:
parent AAA
|- children BBB
| |- friends CCC
|
|- friends CCC
_
“家长、孩子、朋友、团体”的数据似乎是重复的
{{ parent.name }}
{% for child in parent.children %}
{{ child.name }}
{% for friend in child.friends %}
{{ friend.name }}
{% for group in friend.groups %}
{{ group.name }} // = echo twice same value ...??
{% endfor %}
{% endfor %}
{% endfor %}
我很困惑。我的DQL错了吗 任何帮助或想法都将不胜感激
编辑:例如: 朋友CCC先生 CCC先生是“第一”组和“第二”组的成员 现在: 应该是这样的:
{{ parent.name }}
{% for child in parent.children %}
{{ child.name }}
{% for friend in child.friends %}
{{ friend.name }} // friend = Mr. CCC
{% for group in friend.groups %} // {{ friend.groups | length }} = 2
{{ group.name }} // = echo : first second
{% endfor %}
{% endfor %}
{% endfor %}
{% for friend in parent.friends %}
{{ friend.name }} // friend = Mr. CCC
{% for group in friend.groups %} // {{ friend.groups | length }} = 2
{{ group.name }} // = echo : first second
{% endfor %}
{% endfor %}
不确定您的问题,{{friend.groups}}包含两个组而不是一个组?您期望得到什么结果?我添加了上面一个示例的描述。好的,您是否尝试执行生成的本机sql查询,她返回了什么?…呃…它与实际的DQL不同,因为为了便于解释,它被简化了。这是太多其他相关的。然而,既然问题是立即获取数据?孩子和父母的朋友不能分开对待吗?我编辑了上面一个例子的描述。
{{ parent.name }}
{% for child in parent.children %}
{{ child.name }}
{% for friend in child.friends %}
{{ friend.name }} // friend = Mr. CCC
{% for group in friend.groups %} // {{ friend.groups | length }} = 4...why?
{{ group.name }} // = echo : first first second second
{% endfor %}
{% endfor %}
{% endfor %}
{% for friend in parent.friends %}
{{ friend.name }} // friend = Mr. CCC
{% for group in friend.groups %} // {{ friend.groups | length }} = 4...why?
{{ group.name }} // = echo : first first second second
{% endfor %}
{% endfor %}
{{ parent.name }}
{% for child in parent.children %}
{{ child.name }}
{% for friend in child.friends %}
{{ friend.name }} // friend = Mr. CCC
{% for group in friend.groups %} // {{ friend.groups | length }} = 2
{{ group.name }} // = echo : first second
{% endfor %}
{% endfor %}
{% endfor %}
{% for friend in parent.friends %}
{{ friend.name }} // friend = Mr. CCC
{% for group in friend.groups %} // {{ friend.groups | length }} = 2
{{ group.name }} // = echo : first second
{% endfor %}
{% endfor %}