一对多关系刷新时出错

时间:2013-10-03 14:31:16

标签: php doctrine-orm symfony-2.3

$cart = new Cart();
$item = new CartItem();

$variant = $repository->findOneById($variantId);

$item->setVariant($variant);
$cart->addItem($item);

$em->persist($cart);
$em->flush();

flush()我收到以下错误:

A new entity was found through the relationship 'CartItem#variant' that was not configured to cascade persist operations for entity:
Variant@0000000034ce4ce4000000000391db0d. 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"}). 
If you cannot find out which entity causes the problem implement 'Variant#__toString()' to get a clue.

Cart.xml

<one-to-many target-entity="CartItem" field="items" mapped-by="cart">
    <cascade>
        <cascade-persist/>
    </cascade>
</one-to-many>

CartItem.xml

<many-to-one field="cart" target-entity="Cart" inversed-by="items">
  <join-column name="cart_id" referenced-column-name="id" nullable="false" />
</many-to-one>
<many-to-one field="variant" target-entity="Variant">
    <join-column name="variant_id" referenced-column-name="id" nullable="false" />
</many-to-one>

在Variant.xml中,我没有CartItem映射。

1 个答案:

答案 0 :(得分:1)

您需要同时保留变体和项目实体。您还应该在变体上设置关系,而不仅仅是项目。