8

I have the following relationships within my doctrine entities:

FavoriteRecipe

/**
 * @ManyToOne(targetEntity="User", inversedBy="favoriteRecipes")
 */
private $user;

/**
 * @ManyToOne(targetEntity="Recipe", inversedBy="favoriteRecipes")
 */
private $recipe;

Recipe

/**
 * @OneToMany(targetEntity="FavoriteRecipe", mappedBy="user")
 */
private $favoriteRecipes;

User

/**
 * @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:

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.

How can I correctly create and save a FavoriteRecipe entity?

1 Answer 1

8

Did you set the cascade option for all your relational entities? This is done by setting the cascade property for example: cascade={"persist", "remove"}

Maybe this page:https://www.doctrine-project.org/projects/doctrine-orm/en/2.9/reference/working-with-associations.html

3
  • 1
    To which side of the association do I add the cascade option? The owning side or the inverse side?
    – moteutsch
    Sep 6, 2011 at 11:59
  • It depends on the behavior you want but in your situation I think the inverse side. I would say try it, and look what happends in the database. Sep 6, 2011 at 13:10
  • 3
    And i would add "and come back here to share you findings". People helped you, don't forget to help people :)
    – Ninj
    Jan 16, 2015 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.