utilisation des attributs + migration

master
Xantxo COQUILLARD 2 years ago
parent 706166fa26
commit 30e8edc728

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

@ -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');
}
}

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

@ -0,0 +1,8 @@
<?php
namespace App\Component\Role\Domain;
interface RoleDataMapper
{
}

@ -0,0 +1,9 @@
<?php
namespace App\Component\Role\Domain;
interface RoleRepository
{
public function findById(int $id): Role;
public function findAll(): array;
}

@ -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,112 +18,160 @@ 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()
{
$this->created = new \DateTime();
$this->updated = new \DateTime();
}
/**
* @return float
* @return int
*/
public function getKwhPrice(): float
public function getId(): int
{
return $this->kwhPrice;
return $this->id;
}
/**
* @param float $kwhPrice
* @param int $id
*/
public function setKwhPrice(float $kwhPrice): void
public function setId(int $id): void
{
$this->kwhPrice = $kwhPrice;
$this->id = $id;
}
/**
* @return float
* @return string
*/
public function getHourlyCost(): float
public function getLastname(): string
{
return $this->hourlyCost;
return $this->lastname;
}
/**
* @param float $hourlyCost
* @param string $lastname
*/
public function setHourlyCost(float $hourlyCost): void
public function setLastname(string $lastname): void
{
$this->hourlyCost = $hourlyCost;
$this->lastname = $lastname;
}
/**
* @return int
* @return string
*/
public function getRoleId(): int
public function getFirstname(): string
{
return $this->roleId;
return $this->firstname;
}
/**
* @param int $roleId
* @param string $firstname
*/
public function setRoleId(int $roleId): void
public function setFirstname(string $firstname): void
{
$this->roleId = $roleId;
$this->firstname = $firstname;
}
/**
* @return string
* @return Role
*/
public function getLastname(): string
public function getRole(): Role
{
return $this->lastname;
return $this->role;
}
/**
* @param string $lastname
* @param Role $role
*/
public function setLastname(string $lastname): void
public function setRole(Role $role): void
{
$this->lastname = $lastname;
$this->role = $role;
}
/**
* @return string
* @return float
*/
public function getFirstname(): string
public function getKwhPrice(): float
{
return $this->firstname;
return $this->kwhPrice;
}
/**
* @param string $firstname
* @param float $kwhPrice
*/
public function setFirstname(string $firstname): void
public function setKwhPrice(float $kwhPrice): void
{
$this->firstname = $firstname;
$this->kwhPrice = $kwhPrice;
}
/**
* @return int
* @return float
*/
public function getId(): int
public function getHourlyCost(): float
{
return $this->id;
return $this->hourlyCost;
}
/**
* @param int $id
* @param float $hourlyCost
*/
public function setId(int $id): void
public function setHourlyCost(float $hourlyCost): void
{
$this->id = $id;
$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;
}
}

@ -0,0 +1,8 @@
<?php
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;

@ -1,8 +0,0 @@
<?php
namespace App\Composant\User\Domain;
interface UserDataMapper
{
}

@ -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…
Cancel
Save