framework-web:symfony:models:relations

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

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:32] – [manyToOne] jcheronframework-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'association, mais vus de chaque côté de la relation. **manyToOne** et **oneToMany** correspondent au même type d'association, mais vus de chaque côté de la relation.
  
-==== manyToOne ==== +==== Création ==== 
- +Exemple :
-=== Exemple === +
 <classdiagram> <classdiagram>
 [Product]0..*-1[Category] [Product]0..*-1[Category]
 </classdiagram> </classdiagram>
 +
 +=== manyToOne===
  
 Chaque produit appartient à une catégorie : Chaque produit appartient à une catégorie :
Ligne 42: Ligne 42:
 </sxh> </sxh>
  
-Chaque catégorie peut contenir plusieurs projets :+=== oneToMany === 
 + 
 +Chaque catégorie peut contenir plusieurs produits :
  
 <sxh php;tilte:src/Entity/Category.php> <sxh php;tilte:src/Entity/Category.php>
Ligne 71: Ligne 73:
 </sxh> </sxh>
  
 +=== Chargement ===
 +
 +**manyToOne** : Récupération de la catégorie d'un produit :
 +
 +<sxh php;title:src/Controller/ProductController.php>
 +use App\Entity\Product;
 +// ...
 +
 +/**
 + * @Route("/product/{id}", name="product_show")
 + */
 +public function showAction(Product $product){
 +
 +    $categoryName = $product->getCategory()->getName();
 +
 +    // ...
 +}
 +</sxh>
 +
 +
 +=== Persistance ===
 +
 +Sauvegarde d'un produit et de sa catégorie
 +
 +<sxh php;title:src/Controller/ProductController.php>
 +
 +// ...
 +
 +use App\Entity\Category;
 +use App\Entity\Product;
 +use Symfony\Component\HttpFoundation\Response;
 +
 +class ProductController extends Controller{
 +    /**
 +     * @Route("/product", name="product")
 +     */
 +    public function index(){
 +        $category = new Category();
 +        $category->setName('Computer Peripherals');
 +
 +        $product = new Product();
 +        $product->setName('Keyboard');
 +        $product->setPrice(19.99);
 +        $product->setDescription('Ergonomic and stylish!');
 +
 +        // relates this product to the category
 +        $product->setCategory($category);
 +
 +        $em = $this->getDoctrine()->getManager();
 +        $em->persist($category);
 +        $em->persist($product);
 +        $em->flush();
 +
 +        return new Response(
 +            'Saved new product with id: '.$product->getId()
 +            .' and new category with id: '.$category->getId()
 +        );
 +    }
 +}
 +</sxh>
 +
 +===== ManyToMany =====
 +Correspond aux associations non hiérarchiques de type CIM, __non porteuses de données__.\\
 +
 +==== Création ====
 +Exemple :
 +
 +<classdiagram>
 +[User]0..*-0..*[Group]
 +</classdiagram>
 +
 +<sxh php;title:app/Entity/User.php>
 +<?php
 +/** @Entity */
 +class User
 +{
 +    // ...
 +
 +    /**
 +     * Each User has Many Groups.
 +     * @ManyToMany(targetEntity="Group", inversedBy="users")
 +     * @JoinTable(name="users_groups")
 +     */
 +    private $groups;
 +
 +    public function __construct() {
 +        $this->groups = new \Doctrine\Common\Collections\ArrayCollection();
 +    }
 +
 +    // ...
 +}
 +</sxh>
  
  • framework-web/symfony/models/relations.1518777147.txt.gz
  • Dernière modification : il y a 6 ans
  • (modification externe)