簡體   English   中英

學說2:在復雜關系中拯救實體

[英]Doctrine 2: Saving Entity in Complex Relationship

我的學說實體內有以下關系:

FavoriteRecipe

/**
 * @ManyToOne(targetEntity="User", inversedBy="favoriteRecipes")
 */
private $user;

/**
 * @ManyToOne(targetEntity="Recipe", inversedBy="favoriteRecipes")
 */
private $recipe;

食譜

/**
 * @OneToMany(targetEntity="FavoriteRecipe", mappedBy="user")
 */
private $favoriteRecipes;

用戶

/**
 * @OneToMany(targetEntity="FavoriteRecipe", mappedBy="user")
 */
private $favoriteRecipes;

在我的一個控制器中,我有以下代碼:

$favoriteRecipe = new \Entities\FavoriteRecipe();
$favoriteRecipe->setRecipe($recipe);
$favoriteRecipe->setUser($user);
$this->_em->persist($favoriteRecipe);
$this->_em->flush();

但是這會拋出以下消息的異常:

通過未配置為級聯持久操作的關系找到新實體:Entities \\ User @ 00000000408bd010000000007cb1380e。 明確保留新實體或在關系上配置級聯持久操作。

如何正確創建和保存FavoriteRecipe實體?

您是否為所有關系實體設置了級聯選項? 這是通過為excample設置cascade屬性來完成的:cascade = {“persist”,“remove”}

也許這個頁面: http//www.doctrine-project.org/docs/orm/2.0/en/reference/working-with-associations.html

或者這些視頻: http//www.zendcasts.com/many-to-many-with-doctrine-2/2011/03/ http://www.zendcasts.com/one-to-many-with-doctrine-二千〇一十一分之二/ 03 /

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM