2
votes

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

2
It's saying that you are trying to insert the same email, did u try to change the email? Idk why you are trying to re-insert the data that you get from your db? Anyway try to add cascade={"persist"} only in the Profile::user not in both.Did
My bad I mean try to add cascade={"persist"} only in the User::profile not in both.Did
tried all variations of persist. I believe the problem is lying in the persist and flush operations. I am testing out now and it seems to be working correctly by persisting and flushing the child entity the error seems to go away and the association is intact.MadScientist
Can you show UserProfile#setUser() and …setAccount() methods? It's strange that you've get $this->user from the Entity Manager and it claims that this is new entity.Storm
Are you detaching something somewhere?Stratadox

2 Answers

2
votes

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.

0
votes

Change UserProfile::setUser method like this:

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