src/Entity/CategoryProduct.php line 17

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