diff --git a/migrations/Version20230329195906.php b/migrations/Version20230329195906.php new file mode 100644 index 0000000000000000000000000000000000000000..e2cd127e894e0b1a061fccfff26fae82a083993e --- /dev/null +++ b/migrations/Version20230329195906.php @@ -0,0 +1,37 @@ +<?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 Version20230329195906 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 category (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE clothes (id INT AUTO_INCREMENT NOT NULL, category_id INT NOT NULL, name VARCHAR(255) NOT NULL, description LONGTEXT NOT NULL, picture VARCHAR(255) NOT NULL, price DOUBLE PRECISION NOT NULL, INDEX IDX_3079B48C12469DE2 (category_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE messenger_messages (id BIGINT AUTO_INCREMENT NOT NULL, body LONGTEXT NOT NULL, headers LONGTEXT NOT NULL, queue_name VARCHAR(190) NOT NULL, created_at DATETIME NOT NULL, available_at DATETIME NOT NULL, delivered_at DATETIME DEFAULT NULL, INDEX IDX_75EA56E0FB7336F0 (queue_name), INDEX IDX_75EA56E0E3BD61CE (available_at), INDEX IDX_75EA56E016BA31DB (delivered_at), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('ALTER TABLE clothes ADD CONSTRAINT FK_3079B48C12469DE2 FOREIGN KEY (category_id) REFERENCES category (id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE clothes DROP FOREIGN KEY FK_3079B48C12469DE2'); + $this->addSql('DROP TABLE category'); + $this->addSql('DROP TABLE clothes'); + $this->addSql('DROP TABLE messenger_messages'); + } +} diff --git a/src/Entity/Category.php b/src/Entity/Category.php new file mode 100644 index 0000000000000000000000000000000000000000..ae22fd663f62387807ef6ef5f5f9767855b2f3bf --- /dev/null +++ b/src/Entity/Category.php @@ -0,0 +1,75 @@ +<?php + +namespace App\Entity; + +use App\Repository\CategoryRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; +use Doctrine\ORM\Mapping as ORM; + +#[ORM\Entity(repositoryClass: CategoryRepository::class)] +class Category +{ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column] + private ?int $id = null; + + #[ORM\Column(length: 255)] + private ?string $name = null; + + #[ORM\OneToMany(mappedBy: 'category', targetEntity: Clothes::class, orphanRemoval: true)] + private Collection $clothes; + + public function __construct() + { + $this->clothes = new ArrayCollection(); + } + + public function getId(): ?int + { + return $this->id; + } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(string $name): self + { + $this->name = $name; + + return $this; + } + + /** + * @return Collection<int, Clothes> + */ + public function getClothes(): Collection + { + return $this->clothes; + } + + public function addClothes(Clothes $clothes): self + { + if (!$this->clothes->contains($clothes)) { + $this->clothes->add($clothes); + $clothes->setCategory($this); + } + + return $this; + } + + public function removeClothes(Clothes $clothes): self + { + if ($this->clothes->removeElement($clothes)) { + // set the owning side to null (unless already changed) + if ($clothes->getCategory() === $this) { + $clothes->setCategory(null); + } + } + + return $this; + } +} diff --git a/src/Entity/Clothes.php b/src/Entity/Clothes.php new file mode 100644 index 0000000000000000000000000000000000000000..00af1fcf56950076c403368e15666aeafd6a6fc1 --- /dev/null +++ b/src/Entity/Clothes.php @@ -0,0 +1,97 @@ +<?php + +namespace App\Entity; + +use App\Repository\ClothesRepository; +use Doctrine\DBAL\Types\Types; +use Doctrine\ORM\Mapping as ORM; + +#[ORM\Entity(repositoryClass: ClothesRepository::class)] +class Clothes +{ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column] + private ?int $id = null; + + #[ORM\Column(length: 255)] + private ?string $name = null; + + #[ORM\Column(type: Types::TEXT)] + private ?string $description = null; + + #[ORM\Column(length: 255)] + private ?string $picture = null; + + #[ORM\Column] + private ?float $price = null; + + #[ORM\ManyToOne(inversedBy: 'clothes')] + #[ORM\JoinColumn(nullable: false)] + private ?Category $category = null; + + public function getId(): ?int + { + return $this->id; + } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(string $name): self + { + $this->name = $name; + + return $this; + } + + public function getDescription(): ?string + { + return $this->description; + } + + public function setDescription(string $description): self + { + $this->description = $description; + + return $this; + } + + public function getPicture(): ?string + { + return $this->picture; + } + + public function setPicture(string $picture): self + { + $this->picture = $picture; + + return $this; + } + + public function getPrice(): ?float + { + return $this->price; + } + + public function setPrice(float $price): self + { + $this->price = $price; + + return $this; + } + + public function getCategory(): ?Category + { + return $this->category; + } + + public function setCategory(?Category $category): self + { + $this->category = $category; + + return $this; + } +} diff --git a/src/Repository/CategoryRepository.php b/src/Repository/CategoryRepository.php new file mode 100644 index 0000000000000000000000000000000000000000..e59e855b232bf8bff2171fcd7ddba9a957f3da2a --- /dev/null +++ b/src/Repository/CategoryRepository.php @@ -0,0 +1,66 @@ +<?php + +namespace App\Repository; + +use App\Entity\Category; +use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; +use Doctrine\Persistence\ManagerRegistry; + +/** + * @extends ServiceEntityRepository<Category> + * + * @method Category|null find($id, $lockMode = null, $lockVersion = null) + * @method Category|null findOneBy(array $criteria, array $orderBy = null) + * @method Category[] findAll() + * @method Category[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) + */ +class CategoryRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Category::class); + } + + public function save(Category $entity, bool $flush = false): void + { + $this->getEntityManager()->persist($entity); + + if ($flush) { + $this->getEntityManager()->flush(); + } + } + + public function remove(Category $entity, bool $flush = false): void + { + $this->getEntityManager()->remove($entity); + + if ($flush) { + $this->getEntityManager()->flush(); + } + } + +// /** +// * @return Category[] Returns an array of Category 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): ?Category +// { +// return $this->createQueryBuilder('c') +// ->andWhere('c.exampleField = :val') +// ->setParameter('val', $value) +// ->getQuery() +// ->getOneOrNullResult() +// ; +// } +} diff --git a/src/Repository/ClothesRepository.php b/src/Repository/ClothesRepository.php new file mode 100644 index 0000000000000000000000000000000000000000..a9e4ea7636249e5556164851276fd60176f503fc --- /dev/null +++ b/src/Repository/ClothesRepository.php @@ -0,0 +1,66 @@ +<?php + +namespace App\Repository; + +use App\Entity\Clothes; +use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; +use Doctrine\Persistence\ManagerRegistry; + +/** + * @extends ServiceEntityRepository<Clothes> + * + * @method Clothes|null find($id, $lockMode = null, $lockVersion = null) + * @method Clothes|null findOneBy(array $criteria, array $orderBy = null) + * @method Clothes[] findAll() + * @method Clothes[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) + */ +class ClothesRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Clothes::class); + } + + public function save(Clothes $entity, bool $flush = false): void + { + $this->getEntityManager()->persist($entity); + + if ($flush) { + $this->getEntityManager()->flush(); + } + } + + public function remove(Clothes $entity, bool $flush = false): void + { + $this->getEntityManager()->remove($entity); + + if ($flush) { + $this->getEntityManager()->flush(); + } + } + +// /** +// * @return Clothes[] Returns an array of Clothes 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): ?Clothes +// { +// return $this->createQueryBuilder('c') +// ->andWhere('c.exampleField = :val') +// ->setParameter('val', $value) +// ->getQuery() +// ->getOneOrNullResult() +// ; +// } +}