加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Doctrine 2:在复杂的关系中保存实体

发布时间:2020-12-13 13:12:02 所属栏目:PHP教程 来源:网络整理
导读:我的学说实体内有以下关系: FavoriteRecipe /** * @ManyToOne(targetEntity="User",inversedBy="favoriteRecipes") */private $user;/** * @ManyToOne(targetEntity="Recipe",inversedBy="favoriteRecipes") */private $recipe; 食谱 /** * @OneToMany(targ
我的学说实体内有以下关系:

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 EntitiesFavoriteRecipe();
$favoriteRecipe->setRecipe($recipe);
$favoriteRecipe->setUser($user);
$this->_em->persist($favoriteRecipe);
$this->_em->flush();

但是这会抛出以下消息的异常:

A new entity was found through a relationship that was not configured
to cascade persist operations:
EntitiesUser@00000000408bd010000000007cb1380e. Explicitly persist the
new entity or configure cascading persist operations on the
relationship.

如何正确创建和保存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-2/2011/03/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读