doubiantiao4994 2015-01-07 21:06
浏览 63
已采纳

如何在Symfony2中处理实体的主/辅助/正常状态

I am developing an application and I came across the following: Lets say I have an entity called Contact, that Contact belongs to a Company and the Company has a Primary Contact and a Secondary Contact and also has the remaining Contacts which I've named Normal.

My question is, what is the best approach for this when talking about entities properties and also form handling. I've though about two things:

  1. Having 2 fields on the Company entity called PrimaryContact and SecondaryContact and also have a one-to-many relationship to a property called contacts.

What I don't like (or I'm not 100% how to do) about this option is that on the Contact entity I would need an inversedBy field for each of the 2 one-to-one properties and also 1 for the one-to-many relationship and my personal thought is that this is kind of messy for the purpose.

  1. Having a property on the Contact entity called Type which would hold if it's primary, secondary or normal and in the Company methods that has to do with Contacts I would modify it and add the getPrimaryContact, getSecondaryContact, etc.

What I don't like about this option is that I would need to have 2 unmapped properties for the Company and I would need to do a lot on the form types in order to get this to work smoothly.

My question is what is the best approach for this structure and how to deal with forms and these dependencies. Let me know if this is not clear enough and I will take time and preparate an example with code and images.

  • 写回答

2条回答 默认 最新

  • dongliyan7318 2015-01-08 17:43
    关注

    Thanks to @Cerad this is the following approach I took:

    1. I have a OneToMany property on the Company to hold all the contacts.
    2. Implemented the getPrimaryContact/setPrimaryContact methods and looped through all the contacts and retrieving the one of the type I want. Did the same for the secondary.
    3. On the Form type of the company my issue was that I had the 'mapped' => 'false' option, I removed this since I implemented the getters and setters SF2 knows it has to go to these methods.

    `

    <?php
    
    namespace XYZ\Entity;
    
    /**
     * @ORM\Entity
     * @ORM\HasLifecycleCallbacks()
     */
    class Company
    {
        ... 
        /**
         * @ORM\OneToMany(targetEntity="\XYZ\Entity\Contact", mappedBy="company", cascade={"persist", "remove"})
         */
        private $contacts;
    
        public function getPrimaryContact() { ... }
        public function setPrimaryContact(Contact $contact) { //Set the type of $contact and add it $this->addContact($contact) }
        public function getSecondaryContact() { ... }    
        public function setSecondaryContact(Contact $contact) { //Set the type of $contact and add it $this->addContact($contact) }
    
    }`
    

    And for the Form Type I have:

    `

    class CompanyType extends AbstractType
    {
        /**
         * @param FormBuilderInterface $builder
         * @param array $options
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
    
            $builder
                ...
                ->add('primaryContact', new ContactType())
                ->add('secondaryContact', new ContactType())
        }
    
        ...
    }`
    

    With this set everything runs smoothly and I can CRUD without much struggle.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改