dsv17139
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
    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';
        }
    }
    
    点赞 评论

相关推荐