Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
framework-web:symfony:models:relations [2018/02/16 09:46] – jcheron | framework-web:symfony:models:relations [2019/08/31 14:21] (Version actuelle) – modification externe 127.0.0.1 | ||
---|---|---|---|
Ligne 3: | Ligne 3: | ||
* manyToOne/ | * manyToOne/ | ||
* manyToMany | * manyToMany | ||
+ | |||
+ | ===== manyToOne/ | ||
+ | Correspond aux associations hiérarchiques de type CIF.\\ | ||
+ | **manyToOne** et **oneToMany** correspondent au même type d' | ||
+ | |||
+ | ==== Création ==== | ||
+ | Exemple : | ||
+ | < | ||
+ | [Product]0..*-1[Category] | ||
+ | </ | ||
+ | |||
+ | === manyToOne=== | ||
+ | |||
+ | Chaque produit appartient à une catégorie : | ||
+ | |||
+ | <sxh php; | ||
+ | |||
+ | // ... | ||
+ | class Product{ | ||
+ | // ... | ||
+ | |||
+ | /** | ||
+ | * @ORM\ManyToOne(targetEntity=" | ||
+ | * @ORM\JoinColumn(nullable=true) | ||
+ | */ | ||
+ | private $category; | ||
+ | |||
+ | public function getCategory(): | ||
+ | { | ||
+ | return $this-> | ||
+ | } | ||
+ | |||
+ | public function setCategory(Category $category) | ||
+ | { | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === oneToMany === | ||
+ | |||
+ | Chaque catégorie peut contenir plusieurs produits : | ||
+ | |||
+ | <sxh php; | ||
+ | |||
+ | // ... | ||
+ | use Doctrine\Common\Collections\ArrayCollection; | ||
+ | use Doctrine\Common\Collections\Collection; | ||
+ | |||
+ | class Category{ | ||
+ | // ... | ||
+ | |||
+ | /** | ||
+ | * @ORM\OneToMany(targetEntity=" | ||
+ | */ | ||
+ | private $products; | ||
+ | |||
+ | public function __construct(){ | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * @return Collection|Product[] | ||
+ | */ | ||
+ | public function getProducts(){ | ||
+ | return $this-> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === Chargement === | ||
+ | |||
+ | **manyToOne** : Récupération de la catégorie d'un produit : | ||
+ | |||
+ | <sxh php; | ||
+ | use App\Entity\Product; | ||
+ | // ... | ||
+ | |||
+ | /** | ||
+ | * @Route("/ | ||
+ | */ | ||
+ | public function showAction(Product $product){ | ||
+ | |||
+ | $categoryName = $product-> | ||
+ | |||
+ | // ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | === Persistance === | ||
+ | |||
+ | Sauvegarde d'un produit et de sa catégorie | ||
+ | |||
+ | <sxh php; | ||
+ | |||
+ | // ... | ||
+ | |||
+ | use App\Entity\Category; | ||
+ | use App\Entity\Product; | ||
+ | use Symfony\Component\HttpFoundation\Response; | ||
+ | |||
+ | class ProductController extends Controller{ | ||
+ | /** | ||
+ | * @Route("/ | ||
+ | */ | ||
+ | public function index(){ | ||
+ | $category = new Category(); | ||
+ | $category-> | ||
+ | |||
+ | $product = new Product(); | ||
+ | $product-> | ||
+ | $product-> | ||
+ | $product-> | ||
+ | |||
+ | // relates this product to the category | ||
+ | $product-> | ||
+ | |||
+ | $em = $this-> | ||
+ | $em-> | ||
+ | $em-> | ||
+ | $em-> | ||
+ | |||
+ | return new Response( | ||
+ | 'Saved new product with id: ' | ||
+ | .' and new category with id: ' | ||
+ | ); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== ManyToMany ===== | ||
+ | Correspond aux associations non hiérarchiques de type CIM, __non porteuses de données__.\\ | ||
+ | |||
+ | ==== Création ==== | ||
+ | Exemple : | ||
+ | |||
+ | < | ||
+ | [User]0..*-0..*[Group] | ||
+ | </ | ||
+ | |||
+ | <sxh php; | ||
+ | <?php | ||
+ | /** @Entity */ | ||
+ | class User | ||
+ | { | ||
+ | // ... | ||
+ | |||
+ | /** | ||
+ | * Each User has Many Groups. | ||
+ | * @ManyToMany(targetEntity=" | ||
+ | * @JoinTable(name=" | ||
+ | */ | ||
+ | private $groups; | ||
+ | |||
+ | public function __construct() { | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | // ... | ||
+ | } | ||
+ | </ | ||
+ |