src/Entity/CategoryProduct.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
  5. use App\Entity\Category;
  6. use App\Entity\Product;
  7. use App\Entity\ProductModel;
  8. /**
  9. * ProductsToCategories
  10. *
  11. * @ORM\Table(name="products_to_categories", indexes={@ORM\Index(name="idx_categories_id", columns={"categories_id", "products_id"})})
  12. * @ORM\Entity(repositoryClass="App\Repository\CategoryProductRepository")
  13. * @ORM\HasLifecycleCallbacks()
  14. */
  15. class CategoryProduct {
  16. /**
  17. * @var int
  18. *
  19. * @ORM\Column(name="id", type="integer", nullable=false)
  20. * @ORM\Id
  21. * @ORM\GeneratedValue(strategy="IDENTITY")
  22. */
  23. private $id;
  24. /**
  25. * @var ?Product
  26. *
  27. *
  28. *
  29. * @ORM\ManyToOne(targetEntity="App\Entity\Product")
  30. * @ORM\JoinColumn(name="products_id", referencedColumnName="products_id", onDelete="cascade")
  31. */
  32. private $product;
  33. /**
  34. * @var int
  35. *
  36. * @ORM\ManyToOne(targetEntity="App\Entity\Category")
  37. * @ORM\JoinColumn(name="categories_id", referencedColumnName="categories_id", onDelete="cascade")
  38. */
  39. private $category;
  40. public function getId() {
  41. return $this->id;
  42. }
  43. public function getProduct() {
  44. return $this->product;
  45. }
  46. public function getModel(): ?ProductModel {
  47. return $this->model;
  48. }
  49. public function getCategory() {
  50. return $this->category;
  51. }
  52. public function setProduct(Product $product) {
  53. $this->product = $product;
  54. }
  55. public function setCategory(Category $category) {
  56. $this->category = $category;
  57. }
  58. public function setModel(?ProductModel $model): void {
  59. $this->model = $model;
  60. }
  61. /**
  62. * @ORM\PostLoad
  63. *
  64. * @todo Remove once 0 values in the table are converted to NULL.
  65. */
  66. public function postLoad(\Doctrine\ORM\Event\LifecycleEventArgs $args) {
  67. if ($this->product && $this->product->getId() == 0) {
  68. $this->product = null;
  69. }
  70. if ($this->category && $this->category->getId() == 0) {
  71. $this->category = null;
  72. }
  73. }
  74. }