Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Doctrine2:PDOException[23000]:SQLSTATE[23000]:完整性约束冲突:1062重复条目';待验证';对于键';名称';_Php_Orm_Pdo_Doctrine Orm - Fatal编程技术网

Php Doctrine2:PDOException[23000]:SQLSTATE[23000]:完整性约束冲突:1062重复条目';待验证';对于键';名称';

Php Doctrine2:PDOException[23000]:SQLSTATE[23000]:完整性约束冲突:1062重复条目';待验证';对于键';名称';,php,orm,pdo,doctrine-orm,Php,Orm,Pdo,Doctrine Orm,我有以下型号: 用户状态: <?php namespace Base; /** @Entity @Table(name="user_status") */ class UserStatus extends \Skeleton\Base { /** * @Id @Column(type="integer") * @GeneratedValue(strategy="AUTO") */ protected $id; /** @Column

我有以下型号:

用户状态:

<?php
namespace Base;

/** @Entity @Table(name="user_status") */
class UserStatus extends \Skeleton\Base {
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /** @Column(length="256") */
    protected $name;

    /**
     * @OneToMany(targetEntity="Base\User", mappedBy="Status")
     */
    protected $Users;
}
控制器代码告诉我:

PDOException [ 23000 ]: SQLSTATE[23000]: Integrity constraint violation: 1062 
Duplicate entry 'To be authenticated' for key 'name'
不用说,但是加载的UserStatus的名称是“待验证”。不知何故,它试图将UserStatus插入数据库,而不是持久化UserStatus的OneToMany关系

谁能告诉我这个代码有什么问题吗

@beberlei:当我删除该行时,我会得到一个不同的错误:

InvalidArgumentException [ 0 ]: A new entity was found through a relationship that was
not configured to cascade persist operations: Base\UserStatus@00000000485b93d40000000031ebec33. Explicitly persist the new entity or 
configure cascading persist operations on the relationship.

您正在为已被管理的实体调用$em->persist($userStatus)。此方法仅适用于新实体。我不确定这是否解决了问题。

我找到了解决方案。我的$entityManager不是单身汉。这意味着,
$UserStatus
是由entitymanager的另一个实例加载的,然后是我试图保存它的那个实例。我试图保存它的那个人还不知道$UserStatus,所以它试图插入它。通过使my
\Doctrine\Configure\EntityManager::instance()
返回EntityManager的单一实例,问题得到解决。

Tnx用于共享。然而,在我看来,单身汉总是有风险的。依赖注入可以帮助解决类似的情况,而不引入由单例引起的问题。只要2美分。
PDOException [ 23000 ]: SQLSTATE[23000]: Integrity constraint violation: 1062 
Duplicate entry 'To be authenticated' for key 'name'
InvalidArgumentException [ 0 ]: A new entity was found through a relationship that was
not configured to cascade persist operations: Base\UserStatus@00000000485b93d40000000031ebec33. Explicitly persist the new entity or 
configure cascading persist operations on the relationship.