Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @ORM\Entity
- */
- class Category
- {
- /**
- * @ORM\Id
- * @ORM\Column(type="integer")
- * @ORM\GeneratedValue
- */
- private $id;
- /**
- * @ORM\OneToMany(targetEntity="Product", mappedBy="category", fetch="EAGER")
- */
- private $products;
- public function __construct()
- {
- $this->products = new ArrayCollection();
- }
- public function products(): Collection
- {
- return $this->products;
- }
- public function id()
- {
- return $this->id;
- }
- public function addProduct(Product $product)
- {
- $product->setCategory($this);
- $this->products->add($product);
- }
- }
- /**
- * @ORM\Entity
- */
- class Product
- {
- /**
- * @ORM\Id
- * @ORM\Column(type="integer")
- * @ORM\GeneratedValue
- */
- protected $id;
- /**
- * @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
- * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
- */
- private $category;
- public function id()
- {
- return $this->id;
- }
- public function setCategory(Category $category)
- {
- $this->category = $category;
- }
- public function category()
- {
- return $this->category;
- }
- }
- $cat = new Category();
- $cat->addProduct(new Product());
- $this->entityManager->persist($prod);
- $this->entityManager->persist($cat);
- $this->entityManager->flush();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement