create offerings and its migration

This commit is contained in:
Jannis Portmann 2021-04-24 17:36:38 +02:00
parent f3ad0e4638
commit 307954d46f
4 changed files with 223 additions and 0 deletions

View file

@ -0,0 +1,38 @@
<?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 Version20210424153343 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 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 \'\'');
}
}

93
src/Entity/Offering.php Normal file
View file

@ -0,0 +1,93 @@
<?php
namespace App\Entity;
use App\Repository\OfferingRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=OfferingRepository::class)
*/
class Offering
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="offerings")
* @ORM\JoinColumn(nullable=false)
*/
private $byUser;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $photoFilename;
public function getId(): ?int
{
return $this->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;
}
}

View file

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

View file

@ -0,0 +1,50 @@
<?php
namespace App\Repository;
use App\Entity\Offering;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Offering|null find($id, $lockMode = null, $lockVersion = null)
* @method Offering|null findOneBy(array $criteria, array $orderBy = null)
* @method Offering[] findAll()
* @method Offering[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class OfferingRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Offering::class);
}
// /**
// * @return Offering[] Returns an array of Offering objects
// */
/*
public function findByExampleField($value)
{
return $this->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()
;
}
*/
}