### Installation
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Install the bundle using composer and register it in config/bundles.php.
```bash
composer require doctrine/doctrine-fixtures-bundle
```
```php
return [
// ...
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['all' => true],
];
```
--------------------------------
### Complex Setup Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/LoadDataFixturesDoctrineCommand.md
Loads specific fixtures ('users', 'products') for a secondary entity manager, excludes 'system_settings' from purging, and uses truncate for speed.
```bash
php bin/console doctrine:fixtures:load \
--group=users \
--group=products \
--em=secondary \
--purge-exclusions=system_settings \
--purge-with-truncate
```
--------------------------------
### Example - Full Configuration
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/ORMPurgerFactory.md
Example demonstrating full configuration of createForEntityManager.
```php
$purger = $factory->createForEntityManager(
'secondary', // EM name
$secondaryEm, // EM instance
excluded: ['doctrine_migrations_versions'],
purgeWithTruncate: true,
);
```
--------------------------------
### Example Usage of createForEntityManager
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/PurgerFactory.md
Example demonstrating how to use the createForEntityManager method with different options.
```php
use Doctrine\Bundle\FixturesBundle\Purger\ORMPurgerFactory;
$factory = new ORMPurgerFactory();
// Create a basic purger
$purger = $factory->createForEntityManager(
'default',
$em,
);
// Create a purger that excludes certain tables
$purger = $factory->createForEntityManager(
'default',
$em,
excluded: ['settings', 'migrations'],
);
// Create a purger using TRUNCATE instead of DELETE
$purger = $factory->createForEntityManager(
'default',
$em,
excluded: [],
purgeWithTruncate: true,
);
// Use the purger
$executor = new ORMExecutor($em, $purger);
$executor->execute($fixtures);
```
--------------------------------
### PHP Service Configuration Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/configuration.md
Example of configuring fixtures and their tags in PHP.
```php
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use App\DataFixture\UserFixture;
return static function (ContainerConfigurator $container): void {
$services = $container->services()
->defaults()
->autowire()
->autoconfigure();
// Auto-discover fixtures
$services->load('App\DataFixture\', '../src/DataFixture')
->exclude('../src/DataFixture/{base,AbstractFixture.php}');
// Add explicit groups if needed
$services->set(UserFixture::class)
->tag('doctrine.fixture.orm')
->tag('doctrine.fixture.orm', ['group' => 'users'])
->tag('doctrine.fixture.orm', ['group' => 'demo']);
};
```
--------------------------------
### getPath() Usage Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/DoctrineFixturesBundle.md
Example demonstrating how to get the absolute path to the bundle's directory using the getPath() method.
```php
$bundle = new DoctrineFixturesBundle();
$path = $bundle->getPath();
// Returns: /path/to/vendor/doctrine/doctrine-fixtures-bundle
```
--------------------------------
### Load Fixtures
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Commands to load fixtures, with options to append or dry-run.
```bash
# Load all fixtures (prompts for confirmation, purges database)
php bin/console doctrine:fixtures:load
# Load without purging
php bin/console doctrine:fixtures:load --append
# Test without committing changes
php bin/console doctrine:fixtures:load --dry-run
```
--------------------------------
### Install DoctrineFixturesBundle
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/docs/index.rst
Commands to install the bundle via Composer, depending on whether Symfony Flex is used.
```terminal
$ composer require --dev orm-fixtures
```
```terminal
$ composer require --dev doctrine/doctrine-fixtures-bundle
```
--------------------------------
### YAML Service Configuration Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/configuration.md
Example of configuring fixtures and their tags in YAML.
```yaml
services:
# Enable autowiring by default
_defaults:
autowire: true
autoconfigure: true
# Fixture services - automatically tagged via ORMFixtureInterface
App\DataFixture\:
resource: '../src/DataFixture'
# Add manual groups if needed
App\DataFixture\UserFixture:
tags:
- { name: doctrine.fixture.orm, group: users }
- { name: doctrine.fixture.orm, group: demo }
App\DataFixture\AdminFixture:
tags:
- { name: doctrine.fixture.orm, group: admin }
```
--------------------------------
### Console Command Reference
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Reference for the doctrine:fixtures:load command and its options.
```bash
php bin/console doctrine:fixtures:load -h
```
--------------------------------
### Create Your First Fixture
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Create a PHP class that extends Fixture and implements the load method.
```php
setEmail('admin@example.com');
$user->setPassword('hashed_password');
$manager->persist($user);
$manager->flush();
}
}
```
--------------------------------
### Direct Implementation Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/ORMFixtureInterface.md
Example of directly implementing ORMFixtureInterface.
```php
use Doctrine\Bundle\FixturesBundle\ORMFixtureInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectManager;
class UserFixture implements ORMFixtureInterface
{
public function load(ObjectManager $manager): void
{
$user = new User();
$user->setEmail('user@example.com');
$manager->persist($user);
$manager->flush();
}
}
```
--------------------------------
### Console Command Options Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/PurgerFactory.md
Example showing various options for the doctrine:fixtures:load command, including purger selection, exclusions, and purge mode.
```bash
php bin/console doctrine:fixtures:load \
--purger=default \
--purge-exclusions=table1 \
--purge-exclusions=table2 \
--purge-with-truncate
```
--------------------------------
### addFixture Method Example 1
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/SymfonyFixturesLoader.md
Example of adding a fixture and its automatic grouping.
```php
$fixture = new UserFixture();
$loader->addFixture($fixture);
// Automatically groups as: ['UserFixture']
```
--------------------------------
### Autoconfiguration Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/ORMFixtureInterface.md
Example of how the DoctrineFixturesExtension registers ORM fixtures.
```php
$container->registerForAutoconfiguration(ORMFixtureInterface::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG);
```
--------------------------------
### Access the Doctrine registry
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/LoadDataFixturesDoctrineCommand.md
Example of how to access the Doctrine registry within a fixture to get an entity manager.
```php
$em = $this->getDoctrine()->getManager('default');
```
--------------------------------
### Organize Fixtures with Groups
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Implement FixtureGroupInterface to organize fixtures into groups.
```php
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
use Doctrine\Persistence\ObjectManager;
class AdminFixture extends Fixture implements FixtureGroupInterface
{
public static function getGroups(): array
{
return ['admin', 'demo'];
}
public function load(ObjectManager $manager): void
{
$admin = new User();
$admin->setEmail('admin@example.com');
$admin->setRole('ROLE_ADMIN');
$manager->persist($admin);
$manager->flush();
}
}
```
--------------------------------
### Dependency Handling Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/overview.md
PHP code example demonstrating fixture dependencies using DependentFixtureInterface.
```php
class CommentFixture extends Fixture implements DependentFixtureInterface
{
public function getDependencies(): array
{
return [UserFixture::class, ProductFixture::class];
}
}
// When loading:
php bin/console doctrine:fixtures:load --group=comments
// Actually loads: UserFixture → ProductFixture → CommentFixture
```
--------------------------------
### Fixture Load Method Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/Fixture.md
Example of implementing the load method in a ProductFixture class to create and persist data.
```php
class ProductFixture extends Fixture
{
public function load(ObjectManager $manager): void
{
$product = new Product();
$product->setName('Laptop');
$product->setPrice(999.99);
// Optional: store reference for other fixtures
$this->addReference('product-laptop', $product);
// Persist to be flushed after load completes
$manager->persist($product);
$manager->flush();
}
}
```
--------------------------------
### Create a Fixture
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/README.md
Example of how to create a fixture class.
```php
use Doctrine\Bundle\FixturesBundle\Fixture;
class UserFixture extends Fixture
{
public function load(ObjectManager $manager): void
{
// Create and persist entities
}
}
```
--------------------------------
### Load Specific Groups
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Commands to load fixtures by group or by class name.
```bash
# Load admin fixtures only
php bin/console doctrine:fixtures:load --group=admin
# Load multiple groups
php bin/console doctrine:fixtures:load --group=admin --group=demo
# Load single fixture by class name
php bin/console doctrine:fixtures:load --group=AdminFixture
```
--------------------------------
### Common Commands - Faster Reset
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Faster reset for tests using --purge-with-truncate.
```bash
php bin/console doctrine:fixtures:load --purge-with-truncate
```
--------------------------------
### Purger Option Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/LoadDataFixturesDoctrineCommand.md
Example of using the --purger option to specify the purger factory.
```bash
# Use the default purger
php bin/console doctrine:fixtures:load --purger=default
# Use a custom MongoDB purger
php bin/console doctrine:fixtures:load --purger=mongodb
```
--------------------------------
### Example Usage of getFixtures
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/SymfonyFixturesLoader.md
Demonstrates how to register fixtures and retrieve them, both all at once and filtered by group.
```php
// Register fixtures
$userFixture = new UserFixture(); // groups: ['users', 'UserFixture']
$adminFixture = new AdminFixture(); // groups: ['admin', 'AdminFixture']
$commentFixture = new CommentFixture(); // groups: ['comments', 'CommentFixture']
// CommentFixture depends on UserFixture
$loader->addFixture($userFixture);
$loader->addFixture($adminFixture);
$loader->addFixture($commentFixture);
// Get all fixtures
$all = $loader->getFixtures();
// Returns: [$userFixture, $adminFixture, $commentFixture] in dependency order
// Get only 'comments' group
$comments = $loader->getFixtures(['comments']);
// Returns: [$userFixture (dependency), $commentFixture]
// Get multiple groups
$multi = $loader->getFixtures(['admin', 'comments']);
// Returns: [$userFixture (dependency), $adminFixture, $commentFixture]
```
--------------------------------
### Common Commands - Add to Existing Data
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Append fixtures without purging the database.
```bash
php bin/console doctrine:fixtures:load --append
```
--------------------------------
### Usage of doctrine.fixtures.provider
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/configuration.md
Example of how to inject and use the doctrine.fixtures.provider service.
```php
public function __construct(FixturesProvider $provider)
{
$this->provider = $provider;
}
```
--------------------------------
### addFixture Method Example 2
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/SymfonyFixturesLoader.md
Example of adding a fixture that implements FixtureGroupInterface and its automatic grouping.
```php
$fixture2 = new AdminUserFixture();
// If AdminUserFixture::getGroups() returns ['admin', 'users']
$loader->addFixture($fixture2);
// Automatically groups as: ['AdminUserFixture', 'admin', 'users']
```
--------------------------------
### Service Configuration Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/LoadDataFixturesDoctrineCommand.md
Example service configuration for LoadDataFixturesDoctrineCommand in Symfony's services.php.
```php
// In config/services.php
->set('doctrine.fixtures_load_command', LoadDataFixturesDoctrineCommand::class)
->args([
service('doctrine.fixtures.provider'),
service('doctrine'),
])
->tag('console.command', ['command' => 'doctrine:fixtures:load'])
```
--------------------------------
### Common Commands - Load Test Fixtures
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Load only test fixtures using the --group option.
```bash
php bin/console doctrine:fixtures:load --group=test
```
--------------------------------
### Entity Manager Option Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/LoadDataFixturesDoctrineCommand.md
Example of using the --em option to specify the entity manager.
```bash
# Load for secondary entity manager
php bin/console doctrine:fixtures:load --em=secondary
```
--------------------------------
### Load specific fixture groups
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Shows how to use the `--group` option to load specific subsets of fixtures.
```bash
# Load minimal structure
php bin/console doctrine:fixtures:load --group=UserFixture --group=ProductFixture
# Load with orders
php bin/console doctrine:fixtures:load --group=orders
# Load complete demo
php bin/console doctrine:fixtures:load --group=demo
```
--------------------------------
### Create a Fixture
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/INDEX.md
Example of creating a custom fixture class.
```php
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
class UserFixture extends Fixture
{
public function load(ObjectManager $manager): void
{
$user = new User();
$user->setEmail('user@example.com');
$manager->persist($user);
$manager->flush();
}
}
```
--------------------------------
### Group Option Examples
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/LoadDataFixturesDoctrineCommand.md
Examples of using the --group option to load specific fixture groups or classes.
```bash
# Load fixtures in the 'users' group
php bin/console doctrine:fixtures:load --group=users
# Load fixtures from multiple groups
php bin/console doctrine:fixtures:load --group=users --group=demo
# Load a single fixture by its class name
php bin/console doctrine:fixtures:load --group=UserFixture
```
--------------------------------
### Example: Multiple Groups
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/overview.md
PHP code example of a fixture implementing FixtureGroupInterface to declare multiple groups.
```php
class AdminUserFixture extends Fixture implements FixtureGroupInterface
{
public static function getGroups(): array
{
return ['admin', 'users'];
}
}
```
--------------------------------
### Best Practice: Declare Dependencies
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Shows how to use `DependentFixtureInterface` to declare dependencies between fixtures.
```php
class OrderFixture extends Fixture implements DependentFixtureInterface
{
public function getDependencies(): array
{
return [UserFixture::class];
}
}
```
```php
class OrderFixture extends Fixture
{
public function load(ObjectManager $manager): void
{
// Hope UserFixture runs first...
}
}
```
--------------------------------
### Create Data Fixtures
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/docs/index.rst
Example of a fixture class extending Fixture to persist entities to the database.
```php
// src/DataFixtures/AppFixtures.php
namespace App\DataFixtures;
use App\Entity\Product;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
class AppFixtures extends Fixture
{
public function load(ObjectManager $manager): void
{
// create 20 products with random prices
for ($i = 0; $i < 20; $i++) {
$product = new Product();
$product->setName('product '.$i);
$product->setPrice(mt_rand(10, 100));
$manager->persist($product);
}
$manager->flush();
}
}
```
--------------------------------
### In Commands
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/SymfonyFixturesLoader.md
Example of loading and executing fixtures within a Symfony console command.
```php
$groups = $input->getOption('group');
$fixtures = $this->fixturesLoader->getFixtures($groups);
if (!$fixtures) {
$ui->error('No fixtures found');
return 1;
}
$executor = new ORMExecutor($em, $purger);
$executor->execute($fixtures, $append);
```
--------------------------------
### Exception Example for createFixture
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/SymfonyFixturesLoader.md
Illustrates the LogicException thrown when an unregistered fixture class is requested.
```text
LogicException: The "App\DataFixture\UnregisteredFixture" fixture class is trying
to be loaded, but is not available. Make sure this class is defined as a service
and tagged with "doctrine.fixture.orm".
```
--------------------------------
### Dependency Interface
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Defines an interface for fixtures that have dependencies on other fixtures.
```php
interface DependentFixtureInterface
{
public function getDependencies(): array; // Return fixture class names
}
```
--------------------------------
### Bundle Registration Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/DoctrineFixturesBundle.md
Example of how to register the DoctrineFixturesBundle in your Symfony application's config/bundles.php file.
```php
// This is called automatically by Symfony when the bundle is loaded.
// In your Symfony app, just register the bundle in config/bundles.php:
return [
// ...
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['all' => true],
];
```
--------------------------------
### Handle Fixture Dependencies
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Ensure fixtures load in the correct order by implementing DependentFixtureInterface.
```php
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
class RoleFixture extends Fixture implements DependentFixtureInterface
{
public function getDependencies(): array
{
return [UserFixture::class]; // Load UserFixture first
}
public function load(ObjectManager $manager): void
{
$user = $this->getReference('user-john');
// ...
}
}
```
--------------------------------
### Auto-Register Fixtures
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Configure services.yaml to auto-discover fixtures in the src/DataFixture/ directory.
```yaml
services:
# Auto-discover fixtures
App\DataFixture\:
resource: '../src/DataFixture'
```
--------------------------------
### Best Practice: Group Related Fixtures
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Demonstrates how to implement `FixtureGroupInterface` to assign fixtures to groups.
```php
class AdminFixture extends Fixture implements FixtureGroupInterface
{
public static function getGroups(): array
{
return ['admin', 'demo'];
}
}
```
```php
// One big file with all fixtures
class AllFixtures extends Fixture
{
public function load(ObjectManager $manager): void
{
// Load 500 users, 1000 products, etc.
}
}
```
--------------------------------
### Fixture Base Class Method Signatures
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Details the essential and optional methods available in the `Fixture` base class.
```php
class MyFixture extends Fixture
{
// Required - implement this
public function load(ObjectManager $manager): void
{
// Create and persist entities
}
// Optional - store objects for other fixtures
protected function addReference(string $name, object $object): void
// Optional - retrieve stored objects
protected function getReference(string $name): object
// Optional - check if reference exists
protected function hasReference(string $name): bool
}
```
--------------------------------
### Multiple Groups Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/FixtureGroupInterface.md
Example of a fixture belonging to multiple groups named 'demo' and 'testing'.
```php
class DemoFixture extends Fixture implements FixtureGroupInterface
{
public static function getGroups(): array
{
return ['demo', 'testing'];
}
public function load(ObjectManager $manager): void
{
// ...
}
}
```
--------------------------------
### Fixture Group Interface Method Signature
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Shows the method signature for the `FixtureGroupInterface`.
```php
interface FixtureGroupInterface
{
public static function getGroups(): array; // Return group names
}
```
--------------------------------
### Configuration with Groups
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/overview.md
YAML configuration example demonstrating how to tag fixtures with specific groups for selective loading.
```yaml
services:
App\DataFixture\UserFixture:
tags:
- { name: doctrine.fixture.orm, group: users }
- { name: doctrine.fixture.orm, group: demo }
App\DataFixture\ProductFixture:
tags:
- { name: doctrine.fixture.orm, group: products }
- { name: doctrine.fixture.orm, group: demo }
```
--------------------------------
### Dependency Resolution Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/overview.md
Illustrates the dependency resolution process and the resulting order of fixture loading.
```text
DemoOrderFixture
├─ depends on: UserFixture
└─ depends on: ProductFixture
DemoCommentFixture
└─ depends on: UserFixture
UserFixture
└─ (no dependencies)
ProductFixture
└─ (no dependencies)
Loading --group=demo results in order:
1. UserFixture (dependency)
2. ProductFixture (dependency)
3. DemoOrderFixture
4. DemoCommentFixture
```
--------------------------------
### Service Registration with PHP
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/PurgerFactory.md
Example of registering a custom PurgerFactory as a service using PHP configuration.
```php
$services->set(CustomPurgerFactory::class)
->tag('doctrine.fixtures.purger_factory', ['alias' => 'custom']);
```
--------------------------------
### Example - With Exclusions
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/ORMPurgerFactory.md
Create a purger excluding certain tables from being purged.
```php
// Create purger excluding certain tables
$purger = $factory->createForEntityManager(
'default',
$em,
excluded: ['migrations', 'settings', 'audit_log'],
);
// These tables will not be truncated or deleted
```
--------------------------------
### Example - Basic Usage
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/ORMPurgerFactory.md
Create a basic purger using the default DELETE mode.
```php
use Doctrine\Bundle\FixturesBundle\Purger\ORMPurgerFactory;
use Doctrine\ORM\EntityManagerInterface;
$em = $container->get('doctrine.orm.entity_manager');
$factory = new ORMPurgerFactory();
// Create basic purger
$purger = $factory->createForEntityManager(
'default',
$em,
);
// Purger will use DELETE mode (slower, preserves sequences)
```
--------------------------------
### Typical Workflow Commands
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Illustrates a typical workflow for creating and loading fixtures, including development, testing with dry-run, loading specific groups, using secondary databases, and appending data.
```bash
# 1. Create fixtures
# File: src/DataFixture/UserFixture.php
# File: src/DataFixture/ProductFixture.php
# 2. Load all fixtures (development)
php bin/console doctrine:fixtures:load
# 3. Test with dry-run (before committing)
php bin/console doctrine:fixtures:load --dry-run
# 4. Load specific group (testing)
php bin/console doctrine:fixtures:load --group=test
# 5. Load for secondary database
php bin/console doctrine:fixtures:load --em=secondary
# 6. Add more data without purging
php bin/console doctrine:fixtures:load --append
```
--------------------------------
### Service Registration with YAML
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/PurgerFactory.md
Example of registering a custom PurgerFactory as a service using YAML configuration.
```yaml
# config/services.yaml
services:
App\Purger\CustomPurgerFactory:
tags:
- { name: doctrine.fixtures.purger_factory, alias: custom }
```
--------------------------------
### Programmatic Loading
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/SymfonyFixturesLoader.md
Example of using FixturesProvider to load and execute fixtures programmatically within a service.
```php
use Doctrine\Bundle\FixturesBundle\Loader\FixturesProvider;
class SomeService
{
public function __construct(private FixturesProvider $fixturesProvider)
{
}
public function seedDatabase(): void
{
// Get all fixtures
$fixtures = $this->fixturesProvider->getFixtures();
// Get only demo fixtures
$demoFixtures = $this->fixturesProvider->getFixtures(['demo']);
foreach ($demoFixtures as $fixture) {
// Execute fixture
}
}
}
```
--------------------------------
### Usage in Tests - TRUNCATE Mode
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/ORMPurgerFactory.md
Example of using ORMPurgerFactory in a test setup method with TRUNCATE mode enabled.
```php
// In a test setup method
$factory = new ORMPurgerFactory();
$purger = $factory->createForEntityManager(
'default',
$this->em,
excluded: ['doctrine_migrations_versions'],
purgeWithTruncate: true,
);
$executor = new ORMExecutor($this->em, $purger);
$fixtures = new FixtureLoader()->getFixtures();
$executor->execute($fixtures);
```
--------------------------------
### Load Command with Options
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Demonstrates various options available for the load command, such as grouping, appending, dry-run, entity manager selection, purger selection, purge exclusions, and truncate purging.
```bash
php bin/console doctrine:fixtures:load \
--group=groupname \
--append \
--dry-run \
--em=entitymanager \
--purger=purgername \
--purge-exclusions=table1 \
--purge-exclusions=table2 \
--purge-with-truncate
```
--------------------------------
### Fixing Reference Name Typo
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/errors.md
Example of how to correctly add and get a reference to ensure spelling matches.
```php
// In UserFixture:
$this->addReference('user-admin', $user);
// In CommentFixture:
$user = $this->getReference('user-admin'); // Correct spelling
```
--------------------------------
### Reference Objects Between Fixtures
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Store and retrieve objects between fixtures using addReference and getReference.
```php
class UserFixture extends Fixture
{
public function load(ObjectManager $manager): void
{
$user = new User();
$user->setEmail('user@example.com');
// Store for other fixtures
$this->addReference('user-john', $user);
$manager->persist($user);
$manager->flush();
}
}
class RoleFixture extends Fixture
{
public function load(ObjectManager $manager): void
{
$role = new Role();
$role->setName('ROLE_USER');
// Get the previously created user
$user = $this->getReference('user-john');
$role->setUser($user);
$manager->persist($role);
$manager->flush();
}
}
```
--------------------------------
### Manual Configuration (PHP) for doctrine.fixture.orm
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/configuration.md
Example of manually configuring a service with the 'doctrine.fixture.orm' tag in PHP.
```php
$services->set(UserFixture::class)
->tag('doctrine.fixture.orm')
->tag('doctrine.fixture.orm', ['group' => 'users'])
->tag('doctrine.fixture.orm', ['group' => 'demo']);
```
--------------------------------
### Fixture Grouping via FixtureGroupInterface
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/configuration.md
Example of implementing FixtureGroupInterface to define fixture groups.
```php
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
class DemoFixture extends Fixture implements FixtureGroupInterface
{
public static function getGroups(): array
{
return ['demo', 'testing'];
}
public function load(ObjectManager $manager): void
{
// ...
}
}
```
--------------------------------
### Best Practice: Use the Base Fixture Class
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Illustrates the recommended way to extend the base Fixture class provided by the bundle.
```php
use Doctrine\Bundle\FixturesBundle\Fixture;
class UserFixture extends Fixture
{
public function load(ObjectManager $manager): void { }
}
```
```php
use Doctrine\Common\DataFixtures\AbstractFixture;
class UserFixture extends AbstractFixture
{
public function load(ObjectManager $manager): void { }
}
```
--------------------------------
### Group Mapping Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/SymfonyFixturesLoader.md
Shows the internal mapping of groups to fixture classes used for O(1) lookup.
```php
private array $groupsFixtureMapping = [
'users' => ['App\DataFixture\UserFixture' => true],
'admin' => ['App\DataFixture\AdminUserFixture' => true],
'AdminUserFixture' => ['App\DataFixture\AdminUserFixture' => true],
// ...
];
```
--------------------------------
### Error: No Fixtures Found
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/LoadDataFixturesDoctrineCommand.md
Example of the error message when no fixtures are found for the specified group.
```bash
$ php bin/console doctrine:fixtures:load --group=nonexistent
[ERROR] Could not find any fixture services to load in the groups (nonexistent).
```
--------------------------------
### Custom FixturesProvider Implementation
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/FixturesProvider.md
Example of creating a custom implementation of the FixturesProvider interface.
```php
use Doctrine\Bundle\FixturesBundle\Loader\FixturesProvider;
use Doctrine\Common\DataFixtures\FixtureInterface;
class CustomFixturesProvider implements FixturesProvider
{
public function getFixtures(array $groups = []): array
{
// Custom logic to retrieve and filter fixtures
// ...
return $fixtures;
}
}
```
--------------------------------
### Error: Unknown Entity Manager
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/LoadDataFixturesDoctrineCommand.md
Example of an error when an unknown entity manager is specified.
```bash
$ php bin/console doctrine:fixtures:load --em=nonexistent
[ERROR] Unknown "nonexistent" entity manager. Available entity managers are: default, secondary.
```
--------------------------------
### File Structure for Organizing Fixtures
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Demonstrates a logical file structure for organizing fixtures by entity or group, including dependency information.
```text
src/
└── DataFixture/
├── UserFixture.php # Group: users, UserFixture
├── ProductFixture.php # Group: products, ProductFixture
├── OrderFixture.php # Group: orders, OrderFixture (depends on User & Product)
└── DemoFixture.php # Group: demo (depends on all above)
```
--------------------------------
### Test with --dry-run
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Demonstrates how to use the `--dry-run` option to test fixture logic without actually persisting data.
```bash
# See if fixture logic works
php bin/console doctrine:fixtures:load --group=new-fixture --dry-run
```
--------------------------------
### hasReference Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/Fixture.md
Example of using the hasReference method to check if a reference exists.
```php
$user = null;
if ($this->hasReference('user-admin')) {
$user = $this->getReference('user-admin');
}
```
--------------------------------
### Fixture dependency example (circular)
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/errors.md
PHP code demonstrating a circular dependency between two fixtures.
```php
class CommentFixture extends Fixture implements DependentFixtureInterface
{
public function getDependencies(): array
{
return [UserFixture::class]; // OK
}
}
class UserFixture extends Fixture implements DependentFixtureInterface
{
public function getDependencies(): array
{
return [CommentFixture::class]; // CIRCULAR!
}
}
```
--------------------------------
### Example Execution Flow
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/LoadDataFixturesDoctrineCommand.md
Illustrates the internal steps of the execute method when loading fixtures, including retrieving fixtures, creating a purger and executor, and executing the fixtures.
```php
// User runs:
// php bin/console doctrine:fixtures:load --group=demo
// 1. Retrieves fixtures
$groups = ['demo'];
$fixtures = $this->fixturesLoader->getFixtures($groups);
if (!$fixtures) {
$ui->error('Could not find any fixture services to load in the groups (demo).');
return 1;
}
// 2. Creates purger
$factory = $this->purgerFactories['default'];
$purger = $factory->createForEntityManager(
'default',
$em,
[],
false,
);
// 3. Creates executor
$executor = new ORMExecutor($em, $purger);
// 4. Executes fixtures (purges first, then loads)
$executor->execute($fixtures, false); // append=false
// 5. Returns success
return 0;
```
--------------------------------
### Register a Custom Purger
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/INDEX.md
Example of registering a custom purger factory and using it via the console command.
```php
use Doctrine\Bundle\FixturesBundle\Purger\PurgerFactory;
class CustomPurgerFactory implements PurgerFactory
{
public function createForEntityManager(
?string $emName,
EntityManagerInterface $em,
array $excluded = [],
bool $purgeWithTruncate = false,
): PurgerInterface {
// Custom purger creation
}
}
```
```yaml
services:
App\Purger\CustomPurgerFactory:
tags:
- { name: doctrine.fixtures.purger_factory, alias: custom }
```
```bash
php bin/console doctrine:fixtures:load --purger=custom
```
--------------------------------
### Custom Purger Factory Implementation
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/configuration.md
Example of creating a custom purger factory for a different database type.
```php
namespace App\Purger;
use Doctrine\Bundle\FixturesBundle\Purger\PurgerFactory;
use Doctrine\Common\DataFixtures\Purger\PurgerInterface;
use Doctrine\ORM\EntityManagerInterface;
class CustomPurgerFactory implements PurgerFactory
{
public function createForEntityManager(
string|null $emName,
EntityManagerInterface $em,
array $excluded = [],
bool $purgeWithTruncate = false,
): PurgerInterface {
// Custom purger creation logic
$purger = new CustomPurger($em);
$purger->setExcludedTables($excluded);
return $purger;
}
}
```
--------------------------------
### No Groups Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/FixtureGroupInterface.md
Example of a fixture that does not implement FixtureGroupInterface and is always loaded.
```php
class CoreFixture extends Fixture
{
// No implementation of FixtureGroupInterface
// This fixture is always loaded
public function load(ObjectManager $manager): void
{
// Always executed
}
}
```
--------------------------------
### Best Practice: Use References for Relationships
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Illustrates the recommended way to use `addReference` and `getReference` for managing relationships between entities in fixtures.
```php
$user = new User();
$this->addReference('user-1', $user);
// Later...
$role->setUser($this->getReference('user-1'));
```
```php
$user = new User();
$this->setId(1); // Brittle, ID-dependent
// Later...
$user = $repository->find(1); // Could be wrong record
```
--------------------------------
### Purge Mode Selection: TRUNCATE Mode
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/PurgerFactory.md
Command-line example to use TRUNCATE mode for faster test database purging.
```bash
# Use TRUNCATE for faster tests
php bin/console doctrine:fixtures:load --purge-with-truncate
```
--------------------------------
### getReference Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/Fixture.md
Example of using the getReference method to retrieve a previously stored reference.
```php
class CommentFixture extends Fixture
{
public function load(ObjectManager $manager): void
{
$comment = new Comment();
$comment->setText('Great article!');
// Reference the user created in UserFixture
$user = $this->getReference('user-admin');
$comment->setAuthor($user);
$manager->persist($comment);
$manager->flush();
}
}
```
--------------------------------
### addFixtures Method Internal Use Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/SymfonyFixturesLoader.md
Example of how the addFixtures method is used internally by FixturesCompilerPass.
```php
$loader->addFixtures([
[
'fixture' => $userFixtureInstance,
'groups' => ['users', 'demo'],
],
[
'fixture' => $productFixtureInstance,
'groups' => ['products', 'demo'],
],
]);
```
--------------------------------
### addReference Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/Fixture.md
Example of using the addReference method to store an object for later reference.
```php
class UserFixture extends Fixture
{
public function load(ObjectManager $manager): void
{
$user = new User();
$user->setEmail('admin@example.com');
// Store for later reference
$this->addReference('user-admin', $user);
$manager->persist($user);
$manager->flush();
}
}
class RoleFixture extends Fixture
{
public function load(ObjectManager $manager): void
{
$role = new Role();
$role->setName('ROLE_ADMIN');
// Get the previously created user
$user = $this->getReference('user-admin');
$role->setUser($user);
$manager->persist($role);
$manager->flush();
}
}
```
--------------------------------
### Manual Configuration (YAML) for doctrine.fixture.orm
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/configuration.md
Example of manually configuring a service with the 'doctrine.fixture.orm' tag in YAML, including group specification.
```yaml
services:
App\DataFixture\UserFixture:
tags:
- doctrine.fixture.orm
- { name: doctrine.fixture.orm, group: users }
- { name: doctrine.fixture.orm, group: demo }
```
--------------------------------
### Troubleshooting: Entity manager not configured
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Provides steps to resolve the 'Unknown 'default' entity manager' error, including database creation, schema creation, and configuration checks.
```bash
php bin/console doctrine:database:create
```
```bash
php bin/console doctrine:schema:create
```
```bash
php bin/console doctrine:schema:validate
```
--------------------------------
### Single Group Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/FixtureGroupInterface.md
Example of a fixture belonging to a single group named 'admin'.
```php
class AdminFixture extends Fixture implements FixtureGroupInterface
{
public static function getGroups(): array
{
return ['admin'];
}
public function load(ObjectManager $manager): void
{
// ...
}
}
```
--------------------------------
### Via Fixture Base Class Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/ORMFixtureInterface.md
Example of extending the Fixture base class, which implements ORMFixtureInterface.
```php
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
class UserFixture extends Fixture
{
public function load(ObjectManager $manager): void
{
$user = new User();
$user->setEmail('user@example.com');
// Additional helper methods available
$this->addReference('user-1', $user);
$manager->persist($user);
$manager->flush();
}
}
```
--------------------------------
### Load Fixtures Command
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/docs/index.rst
Commands to execute fixture loading and view help options.
```terminal
# when using the ORM
$ php bin/console doctrine:fixtures:load
```
```terminal
$ php bin/console doctrine:fixtures:load --help
```
--------------------------------
### Troubleshooting: Fixtures not registered as services
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Provides the fix for the 'Could not find any fixture services to load' error, involving service configuration and fixture class inheritance.
```yaml
services:
App\DataFixture\:
resource: '../src/DataFixture'
```
```php
class MyFixture extends Fixture { }
```
--------------------------------
### Enable Dependency Injection for Custom Fixture Directory (PHP)
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/docs/index.rst
Configure services.php to load classes from the custom fixtures directory, enabling autowiring and autoconfiguration.
```php
// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
return function(ContainerConfigurator $container): void {
$services = $container->services()
->defaults()
->autowire()
->autoconfigure();
$services->load('DataFixtures\', '../fixtures');
};
```
--------------------------------
### Group Filtering Behavior Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/FixturesProvider.md
Example demonstrating group filtering behavior with UserFixture and CommentFixture.
```php
class UserFixture extends Fixture implements FixtureGroupInterface
{
public static function getGroups(): array
{
return ['users', 'demo'];
}
}
class CommentFixture extends Fixture
implements FixtureGroupInterface, DependentFixtureInterface
{
public static function getGroups(): array
{
return ['comments'];
}
public function getDependencies(): array
{
return [UserFixture::class];
}
}
// When loaded with --group=comments:
$provider->getFixtures(['comments'])
// Returns: [UserFixture (dependency), CommentFixture]
```
--------------------------------
### getFixtures Method Example Usage
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/FixturesProvider.md
Example of how to use the getFixtures method to retrieve all fixtures or fixtures filtered by group.
```php
// Assuming a provider is available
$provider = $container->get('doctrine.fixtures.provider');
// Get all fixtures
$allFixtures = $provider->getFixtures();
// Get only fixtures in the 'demo' and 'test' groups
$demoFixtures = $provider->getFixtures(['demo', 'test']);
// Iterate and execute
foreach ($demoFixtures as $fixture) {
// Execute fixture...
}
```
--------------------------------
### Example Fixture with Multiple Groups
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/FixtureGroupInterface.md
An example of a fixture implementing FixtureGroupInterface and belonging to multiple groups ('admin', 'users').
```php
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
use Doctrine\Persistence\ObjectManager;
class AdminUserFixture extends Fixture implements FixtureGroupInterface
{
public static function getGroups(): array
{
return ['admin', 'users'];
}
public function load(ObjectManager $manager): void
{
$user = new User();
$user->setEmail('admin@example.com');
$user->setRole('ROLE_ADMIN');
$manager->persist($user);
$manager->flush();
}
}
```
--------------------------------
### Empty Groups Return Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/FixturesProvider.md
Example showing that calling getFixtures with an empty or null groups array returns all fixtures.
```php
$fixtures = $provider->getFixtures();
// or
$fixtures = $provider->getFixtures([]);
// Both return all registered fixtures in dependency order
```
--------------------------------
### Enable Dependency Injection for Custom Fixture Directory (YAML)
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/docs/index.rst
Configure services.yaml to enable autowiring and autoconfiguration for classes within the custom fixtures directory.
```yaml
services:
DataFixtures\:
resource: '../fixtures'
```
--------------------------------
### Troubleshooting: Reference name typo or fixture not loaded
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/QUICKSTART.md
Offers solutions for the 'Not a valid reference' error, including checking spelling and adding dependencies.
```php
$this->addReference('user-john', $user); // Define...
$this->getReference('user-john'); // Match exactly
```
```php
class RoleFixture extends Fixture implements DependentFixtureInterface
{
public function getDependencies(): array
{
return [UserFixture::class]; // Add this
}
}
```
--------------------------------
### Inject Services into Fixtures
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/docs/index.rst
Demonstrates using dependency injection within a fixture class to access application services.
```php
// src/DataFixtures/AppFixtures.php
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class AppFixtures extends Fixture
{
private UserPasswordHasherInterface $hasher;
public function __construct(UserPasswordHasherInterface $hasher)
{
$this->hasher = $hasher;
}
// ...
public function load(ObjectManager $manager): void
{
$user = new User();
$user->setUsername('admin');
$password = $this->hasher->hashPassword($user, 'pass_1234');
$user->setPassword($password);
$manager->persist($user);
$manager->flush();
}
}
```
--------------------------------
### Excluding Tables by Table Name
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/ORMPurgerFactory.md
Example of excluding specific tables from purging by their names.
```php
$purger = $factory->createForEntityManager(
'default',
$em,
excluded: ['users', 'products', 'settings'],
);
```
--------------------------------
### SymfonyFixturesLoader Service Registration
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/SymfonyFixturesLoader.md
Example of how SymfonyFixturesLoader is instantiated as a service by the Symfony DI container.
```php
->set('doctrine.fixtures.loader', SymfonyFixturesLoader::class)
```
--------------------------------
### Execute Fixtures by Group
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/docs/index.rst
Commands to run specific fixture groups or individual fixture classes.
```terminal
$ php bin/console doctrine:fixtures:load --group=group1
# or to execute multiple groups
$ php bin/console doctrine:fixtures:load --group=group1 --group=group2
```
```terminal
$ php bin/console doctrine:fixtures:load --group=UserFixtures
```
--------------------------------
### Create database and schema
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/errors.md
Commands to create the database and schema if they don't exist.
```bash
php bin/console doctrine:database:create
php bin/console doctrine:schema:create
```
--------------------------------
### Load Fixtures with Custom Purger
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/docs/index.rst
Use the --purger command-line option to specify a custom purger alias for loading fixtures.
```terminal
$ php bin/console doctrine:fixtures:load --purger=my_purger
```
--------------------------------
### Multiple Entity Managers
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/ORMPurgerFactory.md
Example of creating purgers for multiple entity managers within an application.
```php
$factory = new ORMPurgerFactory();
$defaultEm = $container->get('doctrine.orm.entity_manager');
$secondaryEm = $container->get('doctrine.orm.secondary_entity_manager');
$purger1 = $factory->createForEntityManager('default', $defaultEm);
$purger2 = $factory->createForEntityManager('secondary', $secondaryEm);
// Use them separately
$executor1 = new ORMExecutor($defaultEm, $purger1);
$executor2 = new ORMExecutor($secondaryEm, $purger2);
```
--------------------------------
### Fixture with Groups and Dependencies
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/FixtureGroupInterface.md
Example of a fixture implementing both FixtureGroupInterface and DependentFixtureInterface.
```php
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
class CommentFixture extends Fixture implements FixtureGroupInterface, DependentFixtureInterface
{
public static function getGroups(): array
{
return ['comments', 'demo'];
}
public function getDependencies(): array
{
return [UserFixture::class];
}
public function load(ObjectManager $manager): void
{
// UserFixture is loaded first due to dependency
$user = $this->getReference('some-user');
// ...
}
}
```
--------------------------------
### Load Fixtures in Dry Run Mode
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/docs/index.rst
Simulate fixture loading without making database changes using the --dry-run option.
```terminal
$ php bin/console doctrine:fixtures:load --dry-run
```
--------------------------------
### Logging Integration Example
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/api-reference/LoadDataFixturesDoctrineCommand.md
Integrates with Symfony logging by implementing a custom logger that extends AbstractLogger. Fixture execution details are logged using a SymfonyStyle object.
```php
$executor->setLogger(new class ($ui) extends AbstractLogger {
public function __construct(private SymfonyStyle $ui)
{
}
public function log(mixed $level, string|Stringable $message, array $context = []): void
{
$this->ui->text(sprintf(' > %s', $message));
}
});
```
--------------------------------
### Fixture Service Tag Attributes
Source: https://github.com/doctrine/doctrinefixturesbundle/blob/4.3.x/_autodocs/configuration.md
Example of how to tag a fixture service to assign it to specific groups.
```yaml
services:
App\DataFixture\DemoFixture:
tags:
- { name: doctrine.fixture.orm, group: demo }
- { name: doctrine.fixture.orm, group: testing }
```