我的学说实体之间有以下关系:
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"}
也许这个页面: www.doctrine-project/docs/orm/2.0/ en / reference / working-with-associations.html
或这些视频: www.zendcasts/many-to-many-with-doctrine-2/2011/ 03 / http: //www.zendcasts/one-to-many-with-doctrine-2/2011/03/
更多推荐
原则2:复杂关系中的实体保存
发布评论