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
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
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
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html