php - 教义一对一单向错误拯救 child

标签 php doctrine-orm

我正在 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;

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

这是当我尝试保留并刷新子实体时由原则生成的错误。

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

最佳答案

问题是我构建的桥接类中的一个方法,该方法试图持久化并刷新,但管理器中有多个实体。

一旦我删除了它并调用实体管理器持久化,然后刷新子实体,一切就完美了。

现在一切都已启动并正在运行。

关于php - 教义一对一单向错误拯救 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58423505/

相关文章:

doctrine-orm - 带有 OR 条件的原则 findBy

php - 不能用 $stmt->bind_param() 通过引用传递常量变量?

php - 将另一个表中的字段添加到结果

php - 从 URL 字符串获取 GET 变量

Symfony2 ObjectNormalizer 非规范化回调

php - 交响乐 2.3 : CollectionRegionDoctrineCommand not found when update vendors and clear cache

php - 使用 PHP 将 CSV 导入 MySQL

php - Laravel 4 - 获取 N :N Relation exists 中条目的记录

php - 如何在 `collection` 字段 Symfony 2.1 中将选项传递给 CustomType?

php - Symfony和doctrine从sql数据库查询中获取空响应