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 11:02] – jcheron | framework-web:symfony:models:relations [2019/08/31 14:21] (Version actuelle) – modification externe 127.0.0.1 | ||
|---|---|---|---|
| Ligne 8: | Ligne 8: | ||
| **manyToOne** et **oneToMany** correspondent au même type d' | **manyToOne** et **oneToMany** correspondent au même type d' | ||
| - | ==== manyToOne | + | ==== Création |
| + | Exemple : | ||
| + | < | ||
| + | [Product]0..*-1[Category] | ||
| + | </ | ||
| - | === Exemple | + | === manyToOne=== |
| Chaque produit appartient à une catégorie : | Chaque produit appartient à une catégorie : | ||
| Ligne 35: | Ligne 39: | ||
| $this-> | $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-> | ||
| + | } | ||
| + | |||
| + | // ... | ||
| } | } | ||
| </ | </ | ||