Use dependency injection and EntityManagerInterface.

This commit is contained in:
Pol Dellaiera 2021-05-19 21:14:07 +02:00
parent e449efc695
commit 348f649fef
No known key found for this signature in database
GPG Key ID: D476DFE9C67467CA
3 changed files with 14 additions and 14 deletions

View File

@ -15,7 +15,7 @@ use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class ObjectToIdTransformer implements DataTransformerInterface
final class ObjectToIdTransformer implements DataTransformerInterface
{
private ?string $class;
@ -38,7 +38,7 @@ class ObjectToIdTransformer implements DataTransformerInterface
*/
public function reverseTransform($id)
{
if (!$id) {
if (null === $id) {
return null;
}

View File

@ -15,17 +15,17 @@ use Chill\PersonBundle\Entity\Person;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Repository\PersonRepository;
use Doctrine\ORM\EntityRepository;
class PersonToIdTransformer implements DataTransformerInterface
final class PersonToIdTransformer implements DataTransformerInterface
{
/**
* @var ObjectManager
*/
private $om;
private EntityRepository $personRepository;
public function __construct(ObjectManager $om)
public function __construct(PersonRepository $personRepository)
{
$this->om = $om;
$this->personRepository = $personRepository;
}
/**
@ -43,9 +43,8 @@ class PersonToIdTransformer implements DataTransformerInterface
return null;
}
$issue = $this->om
->getRepository('ChillPersonBundle:Person')
->findOneBy(['id' => $id]);
$issue = $this->personRepository
->findOneBy(array('id' => $id));
if (null === $issue) {
throw new TransformationFailedException(sprintf(

View File

@ -40,13 +40,14 @@ class Select2MaritalStatusType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new ObjectToIdTransformer($this->em, 'Chill\PersonBundle\Entity\MaritalStatus');
$transformer = new ObjectToIdTransformer($this->em, MaritalStatus::class);
$builder->addModelTransformer($transformer);
}
public function configureOptions(OptionsResolver $resolver)
{
$maritalStatuses = $this->em->getRepository('Chill\PersonBundle\Entity\MaritalStatus')->findAll();
$locale = $this->requestStack->getCurrentRequest()->getLocale();
$maritalStatuses = $this->em->getRepository(MaritalStatus::class)->findAll();
$choices = [];
foreach ($maritalStatuses as $ms) {