Doctrine One To One Unidirectional Error saving child

Doctrine One To One Unidirectional Error saving child

我正在使用 Doctrine 2.6 不使用 symfony 只是在我自己的平台上工作。我一直在网上和这里的堆栈上到处搜索,我发现的每个解决方案都以同样的错误结束。

请帮忙

用户实体

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;

个人资料实体

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;

控制器中的用法

    $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();

这是当我尝试持久化并刷新子实体时由 doctrine 产生的错误。

( ! ) 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

我尝试在两个实体上添加 cascade={"persist"} 结果出现以下错误

( ! ) 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

像这样更改 UserProfile::setUser 方法:

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

问题出在我构建的桥 class 中的一个方法,该方法试图持久化和刷新,但管理器中有多个实体。

一旦我删除它并调用实体管理器坚持然后刷新子实体一切正常。

一切都结束了,运行 现在。