Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.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 EntityManager因唯一密钥冲突而关闭_Php_Doctrine Orm - Fatal编程技术网

Php EntityManager因唯一密钥冲突而关闭

Php EntityManager因唯一密钥冲突而关闭,php,doctrine-orm,Php,Doctrine Orm,我的应用程序使用Doctrine2 EntityManager。在循环中,我喜欢将5个数据集插入到不同的表中 public function insertRate($dateObj, $rate) { $model = new TestModel(); $model ->setDate($dateObj) ->setRate($rate); $this->getEntityManager()->pe

我的应用程序使用Doctrine2 EntityManager。在循环中,我喜欢将5个数据集插入到不同的表中

public function insertRate($dateObj, $rate)
{       
    $model = new TestModel();

    $model
        ->setDate($dateObj)
        ->setRate($rate);

    $this->getEntityManager()->persist($model);

    try {
        $this->getEntityManager()->flush();
    } catch (\Exception $e) {
        $currentTime = new \DateTime();
        echo 'Duplicate entry <br>' .
            'At Time ' . $currentTime->format('Y-m-d H:i') . "<hr>";
    }
}
使用此workarround,将设置一个新的entityManger,但自动增量ID与1、2、3不同。。。但是1,5,10,15

我不想检查是否由于额外的选择而存在唯一的约束冲突


如何修复此问题,以及此工作区是否正常?

在事务失败后,您的EntityManager将被锁定–这是预期的行为。您可以通过:


事务失败后,EntityManager将被锁定—这是预期的行为。您可以通过:


你能给我们看一下插入数据的代码吗?不管怎样,依赖异常最终都会咬到你。执行查询并完成它。插入失败时自动增量的间隙由数据库引擎的设计给出。下面就是。你能给我们看一下插入数据的代码吗?不管怎样,依赖异常最终都会咬到你。执行查询并完成它。插入失败时自动增量的间隙由数据库引擎的设计给出。下面是。对自动inc问题没有帮助,这是一个愚蠢的问题。@Cerad我想自动增量是由数据库处理的,所以它的问题与Doctrine EntityManager无关。@Cerad也不总是使用
int
作为主键。getDoctrine()在我的上下文中不可用。我怎样才能访问它?@jkucharovic我同意。这就是为什么我称之为愚蠢的要求。这对解决auto inc问题没有帮助,因为auto inc问题有点愚蠢。@Cerad我想autoincrement是由数据库处理的,所以它的问题与Doctrine EntityManager无关。@Cerad也不总是使用
int
作为主键。getDoctrine()在我的上下文中不可用。我怎样才能访问它?@jkucharovic我同意。这就是为什么我称之为愚蠢的要求。
$currentEm = $this->em;
if (!$currentEm->isOpen()) {
    $this->em = $currentEm->create(
        $currentEm->getConnection(), $currentEm->getConfiguration());
}
$this->getDoctrine()->resetManager();