连接元数据损害表Getter / Setters - Doctrine 2 [英] Joining-Table with Metadata Impairs Getters/Setters - Doctrine 2
问题描述
示例实体定义
现在的挑战是 getGroups
和 setGroups
不要生成/设置组
实体(&同样从组
实例透视图),但是它们会生成 GroupUser
实体。
这将加大管理这种关系的延迟,到目前为止,这是非常顺利的 - 例如,我不能简单地添加,删除或检查组
是否存在 getGroups
收益率。
任何人都可以在我的执行过程中识别任何错误,还是推荐一个更流畅的方式来实现这个概念?
编辑:
我的主要关注点是:使用此实现,从组实体检索用户集合需要此实体方法的中介:
public function getUsers(){
return $ this-> users-> map(function($ groupUser){
return $ groupUser-> getUser();
} );
}
我担心这可能意味着重大的表现。我不正确吗?
此外,如何重新实现 setUsers
方法?
组实体
;?php
/ **
* @Entity
* @Table(name =group)
* /
class组{
/ **
* @Column(type =integer,nullable = false)
* @Id
* /
protected $ id = null;
/ **
* @OneToMany(targetEntity =GroupUser,mappedBy =group)
* @var \Doctrine\Common\Collections\Collection
* /
protected $ users;
}
用户实体:
<?php
/ **
* @Entity
* @Table(name = user)
* /
class User {
/ **
* @Column(type =integer,nullable = false)
* @Id
* /
protected $ id = null;
/ **
* @OneToMany(targetEntity =GroupUser,mappedBy =user)
* @var \Doctrine\Common\Collections\Collection
* /
protected $ groups;
}
加入实体:
<?php
/ **
* @Entity
* @Table(name =group_user
* /
class GroupUser {
/ **
* @Id
* @ManyToOne(targetEntity =User,inversedBy =groups )
* @JoinColumn(name =userId,referencedColumnName =id)
* /
protected $ user;
/ **
* @Id
* @ManyToOne(targetEntity =Group,inversedBy =users)
* @JoinColumn(name =groupId ,referencedColumnName =id)
* /
protected $ group;
/ **
* @Column(type =integer)
* /
protected $ relationship;
}
相关 -
- 相同的目标,略有不同的方法,一旦产生的集合被操纵,它们一直产生错误: http://www.doctrine-project.org/jira/browse/DDC-1323
- 支持方法,没有技术细节:原则2连接表+额外字段
我已经找到了这个特定类型的实体定义的两个例子(见问题)关系,然而没有使用它们的示例代码。因此,如何流动(或以其他方式)所得到的设定者和吸气者可能会是。希望这段代码有助于清除任何人进行类似尝试的方法。
在这种情况下,理想的解决方案(感谢#doctrine @ freenode)是实现一个自定义存储库 - 更灵活&高效的地方创造&
使用元数据类加入表的自定义存储库 - 解决方案伴随原始问题的代码
<?php
使用Doctrine\ORM\EntityRepository;
class GroupUserRepository扩展EntityRepository {
/ **
* @param \User $ user
* @param \Group $ group
* @ param integer $ type由GroupUser
* @param string定义的整数类常量之一string $ role定义用户在组中角色的可选字符串。
* @return \GroupUser
* /
public function addUserToGroup(User $ user,Group $ group,$ relationship,$ role =''){
$ groupUser = $ this-> findOneBy(array('user'=> $ user-> getId(),'group'=> $ group-> getId()));
if(!$ groupUser){
$ groupUser = new GroupUser();
$ groupUser-> setGroup($ group);
$ groupUser-> setUser($ user);
$ groupUser-> setRole($ role);
$ groupUser-> setRelationship($ relationship);
$ this-> _em-> persist($ groupUser);
}
return $ groupUser;
}
/ **
* @param \User $ user
* @param \Group $ group
* @return null
* /
public function removeUserFromGroup(User $ user,Group $ group){
$ groupUser = $ this-> findOneBy(array('user'=> $ user-> getId ),'group'=> $ group-> getId()));
if($ groupUser)
$ this-> _em-> remove($ groupUser);
}
}
然后,从join-table类修改实体元数据相应地到指定自定义存储库。
<?php
/ **
* @Entity(repositoryClass =\Path\To\GroupUserRepository)
* /
class GroupUser {
// ...
}
这导致自定义存储库取代默认存储库, 来自实体类的代理方法简单。
<?php
/ **
* @Entity
* /
class Group {
/ **
* @param \User $ user
* @param integer $ relationship One由GroupUser
* @param string定义的整数类常量的定义字符串$ role组中定义用户角色的可选字符串。
* @return \GroupUser
* /
public function addUser(User $ user,$ relationship,$ role =''){
return $ this-> _em- > getRepository('GroupUser')
- > addUserToGroup($ user,$ this,$ relationship,$ role);
}
}
事情与以前一样可以管理。
I'm writing a feature which calls for the records of my joining table to carry extra metadata (Joining-Table with Metadata). I've attempted to implement this in accordance with this section of the Doctrine documentation.
See below for example Entity definitions.
The challenge now is that getGroups
and setGroups
do not yield/set Group
entities (& the same is true from the Group
instance perspective), but they yield GroupUser
entities.
This adds a substantial delay to process of managing this relationships, which so far have been extremely smooth - for example, I cannot simply add, remove, or check for the existence of a Group
to the collection which getGroups
yields.
Can anyone identity any errors in my implementation, or else recommend a more fluid way of implementing this concept?
Thanks in advance for any input.
EDIT:
My main concern is this: using this implementation, retrieving a collection of Users from a Group entity requires this Entity method's mediation:
public function getUsers() {
return $this->users->map(function($groupUser){
return $groupUser->getUser();
});
}
I'm concerned that this could imply a major performance hit down the road. Am I incorrect?
Furthermore, how does one re-implement the setUsers
method?
Group entity:
<?php
/**
* @Entity
* @Table(name="group")
*/
class Group {
/**
* @Column(type="integer", nullable=false)
* @Id
*/
protected $id = null;
/**
* @OneToMany(targetEntity="GroupUser", mappedBy="group")
* @var \Doctrine\Common\Collections\Collection
*/
protected $users;
}
User entity:
<?php
/**
* @Entity
* @Table(name="user")
*/
class User {
/**
* @Column(type="integer", nullable=false)
* @Id
*/
protected $id = null;
/**
* @OneToMany(targetEntity="GroupUser", mappedBy="user")
* @var \Doctrine\Common\Collections\Collection
*/
protected $groups;
}
Joining entity:
<?php
/**
* @Entity
* @Table(name="group_user")
*/
class GroupUser {
/**
* @Id
* @ManyToOne(targetEntity="User", inversedBy="groups")
* @JoinColumn(name="userId", referencedColumnName="id")
*/
protected $user;
/**
* @Id
* @ManyToOne(targetEntity="Group", inversedBy="users")
* @JoinColumn(name="groupId", referencedColumnName="id")
*/
protected $group;
/**
* @Column(type="integer")
*/
protected $relationship;
}
Related -
- Same goal, slightly different approach, which consistently produced errors once the resulting collections were manipulated: http://www.doctrine-project.org/jira/browse/DDC-1323
- Supports the approach, no technical details: Doctrine 2 join table + extra fields
I've found just two examples (see question) of entity definitions for this specific type of relationship, however no example code for how they're used. As such it was fairly unclear how fluid (or otherwise) the resulting setters & getters could be expected to be. Hopefully this code will help clear up the approach for anyone else making a similar attempt.
The ideal solution under the circumstances (thanks #doctrine @ freenode) was to implement a custom repository - a more flexible & efficient place for creating & managing the association.
Example Custom Repository for Join-Table with Metadata Class - Solution accompanies code in original question
<?php
use Doctrine\ORM\EntityRepository;
class GroupUserRepository extends EntityRepository {
/**
* @param \User $user
* @param \Group $group
* @param integer $type One of the integer class constants defined by GroupUser
* @param string $role Optional string defining user's role in the group.
* @return \GroupUser
*/
public function addUserToGroup(User $user, Group $group, $relationship, $role = '') {
$groupUser = $this->findOneBy(array('user' => $user->getId(), 'group' => $group->getId()));
if(!$groupUser) {
$groupUser = new GroupUser();
$groupUser->setGroup($group);
$groupUser->setUser($user);
$groupUser->setRole($role);
$groupUser->setRelationship($relationship);
$this->_em->persist($groupUser);
}
return $groupUser;
}
/**
* @param \User $user
* @param \Group $group
* @return null
*/
public function removeUserFromGroup(User $user, Group $group) {
$groupUser = $this->findOneBy(array('user' => $user->getId(), 'group' => $group->getId()));
if($groupUser)
$this->_em->remove($groupUser);
}
}
Then, from the join-table class, modify the Entity meta-data accordingly to specify the custom repository.
<?php
/**
* @Entity(repositoryClass="\Path\To\GroupUserRepository")
*/
class GroupUser {
// ...
}
This causes the custom repository to yield in place of the default one, making a proxy method from the Entity class simple.
<?php
/**
* @Entity
*/
class Group {
/**
* @param \User $user
* @param integer $relationship One of the integer class constants defined by GroupUser
* @param string $role Optional string defining user's role in the group.
* @return \GroupUser
*/
public function addUser(User $user, $relationship, $role = '') {
return $this->_em->getRepository('GroupUser')
->addUserToGroup($user, $this, $relationship, $role);
}
}
And things are about as manageable as they were before.
这篇关于连接元数据损害表Getter / Setters - Doctrine 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!