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';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致