dsv17139 2014-10-16 01:50
浏览 25
已采纳

在实体之间共享FormType,最佳实践/方法

I have some entities that share the same structure, for example, take this two as a base example:

/**
 * @ORM\Entity
 * @ORM\Table(name="nomencladores.modelo", schema="nomencladores")
 */
class Modelo
{
    use IdentifierAutogeneratedEntityTrait;
    use NamedEntityTrait;
    use ActiveEntityTrait;

    /**
     * @var \TipoTramite
     *
     * @ORM\ManyToOne(targetEntity="TipoTramite")
     * @ORM\JoinColumn(name="tipo_tramite_id", referencedColumnName="id")
     */
    protected $tipo_tramite;

    /**
     * Set tipo tramite
     *
     * @param  \ComunBundle\Entity\TipoTramite $tipo_tramite
     * @return FabricanteDistribuidor
     */
    public function setTipoTramite(\ComunBundle\Entity\TipoTramite $tipo_tramite)
    {
        $this->tipo_tramite = $tipo_tramite;

        return $this;
    }

    /**
     * Get tipo tramite
     *
     * @return \ComunBundle\Entity\TipoTramite
     */
    public function getTipoTramite()
    {
        return $this->tipo_tramite;
    }

}

/**
 * @ORM\Entity
 * @ORM\Table(name="nomencladores.marca", schema="nomencladores")
 */
class Marca
{
    use IdentifierAutogeneratedEntityTrait;
    use NamedEntityTrait;
    use ActiveEntityTrait;

    /**
     * @var \TipoTramite
     *
     * @ORM\ManyToOne(targetEntity="TipoTramite")
     * @ORM\JoinColumn(name="tipo_tramite_id", referencedColumnName="id")
     */
    protected $tipo_tramite;

    /**
     * Set tipo tramite
     *
     * @param  \ComunBundle\Entity\TipoTramite $tipo_tramite
     * @return FabricanteDistribuidor
     */
    public function setTipoTramite(\ComunBundle\Entity\TipoTramite $tipo_tramite)
    {
        $this->tipo_tramite = $tipo_tramite;

        return $this;
    }

    /**
     * Get tipo tramite
     *
     * @return \ComunBundle\Entity\TipoTramite
     */
    public function getTipoTramite()
    {
        return $this->tipo_tramite;
    }

}

As you can see in the code above the entities share almost the same code, just change the table where information is store. Now I need to build a form for each of them and basically will be the same with some minor changes, see this example for Modelo entity:

class ModeloType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
                ->add('nombre')
                ->add('activo')
                ->add('tipo_tramite', 'entity', array(
                    'class' => 'ComunBundle:TipoTramite',
                    'property' => 'nombre',
                    'empty_value' => '-- SELECCIONAR --',
                    'query_builder' => function (EntityRepository $er) {
                        return $er->createQueryBuilder('qb')
                                ->where('qb.activo = :activoValue')
                                ->setParameter('activoValue', TRUE);
                    }
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'ComunBundle\Entity\Modelo'
        ));
    }

    public function getName()
    {
        return 'Modelo';
    }

}

So, the only things changing between one FormType and the other will be: the class name, the data_class attribute and the getName() return value, is there any way to apply DRY on FormType on this scenario?

  • 写回答

1条回答 默认 最新

  • doulan7166 2014-10-16 15:01
    关注

    Firstly, having two almost identical Entities (and hence two almost identical tables) seems like a smell to me - I'd think hard about somehow combining them into one Entity with some additional category to distinguish them.

    That aside, can you just define your own intermediate abstract type, and then inherit from that changing those two small things that are different, e.g.

    //This is not a good name, I'm sure you can do better!
    abstract class AbstractModeloMarcaType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                    ->add('nombre')
                    ->add('activo')
                    ->add('tipo_tramite', 'entity', array(
                        'class' => 'ComunBundle:TipoTramite',
                        'property' => 'nombre',
                        'empty_value' => '-- SELECCIONAR --',
                        'query_builder' => function (EntityRepository $er) {
                            return $er->createQueryBuilder('qb')
                                    ->where('qb.activo = :activoValue')
                                    ->setParameter('activoValue', TRUE);
                        }
            ));
        }
    
        abstract protected function getDataClass();
    
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => $this->getDataClass()
            ));
        }
    
    }
    
    public class ModeloType extends AbstractModeloMarcaType 
    {
        public function getName()
        {
            return 'Modelo';
        }
    
        public function getDataClass()
        {
            return 'ComunBundle\Entity\Modelo';
        }
    }
    
    public class MarcaType extends AbstractModeloMarcaType 
    {
        public function getName()
        {
            return 'Marca';
        }
    
        public function getDataClass()
        {
            return 'ComunBundle\Entity\Marca';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制