MadScientist
MadScientist

Reputation: 565

Doctrine One To One Unidirectional Error saving child

I am working in Doctrine 2.6 Not using symfony just working within my own platform. I been searching everywhere on the net and here on stack and every solution I have found is ending in the same error.

Please help

User Entity

use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\UniqueConstraint;

/**
 * @ORM\Entity
 * @ORM\Table(name="user",uniqueConstraints={@UniqueConstraint(name="user_idx", columns={"email"})})
 */
class User
{

/**
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 * @ORM\Column(type="integer")
 * @var int
 */
protected $id;

/**
 * @ORM\Column(type="string")
 * @var string
 */
protected $email;

 /**
 * @ORM\OneToOne(targetEntity="UserProfile", mappedBy="user")
 */
protected $profile;

Profile entity

use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\JoinColumn;

/**
 * @ORM\Entity
 * @ORM\Table(name="user_profile")
 */
class UserProfile
{

    /**
     * @var integer
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @var User
     * @ORM\OneToOne(targetEntity="User",inversedBy="profile")
     * @JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;

Usage in Controller

    $this->user = $this->entityManager->getRepository('User')->findOneBy(['email' => $login])


    $profile = new \UserProfile();
    $profile
        ->setUser($this->user)
        ->setAccountType(1);

    $this->entityManager->persist($profile);
    $this->entityManager->flush();

This is the error generated by doctrine when I try to persist and flush the child entity.

( ! ) Fatal error: Uncaught Doctrine\ORM\ORMInvalidArgumentException: A new entity was found through the relationship 'UserProfile#user' that was not configured to cascade persist operations for entity: User@0000000045b82749000000006e7c58e6. 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"}). If you cannot find out which entity causes the problem implement 'User#__toString()' to get a clue. in D:\wamp64\coresys\vendor\doctrine\orm\lib\Doctrine\ORM\ORMInvalidArgumentException.php on line 102

( ! ) Doctrine\ORM\ORMInvalidArgumentException: A new entity was found through the relationship 'UserProfile#user' that was not configured to cascade persist operations for entity: User@0000000045b82749000000006e7c58e6. 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"}). If you cannot find out which entity causes the problem implement 'User#__toString()' to get a clue. in D:\wamp64\coresys\vendor\doctrine\orm\lib\Doctrine\ORM\ORMInvalidArgumentException.php on line 102

I have tried adding cascade={"persist"} on both entities and the following error is the result

( ! ) Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '[email protected]' for key 'user_idx' in D:\wamp64\coresys\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\AbstractMySQLDriver.php on line 55 ( ! ) Doctrine\DBAL\Exception\UniqueConstraintViolationException: An exception occurred while executing 'INSERT INTO user (email, firstName, lastName, password, accessRole, status, createdOn, loggedInOn) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' with params ["[email protected]", "Matthew", "Chitty", {}, "ROLE_ADMIN", null, null, null]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '[email protected]' for key 'user_idx' in D:\wamp64\coresys\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\AbstractMySQLDriver.php on line 55

Upvotes: 2

Views: 842

Answers (2)

MadScientist
MadScientist

Reputation: 565

The problem was a method in the bridge class I have built that was trying to persist and flush but there was multiple entities in the manager.

Once I removed that and called entity manager persist then flush on the child entity everything worked perfectly.

It is all up and running now.

Upvotes: 2

delirehberi
delirehberi

Reputation: 522

Change UserProfile::setUser method like this:

public function setUser($user) {
  $user->setProfile($this);
  $this->user = $user;
  return $this;
}

Upvotes: 0

Related Questions