douyun6781 2015-10-24 09:04
浏览 36
已采纳

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

I have a User-Entity with the very base information. The User should be able to share more detailed data in a profile. There are different ProfileSets for different Users.

In this example I try to keep it simple: ProfileSet_A is a profile with personal information, ProfileSet_B stores anonymous data.

A User can only have one ProfileSet.

Entities (Pseudocode)

// 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
}

Form, ProfileSet_A

username:            [ textfield ]
email:               [ textfield ]
firstname:           [ textfield ]
lastname:            [ textfield ]
morePrivateStuff:    [ textfield ]

Form, ProfileSet_B

username:            [ textfield ]
email:               [ textfield ]
anyAnonymousStuff:   [ textfield ]

Problems

  1. UserHasProfileSet should relate to User $user and a ProfileSet, which could be an instance of ProfileSet_A or ProfileSet_B. I'd like to have just the field $profileSet instead of $profileSet_A, $profileSet_B, ...

  2. I'd like to edit the User and it's ProfileSet (A or B) within the same form.

Question

How to solve the problems in a clean way? I'm open to best-practice alternatives. Maybe I'm thinking in a wrong way.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongpan3001 2015-11-05 12:40
    关注

    If you use Doctrine in your project you can archive this with the Doctrine inheritance, for example with 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
    {
        // ...
    }
    

    In this case Doctrine will create 3 tables: profile_sets which will contain common fields and the type of profile, profile_sets_a and profile_sets_b will contain specific fields. When you fetch your User with $profile property, Doctrine will automatically map the needed Object.

    As you have only one ProfileSet entry for each User it's not necessary to define extra UserHasProfileSet entity and you can just set OneToOne association directly. But you can do it the same way if you have to.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题