Symfony:具有灵活关系的实体及其(子)表单-如何设计?

B先生

我有一个User带有基本信息-Entity。User应该能够在配置文件共享更详细的数据。ProfileSets不同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 ]

问题

  1. UserHasProfileSet应该与User $user有关ProfileSet,可以是ProfileSet_A或的一个实例ProfileSet_B我想刚才的领域$profileSet,而不是$profileSet_A$profileSet_B...

  2. 我想在同一表格中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将包含特定字段。当您获取Userwith$profile属性时,Doctrine将自动映射所需的对象。

由于ProfileSet每个条目只有一个条目,User因此不必定义额外的UserHasProfileSet实体,您可以直接设置OneToOne关联。但是,如果需要,您可以用相同的方法进行操作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章