我正在尝试实施 Sylius Cart Bundle,但每次将产品添加到购物车时,都会创建一个新产品。
这可能是我的行的链接:
cascade: ["persist", "remove"]
在我的 YAML 文件中:
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
但是如果把它取下来,我会得到一个错误:
通过关系“Pharmacie\FrontBundle\Entity\CartItem#produit”找到了一个新实体,该实体未配置为实体:3test2 的级联持久操作。要解决此问题:在此未知实体上显式调用 EntityManager#persist() 或配置级联在映射中保留此关联,例如 @ManyToOne(..,cascade={"persist"})
根据学说文档,设置新对象时会发生此错误。但我只通过 ID 获取现有对象:
$product = $this->getProductRepository()->find($productId);
$item->setProduit($product); //this generates the error
$item->setUnitPrice(5); //this works fine
我不明白为什么它被保存为一个新对象。
如果我使用merge
而不是persist
,我会得到同样的错误:
通过关系发现了一个新实体...