I have those Entitys:
First: ProductCupMain -> like a Product
<?php
namespace xxx\Security\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* xxx\Security\Entity\ProductCupMain
*
* @ORM\Table(name="product_cup_main")
* @ORM\Entity
*/
class ProductCupMain
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=45, nullable=true)
*/
private $name;
/**
* @var string $image
*
* @ORM\Column(name="image", type="string", length=128, nullable=true)
*/
private $image;
/**
* @var string $pdf
*
* @ORM\Column(name="pdf", type="string", length=128, nullable=true)
*/
private $pdf;
/**
* @var ProductCategories
*
* @ORM\ManyToMany(targetEntity="ProductCategories", inversedBy="productCupMain")
* @ORM\JoinTable(name="product_cup_main_has_product_categories",
* joinColumns={
* @ORM\JoinColumn(name="product_cup_main_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="product_categories_id", referencedColumnName="id")
* }
* )
*/
private $productCategories;
Secound: ProductCategories -> like Categories
<?php
namespace xxx\Security\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* xxx\Security\Entity\ProductCategories
*
* @ORM\Table(name="product_categories")
* @ORM\Entity
*/
class ProductCategories
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=45, nullable=false)
*/
private $name;
/**
* @var string $image
*
* @ORM\Column(name="image", type="string", length=100, nullable=true)
*/
private $image;
/**
* @var string $shortname
*
* @ORM\Column(name="shortname", type="string", length=164, nullable=false)
*/
private $shortname;
/**
* @var integer $position
*
* @ORM\Column(name="position", type="integer", nullable=false)
*/
private $position;
/**
* @var ProductCupMain
*
* @ORM\ManyToMany(targetEntity="ProductCupMain", mappedBy="productCategories")
*/
private $productCupMain;
So, i want to save Many products in Many categories. (ManyToMany relation) My problem is a position. The product have different positions in the categories.
I want to save the position on the relation, something like this:
http://i.stack.imgur.com/Z0gzw.jpg
I need a method like: getPositionInCategory($categoryId) where i can get the right position. Can you help me ?
Further, i had found a similar problem here, but i cant get the right solution for me.
EDIT 1
Solution from @mbinette: I've created a third entity for the reference. Is it right ?
//ProductCategoryReference
<?php
namespace xxx\Security\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* xxx\Security\Entity\ProductCategoryReference
*
* @ORM\Table(name="product_cup_main_has_product_categories")
* @ORM\Entity
*/
class ProductCategoryReference
{
/**
* @ORM\ManyToOne(targetEntity="ProductCupMain", inversedBy="categoriesHavetheProduct")
*/
private $prductCupMain;
/**
* @ORM\ManyToOne(targetEntity="ProductCategories", inversedBy="productsInCategory")
*/
private $productsCategorie;
/**
* @ORM\Column(name="postionInCategorie", type="integer", length=164, nullable=false)
*/
private $postionInCategorie;
public function getProductCupMain()
{
return $this->prductCupMain;
}
public function setProductCupMain($prductCupMain)
{
$this->prductCupMain = $prductCupMain;
}
public function getProductsCategorie()
{
return $this->productsCategorie;
}
public function setProductsCategorie($productsCategorie)
{
$this->productsCategorie = $productsCategorie;
}
public function getPostionInCategorie()
{
return $this->productsCategorie;
}
public function setPostionInCategorie($postionInCategorie)
{
$this->postionInCategorie = $postionInCategorie;
}
}
But what is with the product entity and the category entity ?
Product Entity:
/**
* @var CategoryHavetheProduct
*
* @OneToMany(targetEntity="ProductCategoryReference", mappedBy="productCupMain")
*
**/
private $categoriesHavetheProduct;
//Please show me the getter and setter methods
Categorie Entity:
/**
* @var ProductsInCategory
*
* @OneToMany(targetEntity="ProductCategoryReference", mappedBy="productsCategorie")
*
**/
private $productsInCategory;
//Please show me the getter and setter methods