doctrine/data-fixtures
Doctrine Data Fixtures provides a simple way to define, manage, and execute fixture classes for loading sample or test data into Doctrine ORM or ODM. Useful for seeding databases, repeatable test setups, and development environments with consistent data.
DryRunORMExecutor) and purgers allow for pre-execution validation or conditional seeding, which is valuable for CI/CD pipelines or partial data loads.doctrine/dbal and doctrine/orm packages are prerequisites for seamless integration.php artisan db:seed-fixtures) for CLI-driven workflows.boot() or register()).doctrine/orm:^3.0).DatabaseMigrations or RefreshDatabase traits.final classes and removed deprecated features (e.g., callable loggers) may require refactoring of custom fixture classes.ORMExecutor can be slow for large datasets; DryRunExecutor adds minimal overhead but may not suit all use cases.ModelCreating), requiring manual hooks.doctrine/dbal and doctrine/orm).composer require doctrine/data-fixtures ^2.2
composer require doctrine/dbal ^3.6 doctrine/orm ^3.0 # If not already present
AbstractFixture classes.
// Before (Eloquent Factory)
$user = User::factory()->create(['name' => 'John']);
// After (Doctrine Fixture)
$user = new User();
$user->name = 'John';
$manager->persist($user);
$this->addReference('user-1', $user); // For later references
Executor-compatible classes.// app/Console/Commands/LoadFixtures.php
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
protected $signature = 'db:load-fixtures';
public function handle() {
$purger = new ORMPurger($entityManager);
$executor = new ORMExecutor($entityManager, $purger);
$executor->execute([
new UserFixture(),
new PostFixture(),
]);
}
// app/Providers/FixtureServiceProvider.php
public function register() {
$this->app->singleton('fixtures.executor', function () {
return new ORMExecutor($this->app->make('doctrine')->getManager());
});
}
| Fixture Version | Doctrine ORM | Doctrine DBAL | PHP Version |
|---|---|---|---|
| 2.2.x | 3.0+ | 3.6+ | 8.1+ |
| 2.1.x | 2.11+ | 3.4+ | 8.0+ |
RefreshDatabase trait:
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
beforeEach(function () {
$executor = new ORMExecutor($this->app->make('doctrine')->getManager(), new ORMPurger($this->app->make('doctrine')->getManager()));
$executor->execute([new TestFixture()]);
});
php artisan db:load-fixtures for local testing.config/fixtures.php).php artisan db:load-fixtures --purge in CI to ensure a clean slate.DryRunExecutor to validate fixtures without persisting.new UserFixture() before new PostFixture()).Fixtures/2023_05_01_User.php) to track changes.doctrine/data-fixtures and doctrine/orm for breaking changes (e.g., v2.x’s strict typing).post-update-cmd to validate fixtures:
{
"scripts": {
"post-update-cmd": "php
How can I help you explore Laravel packages today?