From 307954d46f941698669f0e9a7ab71043c92b8526 Mon Sep 17 00:00:00 2001 From: Jannis Portmann Date: Sat, 24 Apr 2021 17:36:38 +0200 Subject: [PATCH] create offerings and its migration --- migrations/Version20210424153343.php | 38 +++++++++++ src/Entity/Offering.php | 93 +++++++++++++++++++++++++++ src/Entity/User.php | 42 ++++++++++++ src/Repository/OfferingRepository.php | 50 ++++++++++++++ 4 files changed, 223 insertions(+) create mode 100644 migrations/Version20210424153343.php create mode 100644 src/Entity/Offering.php create mode 100644 src/Repository/OfferingRepository.php diff --git a/migrations/Version20210424153343.php b/migrations/Version20210424153343.php new file mode 100644 index 0000000..951a673 --- /dev/null +++ b/migrations/Version20210424153343.php @@ -0,0 +1,38 @@ +addSql('CREATE SEQUENCE offering_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); + $this->addSql('CREATE TABLE offering (id INT NOT NULL, by_user_id INT NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, title VARCHAR(255) NOT NULL, photo_filename VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))'); + $this->addSql('CREATE INDEX IDX_A5682AB1DC9C2434 ON offering (by_user_id)'); + $this->addSql('ALTER TABLE offering ADD CONSTRAINT FK_A5682AB1DC9C2434 FOREIGN KEY (by_user_id) REFERENCES "user" (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE "user" ALTER username DROP DEFAULT'); + } + + 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('DROP SEQUENCE offering_id_seq CASCADE'); + $this->addSql('DROP TABLE offering'); + $this->addSql('ALTER TABLE "user" ALTER username SET DEFAULT \'\''); + } +} diff --git a/src/Entity/Offering.php b/src/Entity/Offering.php new file mode 100644 index 0000000..30328da --- /dev/null +++ b/src/Entity/Offering.php @@ -0,0 +1,93 @@ +id; + } + + public function getByUser(): ?User + { + return $this->byUser; + } + + public function setByUser(?User $byUser): self + { + $this->byUser = $byUser; + + return $this; + } + + public function getCreatedAt(): ?\DateTimeInterface + { + return $this->createdAt; + } + + public function setCreatedAt(\DateTimeInterface $createdAt): self + { + $this->createdAt = $createdAt; + + return $this; + } + + public function getTitle(): ?string + { + return $this->title; + } + + public function setTitle(string $title): self + { + $this->title = $title; + + return $this; + } + + public function getPhotoFilename(): ?string + { + return $this->photoFilename; + } + + public function setPhotoFilename(?string $photoFilename): self + { + $this->photoFilename = $photoFilename; + + return $this; + } +} diff --git a/src/Entity/User.php b/src/Entity/User.php index 51efa46..5bb18d5 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -3,6 +3,8 @@ namespace App\Entity; use App\Repository\UserRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Security\Core\User\UserInterface; @@ -47,6 +49,16 @@ class User implements UserInterface */ private $username; + /** + * @ORM\OneToMany(targetEntity=Offering::class, mappedBy="byUser", orphanRemoval=true) + */ + private $offerings; + + public function __construct() + { + $this->offerings = new ArrayCollection(); + } + public function getId(): ?int { return $this->id; @@ -146,4 +158,34 @@ class User implements UserInterface return $this; } + + /** + * @return Collection|Offering[] + */ + public function getOfferings(): Collection + { + return $this->offerings; + } + + public function addOffering(Offering $offering): self + { + if (!$this->offerings->contains($offering)) { + $this->offerings[] = $offering; + $offering->setByUser($this); + } + + return $this; + } + + public function removeOffering(Offering $offering): self + { + if ($this->offerings->removeElement($offering)) { + // set the owning side to null (unless already changed) + if ($offering->getByUser() === $this) { + $offering->setByUser(null); + } + } + + return $this; + } } diff --git a/src/Repository/OfferingRepository.php b/src/Repository/OfferingRepository.php new file mode 100644 index 0000000..bcf3f45 --- /dev/null +++ b/src/Repository/OfferingRepository.php @@ -0,0 +1,50 @@ +createQueryBuilder('o') + ->andWhere('o.exampleField = :val') + ->setParameter('val', $value) + ->orderBy('o.id', 'ASC') + ->setMaxResults(10) + ->getQuery() + ->getResult() + ; + } + */ + + /* + public function findOneBySomeField($value): ?Offering + { + return $this->createQueryBuilder('o') + ->andWhere('o.exampleField = :val') + ->setParameter('val', $value) + ->getQuery() + ->getOneOrNullResult() + ; + } + */ +}