Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.41 KB | None | 0 0
  1. /**
  2.  * @ORM\Entity
  3.  */
  4. class Category
  5. {
  6.     /**
  7.      * @ORM\Id
  8.      * @ORM\Column(type="integer")
  9.      * @ORM\GeneratedValue
  10.      */
  11.     private $id;
  12.     /**
  13.      * @ORM\OneToMany(targetEntity="Product", mappedBy="category", fetch="EAGER")
  14.      */
  15.     private $products;
  16.  
  17.     public function __construct()
  18.     {
  19.         $this->products = new ArrayCollection();
  20.     }
  21.  
  22.     public function products(): Collection
  23.     {
  24.         return $this->products;
  25.     }
  26.  
  27.     public function id()
  28.     {
  29.         return $this->id;
  30.     }
  31.  
  32.     public function addProduct(Product $product)
  33.     {
  34.         $product->setCategory($this);
  35.         $this->products->add($product);
  36.     }
  37. }
  38.  
  39. /**
  40.  * @ORM\Entity
  41.  */
  42. class Product
  43. {
  44.     /**
  45.      * @ORM\Id
  46.      * @ORM\Column(type="integer")
  47.      * @ORM\GeneratedValue
  48.      */
  49.     protected $id;
  50.  
  51.     /**
  52.      * @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
  53.      * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
  54.      */
  55.     private $category;
  56.  
  57.     public function id()
  58.     {
  59.         return $this->id;
  60.     }
  61.    
  62.     public function setCategory(Category $category)
  63.     {
  64.         $this->category = $category;
  65.     }
  66.  
  67.     public function category()
  68.     {
  69.         return $this->category;
  70.     }
  71. }
  72.  
  73.  
  74. $cat = new Category();
  75. $cat->addProduct(new Product());
  76.  
  77. $this->entityManager->persist($prod);
  78. $this->entityManager->persist($cat);
  79. $this->entityManager->flush();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement