utilisation des attributs + migration
This commit is contained in:
parent
706166fa26
commit
30e8edc728
config/packages
migrations
src
Component
Role
User
Controller
@ -12,8 +12,8 @@ doctrine:
|
||||
mappings:
|
||||
App:
|
||||
is_bundle: false
|
||||
dir: '%kernel.project_dir%/src/Composant'
|
||||
prefix: 'App\Composant'
|
||||
dir: '%kernel.project_dir%/src/Component'
|
||||
prefix: 'App\Component'
|
||||
alias: App
|
||||
|
||||
when@test:
|
||||
|
33
migrations/Version20220407211354.php
Normal file
33
migrations/Version20220407211354.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
/**
|
||||
* Auto-generated Migration: Please modify to your needs!
|
||||
*/
|
||||
final class Version20220407211354 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
// this up() migration is auto-generated, please modify it to your needs
|
||||
$this->addSql('CREATE TABLE roles (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, created DATETIME NOT NULL, updated DATETIME NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
|
||||
$this->addSql('CREATE TABLE users (id INT AUTO_INCREMENT NOT NULL, lastname VARCHAR(255) NOT NULL, firstname VARCHAR(255) NOT NULL, kwh_price NUMERIC(2, 0) NOT NULL, hourly_cost NUMERIC(2, 0) NOT NULL, created DATETIME NOT NULL, updated DATETIME NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
// this down() migration is auto-generated, please modify it to your needs
|
||||
$this->addSql('DROP TABLE roles');
|
||||
$this->addSql('DROP TABLE users');
|
||||
}
|
||||
}
|
62
src/Component/Role/Domain/Role.php
Normal file
62
src/Component/Role/Domain/Role.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Component\Role\Domain;
|
||||
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
|
||||
#[ORM\Entity, ORM\Table(name:"roles")]
|
||||
class Role
|
||||
{
|
||||
|
||||
#[ORM\Id, ORM\GeneratedValue(strategy: 'AUTO')]
|
||||
#[ORM\Column(type:Types::INTEGER)]
|
||||
private int $id;
|
||||
|
||||
#[ORM\Column(type:Types::STRING, length:255)]
|
||||
private string $name;
|
||||
|
||||
#[ORM\Column]
|
||||
public \DateTime $created;
|
||||
|
||||
#[ORM\Column]
|
||||
public \DateTime $updated;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*/
|
||||
public function setId(int $id): void
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*/
|
||||
public function setName(string $name): void
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
}
|
8
src/Component/Role/Domain/RoleDataMapper.php
Normal file
8
src/Component/Role/Domain/RoleDataMapper.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Component\Role\Domain;
|
||||
|
||||
interface RoleDataMapper
|
||||
{
|
||||
|
||||
}
|
9
src/Component/Role/Domain/RoleRepository.php
Normal file
9
src/Component/Role/Domain/RoleRepository.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace App\Component\Role\Domain;
|
||||
|
||||
interface RoleRepository
|
||||
{
|
||||
public function findById(int $id): Role;
|
||||
public function findAll(): array;
|
||||
}
|
38
src/Component/Role/Infrastructure/RoleDatabaseAdapter.php
Normal file
38
src/Component/Role/Infrastructure/RoleDatabaseAdapter.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Component\Role\Infrastructure;
|
||||
|
||||
use App\Component\Role\Domain\Role;
|
||||
use App\Component\Role\Domain\RoleDataMapper;
|
||||
use App\Component\Role\Domain\RoleRepository;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
use Exception;
|
||||
|
||||
class RoleDatabaseAdapter extends ServiceEntityRepository implements RoleDataMapper, RoleRepository
|
||||
{
|
||||
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Role::class);
|
||||
}
|
||||
|
||||
public function findById(int $id): Role
|
||||
{
|
||||
$user = $this->find($id);
|
||||
|
||||
if (!$user)
|
||||
{
|
||||
throw new Exception(
|
||||
'L\'utilisateur n\'existe pas'
|
||||
);
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
public function findAll(): array
|
||||
{
|
||||
// TODO: Implement findAll() method.
|
||||
}
|
||||
}
|
@ -1,7 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Composant\User\Domain;
|
||||
namespace App\Component\User\Domain;
|
||||
|
||||
use App\Component\Role\Domain\Role;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
@ -17,64 +18,47 @@ class User
|
||||
#[ORM\Column(type:Types::STRING, length:255)]
|
||||
private string $lastname;
|
||||
|
||||
#[ORM\Column(type:Types::STRING, length:255)]
|
||||
private string $firstname;
|
||||
|
||||
private int $roleId;
|
||||
#[ORM\OneToOne(mappedBy:"role", targetEntity: Role::class)]
|
||||
#[ORM\JoinTable(name:"roles")]
|
||||
#[ORM\JoinColumn(name: "role_id", referencedColumnName: "id")]
|
||||
#[ORM\InverseJoinColumn(name: "id", referencedColumnName: "role_id")]
|
||||
protected Role $role;
|
||||
|
||||
#[ORM\Column(type:Types::DECIMAL,length:10, precision:2)]
|
||||
private float $kwhPrice;
|
||||
|
||||
#[ORM\Column(type:Types::DECIMAL,length:10, precision:2)]
|
||||
private float $hourlyCost;
|
||||
|
||||
#[ORM\Column]
|
||||
public \DateTime $created;
|
||||
|
||||
#[ORM\Column]
|
||||
public \DateTime $updated;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @return float
|
||||
*/
|
||||
public function getKwhPrice(): float
|
||||
{
|
||||
return $this->kwhPrice;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float $kwhPrice
|
||||
*/
|
||||
public function setKwhPrice(float $kwhPrice): void
|
||||
{
|
||||
$this->kwhPrice = $kwhPrice;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return float
|
||||
*/
|
||||
public function getHourlyCost(): float
|
||||
{
|
||||
return $this->hourlyCost;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float $hourlyCost
|
||||
*/
|
||||
public function setHourlyCost(float $hourlyCost): void
|
||||
{
|
||||
$this->hourlyCost = $hourlyCost;
|
||||
$this->created = new \DateTime();
|
||||
$this->updated = new \DateTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getRoleId(): int
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->roleId;
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $roleId
|
||||
* @param int $id
|
||||
*/
|
||||
public function setRoleId(int $roleId): void
|
||||
public function setId(int $id): void
|
||||
{
|
||||
$this->roleId = $roleId;
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -110,19 +94,84 @@ class User
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
* @return Role
|
||||
*/
|
||||
public function getId(): int
|
||||
public function getRole(): Role
|
||||
{
|
||||
return $this->id;
|
||||
return $this->role;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @param Role $role
|
||||
*/
|
||||
public function setId(int $id): void
|
||||
public function setRole(Role $role): void
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->role = $role;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return float
|
||||
*/
|
||||
public function getKwhPrice(): float
|
||||
{
|
||||
return $this->kwhPrice;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float $kwhPrice
|
||||
*/
|
||||
public function setKwhPrice(float $kwhPrice): void
|
||||
{
|
||||
$this->kwhPrice = $kwhPrice;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return float
|
||||
*/
|
||||
public function getHourlyCost(): float
|
||||
{
|
||||
return $this->hourlyCost;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float $hourlyCost
|
||||
*/
|
||||
public function setHourlyCost(float $hourlyCost): void
|
||||
{
|
||||
$this->hourlyCost = $hourlyCost;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \DateTime
|
||||
*/
|
||||
public function getCreated(): \DateTime
|
||||
{
|
||||
return $this->created;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DateTime $created
|
||||
*/
|
||||
public function setCreated(\DateTime $created): void
|
||||
{
|
||||
$this->created = $created;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \DateTime
|
||||
*/
|
||||
public function getUpdated(): \DateTime
|
||||
{
|
||||
return $this->updated;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DateTime $updated
|
||||
*/
|
||||
public function setUpdated(\DateTime $updated): void
|
||||
{
|
||||
$this->updated = $updated;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Composant\User\Domain;
|
||||
namespace App\Component\User\Domain;
|
||||
|
||||
interface UserDataMapper
|
||||
{
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Composant\User\Domain;
|
||||
namespace App\Component\User\Domain;
|
||||
|
||||
interface UserRepository
|
||||
{
|
@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Composant\User\Infrastructure;
|
||||
namespace App\Component\User\Infrastructure;
|
||||
|
||||
use App\Composant\User\Domain\User;
|
||||
use App\Composant\User\Domain\UserDataMapper;
|
||||
use App\Composant\User\Domain\UserRepository;
|
||||
use App\Component\User\Domain\User;
|
||||
use App\Component\User\Domain\UserDataMapper;
|
||||
use App\Component\User\Domain\UserRepository;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
use Exception;
|
@ -2,8 +2,7 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Composant\User\Infrastructure\UserDatabaseAdapter;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
use App\Component\User\Infrastructure\UserDatabaseAdapter;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
@ -16,10 +15,9 @@ class TestController extends AbstractController
|
||||
{
|
||||
}
|
||||
|
||||
#[Route('/foo')]
|
||||
public function index(ManagerRegistry $registry): Response
|
||||
#[Route('/testUser')]
|
||||
public function index(UserDatabaseAdapter $DoctrineUser): Response
|
||||
{
|
||||
$DoctrineUser = new UserDatabaseAdapter($registry);
|
||||
$user = $DoctrineUser->findById(1);
|
||||
return new Response(
|
||||
$user->getId(). " " .$user->getLastname(),
|
||||
|
Loading…
Reference in New Issue
Block a user