原则2:复杂关系中的实体保存 [英] Doctrine 2: Saving Entity in Complex Relationship
问题描述
我的学说实体之间有以下关系:
I have the following relationships within my doctrine entities:
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;
在我的一个控制器中,我有以下代码:
In one of my controllers I have the following code:
$favoriteRecipe = new \Entities\FavoriteRecipe();
$favoriteRecipe->setRecipe($recipe);
$favoriteRecipe->setUser($user);
$this->_em->persist($favoriteRecipe);
$this->_em->flush();
但是,这会引发异常,并显示以下消息:
But this throws an exception with the following message:
通过没有配置
的关系来发现一个新的实体级联持久化操作:
Entities\User @ 00000000408bd010000000007cb1380e。明确地坚持
新实体或在
关系上配置级联持久性操作。
A new entity was found through a relationship that was not configured to cascade persist operations: Entities\User@00000000408bd010000000007cb1380e. Explicitly persist the new entity or configure cascading persist operations on the relationship.
我正确地创建并保存一个 FavoriteRecipe
实体?
How can I correctly create and save a FavoriteRecipe
entity?
推荐答案
您是否为所有关系实体设置了级联选项?这是通过设置cascade属性为excample来实现的:cascade = {persist,remove}
Did you set the cascade option for all your relational entities? This is done by setting the cascade property for excample: 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-2/2011/03/
这篇关于原则2:复杂关系中的实体保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!