From cb5fdba2f945e6984b822eb1cb75204ebc5cc6fc Mon Sep 17 00:00:00 2001 From: Serreau Jovann Date: Fri, 16 Jan 2026 14:34:38 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(Customer):=20Ajoute=20la=20rel?= =?UTF-8?q?ation=20OneToMany=20avec=20CustomerAddress=20et=20les=20m=C3=A9?= =?UTF-8?q?thodes=20associ=C3=A9es.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit todo :add form create addresse in created account and edit account add multi address --- migrations/Version20260116133405.php | 35 +++++ src/Entity/Customer.php | 43 ++++++ src/Entity/CustomerAddress.php | 142 +++++++++++++++++++ src/Repository/CustomerAddressRepository.php | 43 ++++++ 4 files changed, 263 insertions(+) create mode 100644 migrations/Version20260116133405.php create mode 100644 src/Entity/CustomerAddress.php create mode 100644 src/Repository/CustomerAddressRepository.php diff --git a/migrations/Version20260116133405.php b/migrations/Version20260116133405.php new file mode 100644 index 0000000..fbfef25 --- /dev/null +++ b/migrations/Version20260116133405.php @@ -0,0 +1,35 @@ +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'); + } +} diff --git a/src/Entity/Customer.php b/src/Entity/Customer.php index 6375740..f2654f8 100644 --- a/src/Entity/Customer.php +++ b/src/Entity/Customer.php @@ -3,6 +3,8 @@ namespace App\Entity; use App\Repository\CustomerRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: CustomerRepository::class)] @@ -37,6 +39,17 @@ class Customer #[ORM\Column(length: 255, nullable: true)] private ?string $customerId = null; + /** + * @var Collection + */ + #[ORM\OneToMany(targetEntity: CustomerAddress::class, mappedBy: 'customer')] + private Collection $customerAddresses; + + public function __construct() + { + $this->customerAddresses = new ArrayCollection(); + } + public function getId(): ?int { return $this->id; @@ -137,4 +150,34 @@ class Customer return $this; } + + /** + * @return Collection + */ + 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; + } } diff --git a/src/Entity/CustomerAddress.php b/src/Entity/CustomerAddress.php new file mode 100644 index 0000000..d8dfdc8 --- /dev/null +++ b/src/Entity/CustomerAddress.php @@ -0,0 +1,142 @@ +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; + } +} diff --git a/src/Repository/CustomerAddressRepository.php b/src/Repository/CustomerAddressRepository.php new file mode 100644 index 0000000..b9ad5ae --- /dev/null +++ b/src/Repository/CustomerAddressRepository.php @@ -0,0 +1,43 @@ + + */ +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() + // ; + // } +}