Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine2.3 and OneToOne cascade persist doesn't seem to work

I have two entites (User and UserPreferences) that I want to map OneToOne unidirectional.

The code looks something like this:

/**
 * @ORM\Table("users")
 * @ORM\Entity
 */
class User
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     */
    protected $id;

    ...

    /**
     * @ORM\Column(name="user_preferences_id", type="integer")
     * @ORM\OneToOne
     * (
     *      targetEntity="UserPreferences",
     *      cascade={"persist"}
     * )
     */
    protected $userPreferences;

    public function __construct() {
        $this->userPreferences = new UserPreferences();
    }
}


/**
 * @ORM\Table("user_preferences")
 * @ORM\Entity
 */
class UserPreferences extends UserPreferencesEntity
{
    /**
     * @ORM\Id
     * @ORM\Column(name="user_id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    ...
}

Now when a new User is created, userPreferences is initialized with a new UserPreferences object. When trying to persist user, Doctrine throws an Exception, claiming

A new entity was found through the relationship '...\Entity\User#userPreferences' that was not configured to cascade persist operations for entity: ...\Entity\UserPreferences@000000003ae25e5700000000a6eaafc9. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}).

But what else should I do? User#userPreferences is configured to cascade persist but it doesn't. Am I getting something wrong here?

like image 862
dwalldorf Avatar asked Aug 09 '13 09:08

dwalldorf


1 Answers

Ok found the solution:

/**
 * User
 *
 * @ORM\Table("users")
 * @ORM\Entity
 */
class User extends UserEntity
{
    ...

    /**
     * @ORM\OneToOne
     * (
     *      targetEntity="UserPreferences",
     *      cascade={"persist", "remove"},
     *      inversedBy="user"
     * )
     */
    protected $userPreferences;
}

/**
 * @ORM\Table("user_preferences")
 * @ORM\Entity
 */
class UserPreferences extends UserPreferencesEntity
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    protected $id;

    /**
     * @var int
     *
     * @ORM\OneToOne(targetEntity="User", mappedBy="id", cascade={"persist", "remove"})
     */
    protected $user;

    ...
}

First of all I had to specify mappedBy and inversedBy (which I already tried before but in the wrong direction - mappedBy at the owning side, inversedBy at inversed side). Also I thought that the inversed side did not need to have a separate id and I tried to use the id of the owning side (User#id) as primary key for this one too.

  • http://docs.doctrine-project.org/en/latest/reference/unitofwork-associations.html
  • http://docs.doctrine-project.org/en/latest/reference/association-mapping.html
like image 171
dwalldorf Avatar answered Sep 20 '22 12:09

dwalldorf