原则2:复杂关系中的实体保存

编程入门 行业动态 更新时间:2024-10-10 04:23:20
本文介绍了原则2:复杂关系中的实体保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的学说实体之间有以下关系:

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:复杂关系中的实体保存

本文发布于:2023-10-23 02:49:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519526.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   原则   关系

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!