Symfony2 / Doctrine2 throwing index error when flushing the entity manager

后端 未结 2 1764
[愿得一人]
[愿得一人] 2020-12-15 08:26

Three entities are involved here: Deployment, DeploymentStep, and DeploymentStatusLog. I\'ll start by pasting the relevan

2条回答
  •  一整个雨季
    2020-12-15 09:28

    This is not a "solution" but I hope it provides more troubleshooting information to others.

    I had this same exact error when following Doctrine's recommendation for batch processing mass inserts. FYI, this is from a controller (not a lifecycle event like the other answer mentions).

    Doctrine's Recommended Method

    $batchSize = 20;
    for ($i = 1; $i <= 10000; ++$i) {
        $user = new CmsUser;
        $user->setStatus('user');
        $user->setUsername('user' . $i);
        $user->setName('Mr.Smith-' . $i);
        $em->persist($user);
        if (($i % $batchSize) === 0) {
            $em->flush();
            $em->clear(); // Detaches all objects from Doctrine!
        }
    }
    $em->flush(); //Persist objects that did not make up an entire batch
    $em->clear();
    

    When I used something similar to the recommended code above, it would fail with the same errors after about 2000 inserts.

    Change Order of Persist

    The order I persisted the 10,000 entities made no difference. And it made no difference if I persisted, flushed and cleared every single loop (not ideal, but I tried it).

    Remove Clear

    If I just commented out the $em->clear() within the $batchSize check and made it only clear after the loop finished, then it timed out:

    Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/core/cms/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 541
    

    So I put a set_time_limit(3600) on the script to prevent time out, and it no longer gave the error, but it ran out of memory :P

    This would suggest that the problem occurs when $em->clear() is executed within the loop. This is consistent with other questions. Unfortunately, without $em->clear(), you run out of memory quickly.

    Disable Event Listeners

    The other answer mentions that Event Listeners may be causing this, so I disabled them like it suggested:

    foreach ($em->getEventManager()->getListeners() as $event => $listeners) {
        foreach ($listeners as $listener) {
            $em->getEventManager()->removeEventListener($event, $listener);
        }
    }
    

    But that didn't work either... although it seems like this could be the issue, somehow, and that this doesn't actually successfully disable them.

    Validate Schema

    I also validated my schema:

    php app/console doctrine:schema:validate
    

    And no errors are reported.

    [Mapping]  OK - The mapping files are correct.
    [Database] OK - The database schema is in sync with the mapping files.
    

提交回复
热议问题