manyToOne is creating new products

Posted by user788721 on Stack Overflow See other posts from Stack Overflow or by user788721
Published on 2014-06-09T13:33:18Z Indexed on 2014/06/09 15:26 UTC
Read the original article Hit count: 231

Filed under:
|
|

I am trying to implement Sylius Cart Bundle, but every time I add a product to the cart, a new product is created.

This is probably link to my line:

cascade: ["persist", "remove"]

In my YAML File:

Pharmacie\FrontBundle\Entity\CartItem:
type: entity
table: app_cart_item    
manyToOne:
    produit:
        targetEntity: Pharmacie\FrontBundle\Entity\Product  
        cascade: ["persist", "remove"]                       
        joinColumn:
            name: product_id
            referencedColumnName: id 

But if take it off, I get an error:

A new entity was found through the relationship 'Pharmacie\FrontBundle\Entity\CartItem#produit' that was not configured to cascade persist operations for entity: 3test2. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"})

According to the doctrine doc, this error occurs when you set a new object. But I am only getting an existing object By ID:

$product = $this->getProductRepository()->find($productId);
$item->setProduit($product); //this generates the error
$item->setUnitPrice(5); //this works fine

I don't understand why it's save as a new object.

If I use merge instead of persist, I get the same error:

A new entity was found through the relationship...

© Stack Overflow or respective owner

Related posts about symfony2

Related posts about doctrine2