大家好,我上周在数据变换器上遇到困难。
它的目标是转换实体中的TextType(Departement) 因为我可以通过EntityType来完成它。
我已经在Symfony Doc上阅读了有关它的整个文档,但错误总是一样的。
错误是:
捕获致命错误:参数1传递给ExellBundle \ Form \ Type \ SearchType :: __ construct()必须是ExellBundle \ Form \ DataTransfomer \ DepartementTransformer的实例,没有给出,在/ Users / maxime / Documents / www /中调用第92行的exellapp / vendor / symfony / symfony / src / Symfony / Component / Form / FormRegistry.php已定义
DepartementTransformer.php
<?php
namespace ExellBundle\Form\DataTransfomer;
use ExellBundle\Entity\Departement;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class DepartementTransformer implements DataTransformerInterface
{
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
/**
* @param Departement|null $departement
* @return string
*/
public function transform($departement)
{
// TODO: Implement transform() method.
if(null == $departement)
{
return '';
}
return $departement->getId();
}
/**
* @param string $departementNumber
* @return Departement|null
* @throws TransformationFailedException if object (departement) is not found
*/
public function reverseTransform($departementNumber)
{
// TODO: Implement reverseTransform() method.
if(!$departementNumber)
{
return;
}
$departement = $this->em->getRepository('ExellBundle:Departement')->find($departementNumber);
if(null === $departement)
{
throw new TransformationFailedException(sprintf(
'Le departement %s n\'existe pas ! ',
$departementNumber
));
}
return $departement;
}
}
SearchType.php
<?php
namespace ExellBundle\Form\Type;
use ExellBundle\Form\DataTransfomer\DepartementTransformer;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use ExellBundle\Entity\Bien;
class SearchType extends AbstractType
{
private $transformer;
public function __construct(DepartementTransformer $transformer)
{
$this->transformer = $transformer;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('typeInvest',EntityType::class,array(
'placeholder' => 'Veuillez choisir',
'label' => 'Type d\'investistement',
'class' => 'ExellBundle\Entity\TypeInvest',
'choice_label' => 'libelle',
'expanded' => false,
'multiple' => false,
'attr' => [
'class' => 'form-inline',
'required' => true,
]
))
/*->add('departement',EntityType::class,array(
'placeholder' => 'Veuillez renseigner le Département',
'class' => 'ExellBundle\Entity\Departement',
'attr' => [
'class' => 'form-inline',
'required' => true,
],
));*/
->add('departement',TextType::class,array(
'invalid_message' => ' Cela n\'est pas un numéro de département valide',
));
$builder->get('departement')->addModelTransformer($this->transformer);
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Bien::class,
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'exellbundle_bien';
}
}