我有一个User
带有基本信息的-Entity。本User
应该能够在配置文件共享更详细的数据。的ProfileSet
s不同User
。
在此示例中,我尝试使其简单:ProfileSet_A
是包含个人信息的配置文件,用于ProfileSet_B
存储匿名数据。
一个User
只能有一个ProfileSet
。
实体(伪代码)
// table: users
class User {
protected $id,
$email,
$username,
$password; // ...
}
// table: - none -
class ProfileSet {
protected $id,
$name; // ...
}
// table: profileset_a
class ProfileSet_A extends ProfileSet {
protected $firstname,
$lastname,
$morePrivateStuff; // ...
}
// table: profileset_b
class ProfileSet_B extends ProfileSet {
protected $anyAnonymousStuff; // ...
}
// table: user_has_profileset
class UserHasProfileSet {
protected $user, // relation to User
$profileSet; // relation to ProfileSet_A OR ProfileSet_B
}
表单,ProfileSet_A
username: [ textfield ]
email: [ textfield ]
firstname: [ textfield ]
lastname: [ textfield ]
morePrivateStuff: [ textfield ]
表单,ProfileSet_B
username: [ textfield ]
email: [ textfield ]
anyAnonymousStuff: [ textfield ]
问题
UserHasProfileSet
应该与User $user
和有关ProfileSet
,可以是ProfileSet_A
或的一个实例ProfileSet_B
。我想刚才的领域$profileSet
,而不是$profileSet_A
,$profileSet_B
...
我想在同一表格中User
对其进行编辑ProfileSet
(A或B)。
问题
如何彻底解决问题?我愿意接受最佳做法的替代方案。也许我想的是错误的方式。
提前致谢!
如果Doctrine
在项目中使用,则可以使用Doctrine继承将其存档,例如MappedSuperClass
:
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User
{
// other fields
/**
* @ORM\OneToOne(targetEntity="ProfileSet")
*/
private $profile;
}
/**
* @ORM\Entity
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="type", type="string")
* @ORM\DiscriminatorMap({"a" = "ProfileSet_A", "b" = "ProfileSet_B"})
* @ORM\Table(name="profile_sets")
*/
class ProfileSet
{
// common properties
}
/**
* @ORM\Entity
* @ORM\Table(name="profile_sets_a")
*/
class ProfileSetA extends ProfileSet
{
// ...
}
/**
* @ORM\Entity
* @ORM\Table(name="profile_sets_b")
*/
class ProfileSetB extends ProfileSet
{
// ...
}
在这种情况下教义将创建3代表:profile_sets
其中将包含公共字段和配置文件的类型,profile_sets_a
以及profile_sets_b
将包含特定字段。当您获取User
with$profile
属性时,Doctrine将自动映射所需的对象。
由于ProfileSet
每个条目只有一个条目,User
因此不必定义额外的UserHasProfileSet
实体,您可以直接设置OneToOne
关联。但是,如果需要,您可以用相同的方法进行操作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句