Skip to content

Instantly share code, notes, and snippets.

@Remiii
Last active February 26, 2021 08:36
Show Gist options
  • Save Remiii/9f10b905157d77658837 to your computer and use it in GitHub Desktop.
Save Remiii/9f10b905157d77658837 to your computer and use it in GitHub Desktop.
Doctrine (Symfony) DB mapping - Relation Mapping

Doctrine (Symfony) DB mapping

OneToOne (1=>1)

Sample: Category and Product

// src/Acme/StoreBundle/Entity/Category.php

// ...
use Doctrine\Common\Collections\ArrayCollection;

class Category
{
    // ...

    /**
     * @ORM\OneToOne(targetEntity="Product", inversedBy="category")
     * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
     */
    protected $product;
    
    // ....

}
// src/Acme/StoreBundle/Entity/Product.php

// ...
class Product
{

    // ...
    
    /**
      * @ORM\OneToOne(targetEntity="Category", mappedBy="product")
      */
     private $category;
    
    // ...

}
$ php app/console doctrine:schema:update --force
$ php app/console generate:doctrine:entities yourVendorName

OneToMany (1=>n)

Sample: Category and Product

// src/Acme/StoreBundle/Entity/Category.php
// Owning Side

class Category
{
    // ...

    /**
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="categories")
     * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
     */
    protected $product;

    // ...

}
// src/Acme/StoreBundle/Entity/Product.php
// Reverse Side

// ...
use Doctrine\Common\Collections\ArrayCollection;
// ...

class Product
{
    // ...

    /**
     * @ORM\OneToMany(targetEntity="Category", mappedBy="product")
     */
    protected $categories;
    
    // ....

    public function __construct()
    {
        $this->categories = new ArrayCollection();
    }
    
    // ...

    /**
     * Add category
     *
     * @param \remiii\GlobalBundle\Entity\Category $category
     * @return Product
     */
    public function addCategory(\remiii\GlobalBundle\Entity\Category $category)
    {
        $this->categories[] = $category;
        $category -> addProduct ( $this ) ;

        return $this;
    }
    
    // ...


}
$ php app/console doctrine:schema:update --force
$ php app/console generate:doctrine:entities yourVendorName

ManyToMany (n=>n)

Sample: Category and Product

// src/Acme/StoreBundle/Entity/Category.php
// Owning Side

// ...
use Doctrine\Common\Collections\ArrayCollection;

class Category
{
    // ...

    /**
     * @ORM\ManyToMany(targetEntity="Product", inversedBy="categories", cascade={"persist","remove"})
     * @ORM\JoinTable(name="category_product")
     */
    protected $products;

    public function __construct()
    {
        $this->products = new ArrayCollection();
    }
    
    // ...

}
// src/Acme/StoreBundle/Entity/Product.php
// Reverse Side

// ...
class Product
{
    // ...

    /**
     * @ORM\ManyToMany(targetEntity="Category", mappedBy="products", cascade={"persist","remove"})
     */
    protected $categories;

    public function __construct()
    {
        $this->categories = new ArrayCollection();
    }

    // ...

    /**
     * Add categories
     *
     * @param \remiii\GlobalBundle\Entity\Category $categories
     * @return Product
     */
    public function addCategories(\remiii\GlobalBundle\Entity\Category $categories)
    {
        $this->categories[] = $categories;
        $categories -> addProduct ( $this ) ;

        return $this;
    }

    // ...

}
$ php app/console doctrine:schema:update --force
$ php app/console generate:doctrine:entities yourVendorName

Sources

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment