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.

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

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探