我开始在symfony2中建模db / entities。 我有两个实体。第一个是用户,第二个是组。 我想连接两者。我想我应该使用多对多关系(?)。
但我的主要问题是,如果用户加入此群组,我如何获得包含信息的所有群组的列表。
集团实体:
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="GroupBundle\Entity\Group">
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<field name="name" type="string" column="name" length="50"/>
<field name="description" type="string" column="description"/>
<many-to-many field="users" mapped-by="groups" target-entity="AccountBundle\Entity\User"/>
</entity>
</doctrine-mapping>
和用户实体:
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="AccountBundle\Entity\User">
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<field name="username" type="string" column="username" length="255"/>
<field name="password" type="string" column="password" length="255"/>
<field name="salt" type="string" column="salt" length="255"/>
<field name="email" type="string" column="email" length="255"/>
<field name="active" type="boolean" column="active"></field>
<field name="token" type="string" column="token" length="255"/>
<field name="lastLoginTime" type="datetime" column="lastLoginTime" nullable="true"/>
<field name="registerTime" type="datetime" column="registerTime"/>
<one-to-many field="events" target-entity="CoreBundle\Entity\Event" mapped-by="user" />
<many-to-many field="groups" inversed-by="users" target-entity="GroupBundle\Entity\Group">
<join-table name="UserGroups">
<join-columns>
<join-column name="userId" referenced-column-name="id" />
</join-columns>
<inverse-join-columns>
<join-column name="groupId" referenced-column-name="id" />
</inverse-join-columns>
</join-table>
</many-to-many>
</entity>
</doctrine-mapping>
之间没有关系,因为我不知道什么是最好的。 也许我必须在UserGroup之间创建额外的实体?
D4V1D你有权利。假设是:用户可以加入很多组,组可以有很多用户。
Okey我添加了多对多关系。我希望我做得对。
现在。这是我的控制器的片段:
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository("GroupBundle\Entity\Group");
$groups = $repo->findAll();
$user = $this->getUser();
$user_groups = $user->getGroups();
foreach($user_groups as $user_group){
/* @var $group Group */
foreach($groups as $group){
if($group->getId() == $user_group->getId()){
$group->setUserInGroup(true); // of course i extend my entity file about extra set and get method.
}
}
}
return $this->render('GroupBundle:showAll.html.twig', array('groups' => $groups));
并查看:
{% for group in groups %}
<div>
{{ group.name }}
{% if(group.getUserInGroup()) %}
<a href="#">join</a>
{% endif %}
</div>
{% endfor %}
我尝试找到最好的方法来做到这一点。
答案 0 :(得分:0)
如果没有OneToMany与用户和组的关系,您如何加入该组与用户?哪个领域有此信息?你需要一个关系是的。
集团实体:
<many-to-many field="users" target-entity="AccountBundle\Entity\User" mapped-by="groups" />
用户实体:
<many-to-many field="groups" target-entity="GroupBundle\Entity\Group" inversed-by="users"/>