ORM-ManyToMany 关系中的附加字段
Additional fields in ORM-ManyToMany relation
我有以下 table 数据:用户和组。在我的数据库中,这两个实体之间的关系是 ManyToMany,因为许多用户可以在许多组中,而许多组可以有许多用户。这很简单。我创建了这段代码并且它有效:
用户实体:
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="Group", inversedBy="users")
* @ORM\JoinTable(name="users_in_groups")
*/
protected $groups;
}
组实体:
/**
* @ORM\Entity
* @ORM\Table(name="groups")
*/
class Group
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="groups")
*/
protected $users;
}
Symfony 创建了三个 table:users
、groups
和 users_in_groups
。一切都很好,但我想为每个组中的不同用户设置不同的权限。例如:User1 在 Group1 中是管理员,在 Group2 中是标准成员,等等。最好的解决方案是向 users_in_groups
插入新字段,但是这个 table 是由 @ORM\JoinTable 函数创建的,我不知道我不知道我该怎么做。你知道这个问题的简单解决方案吗?
看看documentation on ManyToMany relationships。
Why are many-to-many associations less common? Because frequently you want to associate additional attributes with an association, in which case you introduce an association class. Consequently, the direct many-to-many association disappears and is replaced by one-to-many/many-to-one associations between the 3 participating classes.
所以换句话说,一旦您想要关系 table 上的属性,它就不再只是关系 table。它需要它自己的实体。该新实体现在与用户和组建立 one-to-many/many-to-one 关系。
然后您可以在新实体中使用 Composite and Foreign Keys as Primary Key 功能。
过去关于这个话题的一些讨论:https://groups.google.com/forum/#!topic/doctrine-user/0dh8lgUudvc
我发现最简单的方法是使用第三方实体
以下是它在您的情况下的工作方式
用户实体:
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="UserGroup", mappedBy="user")
* */
protected $usergroup;
}
组实体:
/**
* @ORM\Entity
* @ORM\Table(name="groups")
*/
class Group
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="UserGroup" , mappedBy="group"})
* */
protected $usergroup;
}
用户组实体:
/**
* @ORM\Entity
* @ORM\Table(name="user_group")
*/
class UserGroup
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*
* @var integer $id
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="usergroup")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
* */
protected $user;
/**
* @ORM\ManyToOne(targetEntity="Group", inversedBy="usergroup")
* @ORM\JoinColumn(name="group_id", referencedColumnName="id")
* */
protected $group;
// Additional fields, Getter, Setters, _Construct, __toString
}
我有以下 table 数据:用户和组。在我的数据库中,这两个实体之间的关系是 ManyToMany,因为许多用户可以在许多组中,而许多组可以有许多用户。这很简单。我创建了这段代码并且它有效:
用户实体:
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="Group", inversedBy="users")
* @ORM\JoinTable(name="users_in_groups")
*/
protected $groups;
}
组实体:
/**
* @ORM\Entity
* @ORM\Table(name="groups")
*/
class Group
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="groups")
*/
protected $users;
}
Symfony 创建了三个 table:users
、groups
和 users_in_groups
。一切都很好,但我想为每个组中的不同用户设置不同的权限。例如:User1 在 Group1 中是管理员,在 Group2 中是标准成员,等等。最好的解决方案是向 users_in_groups
插入新字段,但是这个 table 是由 @ORM\JoinTable 函数创建的,我不知道我不知道我该怎么做。你知道这个问题的简单解决方案吗?
看看documentation on ManyToMany relationships。
Why are many-to-many associations less common? Because frequently you want to associate additional attributes with an association, in which case you introduce an association class. Consequently, the direct many-to-many association disappears and is replaced by one-to-many/many-to-one associations between the 3 participating classes.
所以换句话说,一旦您想要关系 table 上的属性,它就不再只是关系 table。它需要它自己的实体。该新实体现在与用户和组建立 one-to-many/many-to-one 关系。
然后您可以在新实体中使用 Composite and Foreign Keys as Primary Key 功能。
过去关于这个话题的一些讨论:https://groups.google.com/forum/#!topic/doctrine-user/0dh8lgUudvc
我发现最简单的方法是使用第三方实体
以下是它在您的情况下的工作方式
用户实体:
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="UserGroup", mappedBy="user")
* */
protected $usergroup;
}
组实体:
/**
* @ORM\Entity
* @ORM\Table(name="groups")
*/
class Group
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="UserGroup" , mappedBy="group"})
* */
protected $usergroup;
}
用户组实体:
/**
* @ORM\Entity
* @ORM\Table(name="user_group")
*/
class UserGroup
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*
* @var integer $id
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="usergroup")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
* */
protected $user;
/**
* @ORM\ManyToOne(targetEntity="Group", inversedBy="usergroup")
* @ORM\JoinColumn(name="group_id", referencedColumnName="id")
* */
protected $group;
// Additional fields, Getter, Setters, _Construct, __toString
}