From 3c646640dc50cdf4e8881518cb19175fa8a6e074 Mon Sep 17 00:00:00 2001 From: Xantxo COQUILLARD Date: Wed, 6 Apr 2022 20:09:42 +0200 Subject: [PATCH] mise en place doctrine --- config/bundles.php | 3 ++ config/packages/doctrine.yaml | 42 +++++++++++++++++++ config/packages/doctrine_migrations.yaml | 6 +++ config/services.yaml | 3 ++ migrations/.gitignore | 0 src/App/Composant/User/Domain/User.php | 36 +++++++++++++++- .../User/Infrastructure/DoctrineUser.php | 21 ++++++++-- src/App/Controller/test.php | 12 +++--- 8 files changed, 114 insertions(+), 9 deletions(-) create mode 100644 config/packages/doctrine.yaml create mode 100644 config/packages/doctrine_migrations.yaml create mode 100644 migrations/.gitignore diff --git a/config/bundles.php b/config/bundles.php index 49d3fb6..de8898b 100644 --- a/config/bundles.php +++ b/config/bundles.php @@ -2,4 +2,7 @@ return [ Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true], + Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true], + Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true], + Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true], ]; diff --git a/config/packages/doctrine.yaml b/config/packages/doctrine.yaml new file mode 100644 index 0000000..e0a25e7 --- /dev/null +++ b/config/packages/doctrine.yaml @@ -0,0 +1,42 @@ +doctrine: + dbal: + url: '%env(resolve:DATABASE_URL)%' + + # IMPORTANT: You MUST configure your server version, + # either here or in the DATABASE_URL env var (see .env file) + #server_version: '13' + orm: + auto_generate_proxy_classes: true + naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware + auto_mapping: true + mappings: + App: + is_bundle: false + dir: '%kernel.project_dir%/src/App/Composant' + prefix: 'App\Composant' + alias: App + +when@test: + doctrine: + dbal: + # "TEST_TOKEN" is typically set by ParaTest + dbname_suffix: '_test%env(default::TEST_TOKEN)%' + +when@prod: + doctrine: + orm: + auto_generate_proxy_classes: false + query_cache_driver: + type: pool + pool: doctrine.system_cache_pool + result_cache_driver: + type: pool + pool: doctrine.result_cache_pool + + framework: + cache: + pools: + doctrine.result_cache_pool: + adapter: cache.app + doctrine.system_cache_pool: + adapter: cache.system diff --git a/config/packages/doctrine_migrations.yaml b/config/packages/doctrine_migrations.yaml new file mode 100644 index 0000000..a0a17a0 --- /dev/null +++ b/config/packages/doctrine_migrations.yaml @@ -0,0 +1,6 @@ +doctrine_migrations: + migrations_paths: + # namespace is arbitrary but should be different from App\Migrations + # as migrations classes should NOT be autoloaded + 'DoctrineMigrations': '%kernel.project_dir%/migrations' + enable_profiler: '%kernel.debug%' diff --git a/config/services.yaml b/config/services.yaml index 2d6a76f..1e5a5fa 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -19,6 +19,9 @@ services: - '../src/DependencyInjection/' - '../src/Entity/' - '../src/Kernel.php' + App\Controller\: + resource: '../src/App/Controller' + tags: ['controller.service_arguments'] # add more service definitions when explicit configuration is needed # please note that last definitions always *replace* previous ones diff --git a/migrations/.gitignore b/migrations/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/src/App/Composant/User/Domain/User.php b/src/App/Composant/User/Domain/User.php index 588cc6e..758cd47 100644 --- a/src/App/Composant/User/Domain/User.php +++ b/src/App/Composant/User/Domain/User.php @@ -2,10 +2,28 @@ namespace App\Composant\User\Domain; +use Doctrine\ORM\Mapping as ORM; +/** + * @ORM\Entity + * @ORM\Table(name="users") + */ class User { - private string $lastname, $firstname; + /** + * @ORM\Id() + * @ORM\GeneratedValue() + * @ORM\Column(type="integer") + */ + private int $id; + + /** + * @ORM\Column(type="string", length=255) + */ + private string $lastname; + + //finir le reste xD + private string $firstname; private int $roleId; private float $kwhPrice, $hourlyCost; @@ -93,4 +111,20 @@ class User $this->firstname = $firstname; } + /** + * @return int + */ + public function getId(): int + { + return $this->id; + } + + /** + * @param int $id + */ + public function setId(int $id): void + { + $this->id = $id; + } + } \ No newline at end of file diff --git a/src/App/Composant/User/Infrastructure/DoctrineUser.php b/src/App/Composant/User/Infrastructure/DoctrineUser.php index 4f23ff1..dada3a5 100644 --- a/src/App/Composant/User/Infrastructure/DoctrineUser.php +++ b/src/App/Composant/User/Infrastructure/DoctrineUser.php @@ -5,13 +5,28 @@ namespace App\Composant\User\Infrastructure; use App\Composant\User\Domain\User; use App\Composant\User\Domain\UserDataMapper; use Doctrine\Persistence\ManagerRegistry; +use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; +use Exception; - -class DoctrineUser implements UserDataMapper +class DoctrineUser extends ServiceEntityRepository implements UserDataMapper { + + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, User::class); + } + public function findById(int $id): User { - // TODO: Implement findById() method. + $entityManager = $this->getEntityManager(); + $User = $entityManager->getRepository(User::class)->find($id); + if (!$User) { + throw new Exception( + 'L\'utilisateur n\'existe pas' + ); + } + + return $User; } public function findAll(): array diff --git a/src/App/Controller/test.php b/src/App/Controller/test.php index e117c78..8236df2 100644 --- a/src/App/Controller/test.php +++ b/src/App/Controller/test.php @@ -4,11 +4,12 @@ namespace App\Controller; use App\Composant\User\Domain\User; use App\Composant\User\Infrastructure\DoctrineUser; +use Doctrine\Persistence\ManagerRegistry; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\HttpFoundation\Response; -class test +class test extends AbstractController { public function __construct() @@ -25,12 +26,13 @@ class test * expressions can also include configuration parameters: * condition: "request.headers.get('User-Agent') matches '%app.allowed_browsers%'" */ - public function index(): Response + public function index(ManagerRegistry $registry): Response { - $DoctrineUser = new DoctrineUser(); - $DoctrineUser->findById(1); + $DoctrineUser = new DoctrineUser($registry); + $User = $DoctrineUser->findById(1); + dd($User); return new Response( - 'Content', + "", Response::HTTP_OK, ['content-type' => 'text/html'] );