collection-crud/src/Entity/Brand.php

63 lines
1.6 KiB
PHP
Raw Normal View History

2022-09-29 20:09:31 -04:00
<?php declare(strict_types=1);
namespace App\Entity;
2022-10-07 22:00:14 -04:00
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
2022-09-29 20:09:31 -04:00
use Doctrine\ORM\Mapping as ORM;
#[ORM\Table(name: 'brand', schema: 'collection')]
2022-10-20 11:07:27 -04:00
#[ORM\Entity]
#[ORM\UniqueConstraint(name: 'brand_unq', columns: ["name"])]
2022-10-25 14:52:58 -04:00
class Brand {
2022-11-17 15:32:57 -05:00
use GetSet;
2022-09-29 20:09:31 -04:00
2022-10-25 14:52:58 -04:00
#[ORM\Column(name: 'id', type: 'integer', nullable: FALSE)]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'IDENTITY')]
#[ORM\SequenceGenerator(sequenceName: 'brand_id_seq', allocationSize: 1, initialValue: 1)]
private int $id;
2022-09-29 20:09:31 -04:00
2022-10-20 11:07:27 -04:00
/**
* @var Collection<int, BrandCategory>
*/
2022-11-17 15:32:57 -05:00
#[ORM\ManyToMany(targetEntity: BrandCategory::class, fetch: 'EXTRA_LAZY')]
2022-10-07 22:00:14 -04:00
#[ORM\JoinTable(name: 'collection.brand_category_link')]
#[ORM\JoinColumn(name: 'brand_id', referencedColumnName: 'id')]
#[ORM\InverseJoinColumn(name: 'brand_category', referencedColumnName: 'category_name')]
#[ORM\OrderBy(['name' => 'asc'])]
2022-10-20 11:07:27 -04:00
private Collection $categories;
2022-10-07 22:00:14 -04:00
2022-10-25 14:52:58 -04:00
#[ORM\Column(name: 'name', unique: TRUE, nullable: FALSE)]
private string $name;
2022-09-29 20:09:31 -04:00
2022-11-17 15:32:57 -05:00
// ------------------------------------------------------------------------
2022-10-25 14:52:58 -04:00
public function __construct()
{
$this->categories = new ArrayCollection();
}
2022-10-07 22:00:14 -04:00
2022-10-25 14:52:58 -04:00
public function __toString(): string
{
return $this->name;
}
2022-09-29 20:09:31 -04:00
2022-10-25 14:52:58 -04:00
public function addCategory(BrandCategory $category): self
{
if ( ! $this->categories->contains($category))
{
$this->categories->add($category);
}
2022-10-07 22:00:14 -04:00
2022-10-25 14:52:58 -04:00
return $this;
}
2022-10-07 22:00:14 -04:00
2022-10-25 14:52:58 -04:00
public function removeCategory(BrandCategory $category): self
{
$this->categories->removeElement($category);
2022-10-07 22:00:14 -04:00
2022-10-25 14:52:58 -04:00
return $this;
}
2022-09-29 20:09:31 -04:00
}