DoctrineFixturesBundle: Loading Fixtures with PHP8 throws a PDO exception "There is no active transaction"

When running the command doctrine:fixtures:load all my fixtures are load, then a critical PDO exception is thrown

/srv # bin/console doctrine:fixtures:load

 Careful, database "casechek_api2" will be purged. Do you want to continue? (yes/no) [no]:
 > yes

   > purging database
   > loading App\Fixtures\Core\TaskFixtures
...
   > loading App\Fixtures\Cases\VendorRequestTrayScanFixtures
[2022-01-26 15:26:20] console.CRITICAL: Error thrown while running command "{command}". Message: "{message}" {"exception":"[object] (PDOException(code: 0): There is no active transaction at /srv/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:1853)","command":"doctrine:fixtures:load","message":"There is no active transaction"} []
15:26:20 CRITICAL  [console] Error thrown while running command "doctrine:fixtures:load". Message: "There is no active transaction" ["exception" => PDOException { …},"command" => "doctrine:fixtures:load","message" => "There is no active transaction"]

In Connection.php line 1853:

  There is no active transaction


doctrine:fixtures:load [--append] [--group GROUP] [--em EM] [--purger PURGER] [--purge-exclusions PURGE-EXCLUSIONS] [--shard SHARD] [--purge-with-truncate] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command>

I’ve seen this issue opened for migrations, but doctrine:migrations:migrate works fine for me. Is there an argument or configuration option we can use to disable auto-commit?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 16 (5 by maintainers)

Most upvoted comments

Here is a workaround that works for me. I created a custom purger that wraps an orginal ORMPurger. After calling the original purger it re-opens the transaction, if necessary.

It also contains a fix for the foreign key problem that we already had in older application (see comment above). The purger factory service provides a parameter to set whether to disable foreign key checks during purging. (If set, it will also enable foreign key checks after purging.) So you can set the behavior in services.yaml.

// src/Purger/MysqlOrmPurgerFactory.php
namespace App\Purger;

use Doctrine\Bundle\FixturesBundle\Purger\PurgerFactory;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\Common\DataFixtures\Purger\PurgerInterface;
use Doctrine\ORM\EntityManagerInterface;

class MysqlOrmPurgerFactory implements PurgerFactory
{
    public function __construct(private readonly bool $disableForeignKeyChecks = false) {
    }

    /**
     * {@inheritDoc}
     */
    public function createForEntityManager(
        ?string $emName,
        EntityManagerInterface $em,
        array $excluded = [],
        bool $purgeWithTruncate = false
    ): PurgerInterface
    {
        $purger = new MysqlOrmPurger($em, $excluded);
        $purger->setPurgeMode(
            $purgeWithTruncate ? ORMPurger::PURGE_MODE_TRUNCATE : ORMPurger::PURGE_MODE_DELETE);
        $purger->setDisableForeignKeyChecks($this->disableForeignKeyChecks);
        return $purger;
    }
}
// src/Purger/MysqlOrmPurger.php
namespace App\Purger;

use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\Common\DataFixtures\Purger\ORMPurgerInterface;
use Doctrine\DBAL\Exception;
use Doctrine\ORM\EntityManagerInterface;

class MysqlOrmPurger implements ORMPurgerInterface
{

    private readonly ORMPurgerInterface $purger;
    private bool $disableForeignKeyChecks = false;

    public function __construct(?EntityManagerInterface $em = null,
                                array $excluded = [])
    {
        $this->purger = new ORMPurger($em, $excluded);
    }

    /**
     * Disable foreign key checks before purging. Enable foreign key checks after purging
     */
    public function setDisableForeignKeyChecks(bool $disableForeignKeyChecks): void
    {
        $this->disableForeignKeyChecks = $disableForeignKeyChecks;
    }

    public function getDisableForeignKeyChecks(): bool
    {
        return $this->disableForeignKeyChecks;
    }


    /**
     * Set the purge mode
     *
     * @param int $mode
     *
     * @return void
     */
    public function setPurgeMode($mode): void
    {
        $this->purger->setPurgeMode($mode);
    }

    /**
     * Get the purge mode
     *
     * @return int
     */
    public function getPurgeMode()
    {
        return $this->purger->getPurgeMode();
    }

    public function setEntityManager(EntityManagerInterface $em)
    {
        $this->purger->setEntityManager($em);
    }

    /**
     * Retrieve the EntityManagerInterface instance this purger instance is using.
     *
     * @return EntityManagerInterface
     */
    public function getObjectManager()
    {
        return $this->purger->getObjectManager();
    }

    /**
     * @throws Exception
     */
    public function purge()
    {
        $conn = $this->getObjectManager()->getConnection();
        /** @var \PDO $pdo */
        $pdo = $conn->getNativeConnection();
        if(!($pdo instanceof \PDO)) {
            throw new \Exception('Unsupported native connection');
        }
        $wasInTransaction = $pdo->inTransaction();

        if ($this->disableForeignKeyChecks) {
            $conn->executeStatement('SET FOREIGN_KEY_CHECKS = 0');
        }

        $this->purger->purge();

        if ($this->disableForeignKeyChecks) {
            $conn->executeStatement('SET FOREIGN_KEY_CHECKS = 1');
        }

        if ($wasInTransaction && !$pdo->inTransaction()) {
            $pdo->beginTransaction();
        }
    }
}
# config/services.yaml
services:
    App\Purger\MysqlOrmPurgerFactory:
        arguments:
            $disableForeignKeyChecks: true
        tags:
            - { name: 'doctrine.fixtures.purger_factory', alias: 'mysql_purger' }

Then call the command with:

php bin/console doctrine:fixtures:load --purge-with-truncate --purger=mysql_purger

I’m still having this error using:

  • PHP8
  • doctrine/doctrine-fixtures-bundle v3.4.2
  • doctrine/data-fixtures v1.5.3

Update (2/19/2024): This is still a problem over a year later. Also, the fix below does not work if use_savepoints is enabled in the doctrine config, I get the error SAVEPOINT DOCTRINE_2 does not exist.

A work-around is to start my own transaction in the load function, do my work, commit and then start a new transaction before returning from the method:

    public function load(ObjectManager $manager)
    {
        /** @var Connection db */
        $db = $manager->getConnection();

        // start new transaction
        $db->beginTransaction();

        // do work ...

        // commit and re-start new transaction
        $db->commit();
        // work-around bug "There is no active transaction" in data-fixtures in php8
        $db->beginTransaction();
    }

CREATE PROCEDURE is listed in the link I mentioned above. ಠ_ಠ