diff --git a/app/config/packages/doctrine.yaml b/app/config/packages/doctrine.yaml index 757d04787..cbf1c485a 100644 --- a/app/config/packages/doctrine.yaml +++ b/app/config/packages/doctrine.yaml @@ -24,7 +24,11 @@ doctrine: is_bundle: false dir: '%kernel.project_dir%/../sources/AppBundle/Accounting/Entity' prefix: 'AppBundle\Accounting\Entity' - alias: Accounting + Site: + type: attribute + is_bundle: false + dir: '%kernel.project_dir%/../sources/AppBundle/Site/Entity' + prefix: 'AppBundle\Site\Entity' when@test: doctrine: diff --git a/sources/Afup/Utils/Pays.php b/sources/Afup/Utils/Pays.php index 6031b1fbd..6f8fdad2c 100644 --- a/sources/Afup/Utils/Pays.php +++ b/sources/Afup/Utils/Pays.php @@ -4,7 +4,7 @@ namespace Afup\Site\Utils; -use AppBundle\Site\Model\Repository\CountryRepository; +use AppBundle\Site\Entity\Repository\CountryRepository; /** * Classe de gestion des pays @@ -20,11 +20,11 @@ public function __construct(private readonly CountryRepository $countryRepositor * * @return array */ - public function obtenirPays() + public function obtenirPays(): array { $result = []; - foreach ($this->countryRepository->getAllCountries() as $country) { - $result[$country->getId()] = $country->getName(); + foreach ($this->countryRepository->getAllSortedByName() as $country) { + $result[$country->id] = $country->name; } return $result; @@ -35,6 +35,6 @@ public function obtenirPays() */ public function obtenirNom(string $id): string { - return $this->countryRepository->getOneBy(['id' => $id])->getName(); + return $this->countryRepository->find($id)->name; } } diff --git a/sources/AppBundle/Site/Entity/Country.php b/sources/AppBundle/Site/Entity/Country.php new file mode 100644 index 000000000..3fab706dd --- /dev/null +++ b/sources/AppBundle/Site/Entity/Country.php @@ -0,0 +1,19 @@ + + */ +final class CountryRepository extends EntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Country::class); + } + + /** + * @return array + */ + public function getAllSortedByName(): array + { + return $this->createQueryBuilder('c') + ->orderBy('c.name', 'asc') + ->getQuery() + ->execute(); + } +} diff --git a/sources/AppBundle/Site/Model/Country.php b/sources/AppBundle/Site/Model/Country.php deleted file mode 100644 index 9c02301d5..000000000 --- a/sources/AppBundle/Site/Model/Country.php +++ /dev/null @@ -1,43 +0,0 @@ -id; - } - - public function setId(string $id): self - { - $this->propertyChanged('id', $this->id, $id); - $this->id = $id; - - return $this; - } - - public function getName(): string - { - return $this->name; - } - - public function setName(string $name): self - { - $this->propertyChanged('name', $this->name, $name); - $this->name = $name; - - return $this; - } -} diff --git a/sources/AppBundle/Site/Model/Repository/CountryRepository.php b/sources/AppBundle/Site/Model/Repository/CountryRepository.php deleted file mode 100644 index 2e8f1d75d..000000000 --- a/sources/AppBundle/Site/Model/Repository/CountryRepository.php +++ /dev/null @@ -1,61 +0,0 @@ - - */ -class CountryRepository extends Repository implements MetadataInitializer -{ - /** - * @return CollectionInterface - */ - public function getAllCountries(): CollectionInterface - { - /** @var SelectInterface $builder */ - $builder = $this->getQueryBuilder(self::QUERY_SELECT); - $builder->cols(['*']) - ->from('afup_pays') - ->orderBy(['nom asc']); - - return $this->getQuery($builder->getStatement())->query($this->getCollection(new HydratorSingleObject())); - } - - - /** - * @inheritDoc - */ - public static function initMetadata(SerializerFactoryInterface $serializerFactory, array $options = []): Metadata - { - $metadata = new Metadata($serializerFactory); - $metadata->setEntity(Country::class); - $metadata->setConnectionName('main'); - $metadata->setDatabase($options['database']); - $metadata->setTable('afup_pays'); - $metadata - ->addField([ - 'columnName' => 'id', - 'fieldName' => 'id', - 'primary' => true, - 'type' => 'string', - ]) - ->addField([ - 'columnName' => 'nom', - 'fieldName' => 'name', - 'type' => 'string', - ]); - - return $metadata; - } -}