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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Doctrine 2:在复杂的关系中保存实体脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我的学说实体内有以下关系:

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();

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

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

脚本宝典总结

以上是脚本宝典为你收集整理的php – Doctrine 2:在复杂的关系中保存实体全部内容,希望文章能够帮你解决php – Doctrine 2:在复杂的关系中保存实体所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。