mise en place doctrine

master
Xantxo COQUILLARD 3 years ago
parent 0934124428
commit 3c646640dc

@ -2,4 +2,7 @@
return [ return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true], 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],
]; ];

@ -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

@ -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%'

@ -19,6 +19,9 @@ services:
- '../src/DependencyInjection/' - '../src/DependencyInjection/'
- '../src/Entity/' - '../src/Entity/'
- '../src/Kernel.php' - '../src/Kernel.php'
App\Controller\:
resource: '../src/App/Controller'
tags: ['controller.service_arguments']
# add more service definitions when explicit configuration is needed # add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones # please note that last definitions always *replace* previous ones

@ -2,10 +2,28 @@
namespace App\Composant\User\Domain; namespace App\Composant\User\Domain;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User 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 int $roleId;
private float $kwhPrice, $hourlyCost; private float $kwhPrice, $hourlyCost;
@ -93,4 +111,20 @@ class User
$this->firstname = $firstname; $this->firstname = $firstname;
} }
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @param int $id
*/
public function setId(int $id): void
{
$this->id = $id;
}
} }

@ -5,13 +5,28 @@ namespace App\Composant\User\Infrastructure;
use App\Composant\User\Domain\User; use App\Composant\User\Domain\User;
use App\Composant\User\Domain\UserDataMapper; use App\Composant\User\Domain\UserDataMapper;
use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Exception;
class DoctrineUser extends ServiceEntityRepository implements UserDataMapper
{
class DoctrineUser implements UserDataMapper public function __construct(ManagerRegistry $registry)
{ {
parent::__construct($registry, User::class);
}
public function findById(int $id): User 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 public function findAll(): array

@ -4,11 +4,12 @@ namespace App\Controller;
use App\Composant\User\Domain\User; use App\Composant\User\Domain\User;
use App\Composant\User\Infrastructure\DoctrineUser; use App\Composant\User\Infrastructure\DoctrineUser;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
class test class test extends AbstractController
{ {
public function __construct() public function __construct()
@ -25,12 +26,13 @@ class test
* expressions can also include configuration parameters: * expressions can also include configuration parameters:
* condition: "request.headers.get('User-Agent') matches '%app.allowed_browsers%'" * condition: "request.headers.get('User-Agent') matches '%app.allowed_browsers%'"
*/ */
public function index(): Response public function index(ManagerRegistry $registry): Response
{ {
$DoctrineUser = new DoctrineUser(); $DoctrineUser = new DoctrineUser($registry);
$DoctrineUser->findById(1); $User = $DoctrineUser->findById(1);
dd($User);
return new Response( return new Response(
'Content', "",
Response::HTTP_OK, Response::HTTP_OK,
['content-type' => 'text/html'] ['content-type' => 'text/html']
); );

Loading…
Cancel
Save