doctrine2 - Symfony Doctrine ManyToMany add custom join field -
i have 2 entites : cart , item, relation configured manytomany because cart can have multiple items, , items can in multiple carts.
so have link table item_cart item_id , cart_id.
how can work quantity ? example if need add 800 items id = 2 cart id = 5 ?
is possible add field quantity in link table ?
thanks help.
you can making relationship entity. entity called cartitem or cartitemlink.
the association changes manytomany between cart , item 2 associations manytoone , onetomany:
cart - manytoone - cartitem - onetomany - item
now can add additional fields cartitem, $quantity field mentioned in question.
so this:
the cartitem:
class cartitem { /** many-to-one bidirectional, owning side * @var cart * @orm\manytoone(targetentity="application\entity\cart", inversedby="cartitems") * @orm\joincolumn(name="cart_id", referencedcolumnname="id") */ private $cart; /** many-to-one bidirectional, owning side * @var item * @orm\manytoone(targetentity="application\entity\item", inversedby="cartitems") * @orm\joincolumn(name="item_id", referencedcolumnname="id") */ private $item; /** * @var int * @orm\column(type="integer", nullable=false) */ private $quantity; //.. setters + getters } the cart:
class cart { /** * @var integer * @orm\id * @orm\column(type="integer", nullable=false) * @orm\generatedvalue(strategy="auto") */ private $id; /** one-to-many bidirectional, inverse side * @var arraycollection * @orm\onetomany(targetentity="application\entity\cartitem", mappedby="cart") */ private $cartitems; //.. setters + getters } the item:
class item { /** * @var integer * @orm\id * @orm\column(type="integer", nullable=false) * @orm\generatedvalue(strategy="auto") */ private $id; /** one-to-many bidirectional, inverse side * @var arraycollection * @orm\onetomany(targetentity="application\entity\cartitem", mappedby="item") */ private $cartitems; //.. setters + getters } i didn't add id cartitem because can have either composite key ($item_id + $cart_id) or natural key , leave you.
don't forget initialize $cartitems arraycollection inside constructor of item , cart.
Comments
Post a Comment