### Install DoctrineFixturesBundle with Composer (Flex) Source: https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html Use this command if you are using Symfony Flex to install the bundle. ```bash $ composer require --dev orm-fixtures ``` -------------------------------- ### Install DoctrineFixturesBundle with Composer (No Flex) Source: https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html Use this command if you are not using Symfony Flex to install the bundle. ```bash $ composer require --dev doctrine/doctrine-fixtures-bundle ``` -------------------------------- ### Load Fixtures from the Command Line Source: https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html Execute this command to load your defined fixtures into the database. By default, it purges the database before loading. ```bash # when using the ORM $ php bin/console doctrine:fixtures:load ``` -------------------------------- ### View Fixture Load Command Help Source: https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html Run this command to see all available options and arguments for the `doctrine:fixtures:load` command. ```bash $ php bin/console doctrine:fixtures:load --help ``` -------------------------------- ### Create a Basic Fixture Class Source: https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html Define a fixtures class that extends `Fixture` and implements the `load` method to create and persist objects. ```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(); } } ``` -------------------------------- ### Configure Fixtures in PHP Source: https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html Registers the fixtures directory using the PHP service configuration format. ```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'); }; ``` -------------------------------- ### Organizing Fixtures into Groups Source: https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html Implement FixtureGroupInterface to categorize fixtures and execute them selectively using the --group option. ```php // src/DataFixtures/UserFixtures.php + use Doctrine\\\Bundle\\\FixturesBundle\\\FixtureGroupInterface; - class UserFixtures extends Fixture + class UserFixtures extends Fixture implements FixtureGroupInterface { // ... + public static function getGroups(): array + { + return ['group1', 'group2']; + } } ``` ```bash $ php bin/console doctrine:fixtures:load --group=group1 # or to execute multiple groups $ php bin/console doctrine:fixtures:load --group=group1 --group=group2 ``` ```bash $ php bin/console doctrine:fixtures:load --group=UserFixtures ``` -------------------------------- ### Defining Fixture Dependencies Source: https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html Implement DependentFixtureInterface and define getDependencies to ensure specific fixtures are loaded before others. ```php // src/DataFixtures/UserFixtures.php namespace App\\\DataFixtures; // ... class UserFixtures extends Fixture { public function load(ObjectManager $manager): void { // ... } } // src/DataFixtures/GroupFixtures.php namespace App\\\DataFixtures; // ... use App\\\DataFixtures\\\UserFixtures; use Doctrine\\\Common\\\DataFixtures\\\DependentFixtureInterface; class GroupFixtures extends Fixture implements DependentFixtureInterface { public function load(ObjectManager $manager): void { // ... } public function getDependencies(): array { return [ UserFixtures::class, ]; } } ``` -------------------------------- ### Configure PSR-4 autoloading for fixtures Source: https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html Update composer.json to include a new directory for fixtures. ```json "autoload-dev": { "psr-4": { "...": "...", "DataFixtures\\": "fixtures/" } }, ``` -------------------------------- ### Implement a custom purger and factory Source: https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html Create a custom purger by implementing ORMPurgerInterface and a corresponding factory implementing PurgerFactory. ```php // src/Purger/CustomPurger.php namespace App\Purger; use Doctrine\Common\DataFixtures\Purger\ORMPurger; use Doctrine\Common\DataFixtures\Purger\ORMPurgerInterface; use Doctrine\ORM\EntityManagerInterface; class CustomPurger implements ORMPurgerInterface { private EntityManagerInterface $entityManager; public function setEntityManager(EntityManagerInterface $em): void { $this->entityManager = $em; } public function purge(): void { // ... } } // src/Purger/CustomPurgerFactory.php namespace App\Purger; // ... use Doctrine\Bundle\FixturesBundle\Purger\PurgerFactory; class CustomPurgerFactory implements PurgerFactory { public function createForEntityManager(?string $emName, EntityManagerInterface $em, array $excluded = [], bool $purgeWithTruncate = false) : PurgerInterface { return new CustomPurger(); } } ``` -------------------------------- ### Append Fixtures to Existing Data Source: https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html Add the `--append` option to the load command to add fixture data without purging the existing database. ```bash $ php bin/console doctrine:fixtures:load --append ``` -------------------------------- ### Configure Fixtures in YAML Source: https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html Registers the fixtures directory in the service container using YAML configuration. ```yaml services: DataFixtures\: resource: '../fixtures' ``` -------------------------------- ### Execute fixtures in dry run mode Source: https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html Simulate fixture execution without modifying the database using the --dry-run option. ```bash $ php bin/console doctrine:fixtures:load --dry-run ``` ```bash $ php bin/console doctrine:fixtures:load --group=group1 --append --dry-run ``` -------------------------------- ### Sharing Objects Between Fixtures Source: https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html Use addReference to store an entity and getReference to retrieve it in another fixture file. This functionality is limited to ORM entities. ```php // src/DataFixtures/UserFixtures.php // ... class UserFixtures extends Fixture { public const ADMIN_USER_REFERENCE = 'admin-user'; public function load(ObjectManager $manager): void { $userAdmin = new User('admin', 'pass_1234'); $manager->persist($userAdmin); $manager->flush(); // other fixtures can get this object using the UserFixtures::ADMIN_USER_REFERENCE constant $this->addReference(self::ADMIN_USER_REFERENCE, $userAdmin); } } ``` ```php // src/DataFixtures/GroupFixtures.php // ... class GroupFixtures extends Fixture { public function load(ObjectManager $manager): void { $userGroup = new Group('administrators'); // this reference returns the User object created in UserFixtures $userGroup->addUser($this->getReference(UserFixtures::ADMIN_USER_REFERENCE, User::class)); $manager->persist($userGroup); $manager->flush(); } } ``` -------------------------------- ### Use custom purger via CLI Source: https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html Specify the custom purger alias using the --purger option. ```bash $ php bin/console doctrine:fixtures:load --purger=my_purger ``` -------------------------------- ### Access Services in Fixtures via Dependency Injection Source: https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html Inject services like `UserPasswordHasherInterface` into your fixtures class constructor to use them within the `load` method. ```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(); } } ``` -------------------------------- ### Register custom purger factory Source: https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html Register the custom purger factory as a service with the doctrine.fixtures.purger_factory tag. ```yaml # config/services.yaml services: App\Purger\CustomPurgerFactory: tags: - { name: 'doctrine.fixtures.purger_factory', alias: 'my_purger' } ``` ```xml ``` ```php // config/services.php namespace Symfony\Component\DependencyInjection\Loader\Configurator; use App\Purger\CustomerPurgerFactory; return function(ContainerConfigurator $configurator): void { $services = $configurator->services(); $services->set(CustomerPurgerFactory::class) ->tag('doctrine.fixtures.purger_factory', ['alias' => 'my_purger']) ; }; ``` -------------------------------- ### Exclude tables from purging via CLI Source: https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html Use the --purge-exclusions option to prevent specific tables from being cleared during fixture loading. ```bash $ php bin/console doctrine:fixtures:load --purge-exclusions=post_category --purge-exclusions=comment_type ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.