Friday, 1 November 2019

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 'sales@mk2solutions.com' 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 ["sales@mk2solutions.com", "Matthew", "Chitty", {}, "ROLE_ADMIN", null, null, null]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'sales@mk2solutions.com' for key 'user_idx' in D:\wamp64\coresys\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\AbstractMySQLDriver.php on line 55



from Doctrine One To One Unidirectional Error saving child

No comments:

Post a Comment