I have this mapping betwenn two entities:
class Orders {
// Other attributes
/**
* @ORM\OneToMany(targetEntity="OrderHasProduct", mappedBy="order")
*/
protected $orderProducts;
// Other set/get methods
public function getOrderProducts()
{
return $this->orderProducts;
}
}
class Product {
// Other attributes
/**
* @ORM\OneToMany(targetEntity="\Tanane\FrontendBundle\Entity\OrderHasProduct", mappedBy="product")
*/
protected $orderProducts;
// Other set/get methods
public function getOrderProducts()
{
return $this->orderProducts;
}
}
And of course since many Orders can have many products but also there is an extra attribute this other entity is needed:
class OrderHasProduct
{
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="\Tanane\FrontendBundle\Entity\Orders")
* @ORM\JoinColumn(name="general_orders_id", referencedColumnName="id")
*/
protected $order;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="\Tanane\ProductBundle\Entity\Product")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
protected $product;
/**
* @ORM\Column(type="integer", nullable=false)
*/
protected $amount;
public function setOrder(\Tanane\FrontendBundle\Entity\Orders $order)
{
$this->order = $order;
}
public function getOrder()
{
return $this->order;
}
public function setProduct(\Tanane\ProductBundle\Entity\Product $product)
{
$this->product = $product;
}
public function getProduct()
{
return $this->product;
}
public function setAmount($amount)
{
$this->amount = $amount;
}
public function getAmount()
{
return $this->amount;
}
}
When I edit a order I should able to add/remove the products on that order but I don't know how to achieve this. I knew that I must use a form collection but how? I mean a collection should be embed as follow:
$builder->add('product', 'collection', array(
'type' => new OrderHasProductType(),
'allow_add' => true,
'allow_delete' => true
));
When I should create a new OrderHasProductType
form and I think I understand until this point but my question now is, what happens to the ID of the order? What is the proper way to handle an embedded form a relationship n:m with extra parameters?
Can any give me some code example to order my ideas?
Extra resources
- Orders Entity Complete Source
- Product Entity Complete Source
- Orders Form Type Complete Source
- OrderHasProduct Form Type Complete Source