framework-web:symfony:models:relations

Ceci est une ancienne révision du document !


Relations

2 grands types de relations existent :

  • manyToOne/oneToMany
  • manyToMany

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.

Titre

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;
    }
}

  • framework-web/symfony/models/relations.1518777456.txt.gz
  • Dernière modification : il y a 6 ans
  • (modification externe)