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?