Orm Symfony2原则许多人依赖于业主
我有一个用户名和姓氏的用户对象。用户可以设置为重影。若用户是ghost,我想让其他用户可以随意命名他Orm Symfony2原则许多人依赖于业主,orm,doctrine,Orm,Doctrine,我有一个用户名和姓氏的用户对象。用户可以设置为重影。若用户是ghost,我想让其他用户可以随意命名他 |User | |Representation| |-------| |--------------| |ID | |owner | |isGhost| |ghost | |name | |name | |surname| |surname | 所有者和重影指用户 owner始终是登录用户 我
|User | |Representation|
|-------| |--------------|
|ID | |owner |
|isGhost| |ghost |
|name | |name |
|surname| |surname |
所有者
和重影
指用户
owner
始终是登录用户
我想要实现的是-如果用户不是ghost-(作为所有者),我想看到User.name
和User.姓氏
,但是如果用户是ghost-(作为所有者),我想看到Representation.name
和Representation.姓氏
如何使这种情况的映射最有效
编辑:
还有什么-我有一个关系对象,它引用User,并使用getUser()
关联,我希望根据用户是否为ghost来查看正确的名称和姓氏
是否可以使用ArrayCollection而不是其他表,并在ghost User中保存所有表示?预测每个ghost用户的最大表示数约为5-10。不多。您应该像设计两个表一样继续:在ghost上创建一个OneToMany
关系bw User和Representation,以及另一个OneToMany
关系bw User和Owner
确保拥有方是用户,因为在查询用户时不希望每次加载表示对象
然后,只执行一个->getRepresentations()
,在必要时为用户获取关联的表示,例如:when true==isGhost
问题是,如果一个用户是ghost,并且没有所有者的表示,您会显示什么
您可以执行以下功能:
public function getUsername($ownerRepresentation = false)
{
// user is ghost mode, but we do not have an owner for its representation
if ($this->isGhost && false === $ownerRepresentation) {
return '~';
}
// user is ghost, and we have provided an owner to search for his representation
if ($this->isGhost && false !== $ownerRepresentation) {
$representations = $this->getRepresentations();
foreach ($representations as $representation) {
if ($representation->getOwner()->getId() === $ownerRepresentation->getId()) {
return $representation->getName() . ' ' . $representation->getSurname();
}
}
// no representation found for this owner
return '~';
}
// user is no ghost, we can freely display his real name
return $this->getName() . ' ' . $this->getSurname();
}