나는 동일한 구조를 공유하는 일부 기관이, 예를 들어, 기본 예제로이 둘을 :공유 FormType, 모범 사례/접근
/**
* @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;
}
}
당신이 엔티티 위의 코드에서 볼 수 있듯이 거의 동일한 코드를 공유하고, 정보가 저장되는 테이블을 변경하십시오.
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';
}
}
그래서, 유일한 일이 하나 FormType 다른 사이 변경하면됩니다 : 지금은 그들 각각의 양식을 구축하고 기본적으로 약간의 변경과 동일합니다, Modelo
법인이 예를 참조해야합니다 : 클래스 이름, data_class
속성 및 getName()
반환 값은이 시나리오에서 FormType에 DRY를 적용 할 수있는 방법이 있습니까?
실체가 거의 동일하지만 나는 내가 DB 모델 뒤에 전문가가 아니에요, 두 가지를두고 그 다른 테이블과의 관계를 공유하는, 즉 DB 설계자, 난 코드 뒤에 사람이야 – ReynierPM