Answer the question
In order to leave comments, you need to log in
Doctrine OneToMany, is everything correct?
Hello!
There are entities (simplified):
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Brand
*
* @ORM\Table(name="brand")
* @ORM\Entity(repositoryClass="AppBundle\Repository\BrandRepository")
*/
class Brand
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* 1 brand:many products
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Product",
* mappedBy="brand")
*/
private $products;
public function __construct() {
parent::__construct();
$this->products = new ArrayCollection();
}
/**
* Get products
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getProducts()
{
return $this->products;
}
}
?>
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Product
*
* @ORM\Table(name="product")
* @ORM\Entity(repositoryClass=AppBundle\Repository\ProductRepository")
*/
class Product
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* many prodcuts : 1 brand
* @var integer
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Brand", inversedBy="products")
*/
private $brand;
}
?>
Answer the question
In order to leave comments, you need to log in
What is it?
class Brand
{
...
public function __construct() {
parent::__construct();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question