Ceci est une ancienne révision du document !
Relations
2 grands types de relations existent :
- manyToOne/oneToMany
- manyToMany
manyToOne/oneToMany
Correspond aux associations hiérarchiques de type CIF.
manyToOne et oneToMany correspondent au même type d'association, mais vus de chaque côté de la relation.
manyToOne
Exemple
Chaque produit appartient à une catégorie :
// ...
class Product{
// ...
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="products")
* @ORM\JoinColumn(nullable=true)
*/
private $category;
public function getCategory(): Category
{
return $this->category;
}
public function setCategory(Category $category)
{
$this->category = $category;
}
}
Chaque catégorie peut contenir plusieurs projets :
// ...
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
class Category{
// ...
/**
* @ORM\OneToMany(targetEntity="App\Entity\Product", mappedBy="category")
*/
private $products;
public function __construct(){
$this->products = new ArrayCollection();
}
/**
* @return Collection|Product[]
*/
public function getProducts(){
return $this->products;
}
}