mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Use dependency injection and EntityManagerInterface.
This commit is contained in:
parent
e449efc695
commit
348f649fef
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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(
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user