feat(Customer): Ajoute la relation OneToMany avec CustomerAddress et les méthodes associées.

todo :add form create addresse in created account and edit account add multi address
This commit is contained in:
Serreau Jovann
2026-01-16 14:34:38 +01:00
parent 7e7a10f5b6
commit cb5fdba2f9
4 changed files with 263 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
<?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 Version20260116133405 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 customer_address (id SERIAL NOT NULL, customer_id INT DEFAULT NULL, address VARCHAR(255) NOT NULL, address2 VARCHAR(255) DEFAULT NULL, address3 VARCHAR(255) DEFAULT NULL, zipcode VARCHAR(255) NOT NULL, city VARCHAR(255) NOT NULL, country VARCHAR(255) NOT NULL, comment TEXT DEFAULT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE INDEX IDX_1193CB3F9395C3F3 ON customer_address (customer_id)');
$this->addSql('ALTER TABLE customer_address ADD CONSTRAINT FK_1193CB3F9395C3F3 FOREIGN KEY (customer_id) REFERENCES customer (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE SCHEMA public');
$this->addSql('ALTER TABLE customer_address DROP CONSTRAINT FK_1193CB3F9395C3F3');
$this->addSql('DROP TABLE customer_address');
}
}

View File

@@ -3,6 +3,8 @@
namespace App\Entity; namespace App\Entity;
use App\Repository\CustomerRepository; use App\Repository\CustomerRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: CustomerRepository::class)] #[ORM\Entity(repositoryClass: CustomerRepository::class)]
@@ -37,6 +39,17 @@ class Customer
#[ORM\Column(length: 255, nullable: true)] #[ORM\Column(length: 255, nullable: true)]
private ?string $customerId = null; private ?string $customerId = null;
/**
* @var Collection<int, CustomerAddress>
*/
#[ORM\OneToMany(targetEntity: CustomerAddress::class, mappedBy: 'customer')]
private Collection $customerAddresses;
public function __construct()
{
$this->customerAddresses = new ArrayCollection();
}
public function getId(): ?int public function getId(): ?int
{ {
return $this->id; return $this->id;
@@ -137,4 +150,34 @@ class Customer
return $this; return $this;
} }
/**
* @return Collection<int, CustomerAddress>
*/
public function getCustomerAddresses(): Collection
{
return $this->customerAddresses;
}
public function addCustomerAddress(CustomerAddress $customerAddress): static
{
if (!$this->customerAddresses->contains($customerAddress)) {
$this->customerAddresses->add($customerAddress);
$customerAddress->setCustomer($this);
}
return $this;
}
public function removeCustomerAddress(CustomerAddress $customerAddress): static
{
if ($this->customerAddresses->removeElement($customerAddress)) {
// set the owning side to null (unless already changed)
if ($customerAddress->getCustomer() === $this) {
$customerAddress->setCustomer(null);
}
}
return $this;
}
} }

View File

@@ -0,0 +1,142 @@
<?php
namespace App\Entity;
use App\Repository\CustomerAddressRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: CustomerAddressRepository::class)]
class CustomerAddress
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(inversedBy: 'customerAddresses')]
private ?Customer $customer = null;
#[ORM\Column(length: 255)]
private ?string $address = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $address2 = null;
#[ORM\Column(length: 255,nullable: true)]
private ?string $address3 = null;
#[ORM\Column(length: 255)]
private ?string $zipcode = null;
#[ORM\Column(length: 255)]
private ?string $city = null;
#[ORM\Column(length: 255)]
private ?string $country = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $comment = null;
public function getId(): ?int
{
return $this->id;
}
public function getCustomer(): ?Customer
{
return $this->customer;
}
public function setCustomer(?Customer $customer): static
{
$this->customer = $customer;
return $this;
}
public function getAddress(): ?string
{
return $this->address;
}
public function setAddress(string $address): static
{
$this->address = $address;
return $this;
}
public function getAddress2(): ?string
{
return $this->address2;
}
public function setAddress2(?string $address2): static
{
$this->address2 = $address2;
return $this;
}
public function getAddress3(): ?string
{
return $this->address3;
}
public function setAddress3(string $address3): static
{
$this->address3 = $address3;
return $this;
}
public function getZipcode(): ?string
{
return $this->zipcode;
}
public function setZipcode(string $zipcode): static
{
$this->zipcode = $zipcode;
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(string $city): static
{
$this->city = $city;
return $this;
}
public function getCountry(): ?string
{
return $this->country;
}
public function setCountry(string $country): static
{
$this->country = $country;
return $this;
}
public function getComment(): ?string
{
return $this->comment;
}
public function setComment(?string $comment): static
{
$this->comment = $comment;
return $this;
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace App\Repository;
use App\Entity\CustomerAddress;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<CustomerAddress>
*/
class CustomerAddressRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, CustomerAddress::class);
}
// /**
// * @return CustomerAddress[] Returns an array of CustomerAddress objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('c')
// ->andWhere('c.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('c.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?CustomerAddress
// {
// return $this->createQueryBuilder('c')
// ->andWhere('c.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}