doctrine/data-fixtures
Doctrine Data Fixtures extension for Doctrine ORM/ODM. Provides a simple, structured way to define, manage, and execute data fixtures for loading seed/test data into your database, with tooling and docs to support fixture organization and execution.
Install the package:
composer require doctrine/data-fixtures
Create a fixture class (e.g., database/fixtures/UserFixture.php):
namespace Database\Fixtures;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use App\Models\User;
class UserFixture extends Fixture
{
public function load(ObjectManager $manager)
{
$user = new User();
$user->name = 'John Doe';
$user->email = 'john@example.com';
$manager->persist($user);
$this->addReference('user', $user); // Reference for later use
$manager->flush();
}
}
Load fixtures via Artisan:
php artisan db:fixtures:load
(Note: Laravel doesn’t have a built-in command, so you’ll need to create a custom Artisan command or use a package like doctrine/doctrine-bundle for Symfony-like integration.)
Alternative: Load programmatically (e.g., in a service or test):
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\Common\DataFixtures\Loader;
$loader = new Loader();
$loader->addFixture(new UserFixture());
$purger = new ORMPurger($entityManager);
$executor = new ORMExecutor($entityManager, $purger);
$executor->execute($loader->getFixtures());
UserFixtures, ProductFixtures) and use dependency injection to load them in order.dependsOn() to enforce execution order:
class ProductFixture extends Fixture
{
public function load(ObjectManager $manager)
{
// ...
}
public function getDependencies()
{
return [UserFixture::class]; // Runs after UserFixture
}
}
$this->addReference('admin_user', $adminUser);
$user = $this->getReference('user');
$purger = new ORMPurger($entityManager);
$executor = new ORMExecutor($entityManager, $purger);
use Doctrine\Common\DataFixtures\Executor\DryRunORMExecutor;
$executor = new DryRunORMExecutor($entityManager);
ArrayFixtureuse Doctrine\Common\DataFixtures\ArrayFixture;
class UserArrayFixture extends ArrayFixture
{
public function getEntities()
{
return [
['name' => 'Alice', 'email' => 'alice@example.com'],
['name' => 'Bob', 'email' => 'bob@example.com'],
];
}
public function getEntityClass()
{
return User::class;
}
}
DatabaseSeeder (Laravel) or a custom Artisan command:
// In DatabaseSeeder.php
public function run()
{
$this->call([
FixturesCommand::class, // Custom command to load fixtures
]);
}
DatabaseMigrations and DatabaseSeeders in phpunit.xml):
<env name="DB_DISCOVERY" value="true"/>
<env name="DB_SEED" value="true"/>
ORMPurger deletes all entities in the database. Use cautiously in production.use Doctrine\Common\DataFixtures\Purger\PurgerInterface;
class CustomPurger implements PurgerInterface
{
public function purge(ObjectManager $manager)
{
$manager->createQuery('DELETE FROM App\Models\User')->execute();
}
}
"1") as reference names—they can conflict with auto-increment IDs. Use strings like "user_1".ArrayFixture or batch inserts to avoid memory issues.$manager->beginTransaction();
try {
$executor->execute($loader->getFixtures());
$manager->commit();
} catch (\Exception $e) {
$manager->rollBack();
throw $e;
}
EntityManager is bound to the doctrine service. Inject it via constructor or resolve it:
$entityManager = app('doctrine')->getManager();
php artisan make:command LoadFixtures
// In LoadFixturesCommand.php
protected $signature = 'db:fixtures:load';
protected $description = 'Load Doctrine fixtures';
public function handle()
{
$loader = new Loader();
$loader->addFixture(new UserFixture());
$purger = new ORMPurger(app('doctrine')->getManager());
$executor = new ORMExecutor(app('doctrine')->getManager(), $purger);
$executor->execute($loader->getFixtures());
}
$executor = new DryRunORMExecutor($entityManager);
$config = Setup::createAnnotationMetadataConfiguration([__DIR__.'/../src'], true);
$config->setProxyDir(__DIR__.'/../var/proxies');
$config->setProxyNamespace('Proxies');
$config->setAutoGenerateProxyClasses(true);
$entityManager = EntityManager::create(['url' => 'sqlite:///:memory:'], $config);
$entityManager->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
AbstractFixture or ArrayFixture for reusable logic.preLoad, postLoad) via Doctrine’s event system.ReferenceException: Occurs when a reference is used before it’s defined. Ensure dependsOn() or proper ordering.InvalidArgumentException: Thrown if a fixture class isn’t valid (e.g., missing load() method).doctrine/orm and doctrine/data-fixtures versions are compatible (e.g., v2.x of fixtures works with ORM 3.x+).MongoDBPurger and MongoDBExecutor for Doctrine MongoDB ODM:
$purger = new MongoDBPurger($dm);
$executor = new MongoDBExecutor($dm, $purger);
How can I help you explore Laravel packages today?