|
| 1 | +Data Fixtures |
| 2 | +============= |
| 3 | + |
| 4 | +Data fixtures are a way to load sample data into your MongoDB database. |
| 5 | +This is useful for testing, development, and seeding your database with initial data. |
| 6 | + |
| 7 | +Installation |
| 8 | +------------ |
| 9 | + |
| 10 | +First, install the `doctrine/data-fixtures`_ package: |
| 11 | + |
| 12 | +.. code-block:: bash |
| 13 | +
|
| 14 | + composer require --dev doctrine/data-fixtures |
| 15 | +
|
| 16 | +Creating Fixtures |
| 17 | +----------------- |
| 18 | + |
| 19 | +To create a fixture, create a class that implements ``ODMFixtureInterface`` or extends the |
| 20 | +``Fixture`` base class. The best practice is to extend ``Doctrine\Bundle\MongoDBBundle\Fixture\Fixture``, |
| 21 | +as it provides convenient access to the object manager and reference functionality: |
| 22 | + |
| 23 | +.. code-block:: php |
| 24 | +
|
| 25 | + // src/DataFixtures/ProductFixtures.php |
| 26 | + namespace App\DataFixtures; |
| 27 | +
|
| 28 | + use App\Document\Product; |
| 29 | + use Doctrine\Bundle\MongoDBBundle\Fixture\Fixture; |
| 30 | + use Doctrine\Persistence\ObjectManager; |
| 31 | +
|
| 32 | + class ProductFixtures extends Fixture |
| 33 | + { |
| 34 | + public function load(ObjectManager $manager): void |
| 35 | + { |
| 36 | + $product = new Product(); |
| 37 | + $product->setName('Example Product'); |
| 38 | + $product->setPrice(19.99); |
| 39 | +
|
| 40 | + $manager->persist($product); |
| 41 | + $manager->flush(); |
| 42 | + } |
| 43 | + } |
| 44 | +
|
| 45 | +If you prefer not to extend the base ``Fixture`` class, you can implement the ``ODMFixtureInterface`` |
| 46 | +directly: |
| 47 | + |
| 48 | +.. code-block:: php |
| 49 | +
|
| 50 | + // src/DataFixtures/ProductFixtures.php |
| 51 | + namespace App\DataFixtures; |
| 52 | +
|
| 53 | + use App\Document\Product; |
| 54 | + use Doctrine\Bundle\MongoDBBundle\Fixture\ODMFixtureInterface; |
| 55 | + use Doctrine\Persistence\ObjectManager; |
| 56 | +
|
| 57 | + class ProductFixtures implements ODMFixtureInterface |
| 58 | + { |
| 59 | + public function load(ObjectManager $manager): void |
| 60 | + { |
| 61 | + $product = new Product(); |
| 62 | + $product->setName('Example Product'); |
| 63 | + $product->setPrice(19.99); |
| 64 | +
|
| 65 | + $manager->persist($product); |
| 66 | + $manager->flush(); |
| 67 | + } |
| 68 | + } |
| 69 | +
|
| 70 | +Registering Fixtures |
| 71 | +-------------------- |
| 72 | + |
| 73 | +Fixtures are automatically discovered and registered as services if they are |
| 74 | +in an autoconfigured service namespace (e.g. ``App\DataFixtures``). If your fixtures are located |
| 75 | +elsewhere, or if you have disabled autoconfiguration, you need to manually tag them |
| 76 | +with the ``doctrine.fixture.odm.mongodb`` tag: |
| 77 | + |
| 78 | +.. configuration-block:: |
| 79 | + |
| 80 | + .. code-block:: yaml |
| 81 | +
|
| 82 | + # config/services.yaml |
| 83 | + services: |
| 84 | + App\DataFixtures\ProductFixtures: |
| 85 | + tags: |
| 86 | + - { name: 'doctrine.fixture.odm.mongodb' } |
| 87 | +
|
| 88 | +
|
| 89 | +Loading Fixtures |
| 90 | +---------------- |
| 91 | + |
| 92 | +You can load fixtures using the command line: |
| 93 | + |
| 94 | +.. code-block:: bash |
| 95 | +
|
| 96 | + php bin/console doctrine:mongodb:fixtures:load |
| 97 | +
|
| 98 | +This command will load all registered fixtures. You can also append data instead of truncating: |
| 99 | + |
| 100 | +.. code-block:: bash |
| 101 | +
|
| 102 | + # Append fixtures without truncating the database |
| 103 | + php bin/console doctrine:mongodb:fixtures:load --append |
| 104 | +
|
| 105 | +Fixture Dependencies |
| 106 | +-------------------- |
| 107 | + |
| 108 | +Sometimes you need to load fixtures in a specific order because one fixture depends on data |
| 109 | +created by another fixture. You can implement the ``DependentFixtureInterface`` to specify |
| 110 | +which fixtures must be loaded first: |
| 111 | + |
| 112 | +.. code-block:: php |
| 113 | +
|
| 114 | + // src/DataFixtures/CategoryFixtures.php |
| 115 | + namespace App\DataFixtures; |
| 116 | +
|
| 117 | + use App\Document\Category; |
| 118 | + use Doctrine\Bundle\MongoDBBundle\Fixture\Fixture; |
| 119 | + use Doctrine\Persistence\ObjectManager; |
| 120 | +
|
| 121 | + class CategoryFixtures extends Fixture |
| 122 | + { |
| 123 | + public function load(ObjectManager $manager): void |
| 124 | + { |
| 125 | + $category = new Category(); |
| 126 | + $category->setName('Electronics'); |
| 127 | +
|
| 128 | + $manager->persist($category); |
| 129 | + $manager->flush(); |
| 130 | +
|
| 131 | + // Store a reference for other fixtures to use |
| 132 | + $this->addReference('category-electronics', $category); |
| 133 | + } |
| 134 | + } |
| 135 | +
|
| 136 | +.. code-block:: php |
| 137 | +
|
| 138 | + // src/DataFixtures/ProductFixtures.php |
| 139 | + namespace App\DataFixtures; |
| 140 | +
|
| 141 | + use App\Document\Product; |
| 142 | + use Doctrine\Bundle\MongoDBBundle\Fixture\Fixture; |
| 143 | + use Doctrine\Common\DataFixtures\DependentFixtureInterface; |
| 144 | + use Doctrine\Persistence\ObjectManager; |
| 145 | +
|
| 146 | + class ProductFixtures extends Fixture implements DependentFixtureInterface |
| 147 | + { |
| 148 | + public function load(ObjectManager $manager): void |
| 149 | + { |
| 150 | + $product = new Product(); |
| 151 | + $product->setName('Laptop'); |
| 152 | + $product->setPrice(999.99); |
| 153 | + $product->setCategory($this->getReference('category-electronics')); |
| 154 | +
|
| 155 | + $manager->persist($product); |
| 156 | + $manager->flush(); |
| 157 | + } |
| 158 | +
|
| 159 | + public function getDependencies(): array |
| 160 | + { |
| 161 | + return [CategoryFixtures::class]; |
| 162 | + } |
| 163 | + } |
| 164 | +
|
| 165 | +Using References |
| 166 | +---------------- |
| 167 | + |
| 168 | +The ``Fixture`` base class provides methods to store and retrieve references to documents, |
| 169 | +which is useful when you need to reference one fixture from another: |
| 170 | + |
| 171 | +.. code-block:: php |
| 172 | +
|
| 173 | + // Store a reference |
| 174 | + $this->addReference('my-product', $product); |
| 175 | +
|
| 176 | + // Retrieve a reference |
| 177 | + $product = $this->getReference('my-product'); |
| 178 | +
|
| 179 | + // Check if a reference exists |
| 180 | + if ($this->hasReference('my-product')) { |
| 181 | + $product = $this->getReference('my-product'); |
| 182 | + } |
| 183 | +
|
| 184 | +More Information |
| 185 | +---------------- |
| 186 | + |
| 187 | +For more details about data fixtures and advanced usage patterns, see the official |
| 188 | +`Doctrine Data Fixtures documentation`_. |
| 189 | + |
| 190 | +.. _`doctrine/data-fixtures`: https://packagist.org/packages/doctrine/data-fixtures |
| 191 | +.. _`Doctrine Data Fixtures documentation`: https://www.doctrine-project.org/projects/doctrine-data-fixtures/en/current/how-to/loading-fixtures.html |
| 192 | + |
0 commit comments