Skip to content

Commit 32ad0d1

Browse files
authored
Add documentation page for data fixtures (#974)
1 parent f386c12 commit 32ad0d1

File tree

3 files changed

+202
-9
lines changed

3 files changed

+202
-9
lines changed

docs/console.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Console Commands
22
================
33

4-
The Doctrine2 ODM integration offers various console commands under the
4+
The Doctrine MongoDB ODM integration offers various console commands under the
55
``doctrine:mongodb`` namespace. To view the command list you can run the console
66
without any arguments:
77

@@ -21,7 +21,7 @@ For example, to get details about the ``doctrine:mongodb:query`` task, run:
2121
.. note::
2222

2323
To be able to load data fixtures into MongoDB, you will need to have the
24-
``DoctrineFixturesBundle`` bundle installed. To learn how to do it, read
25-
the "`DoctrineFixturesBundle`_" entry of the documentation.
24+
``doctrine/data-fixtures`` package installed. To learn how to do it, read
25+
the `Doctrine Data Fixtures`_.
2626

27-
.. _`DoctrineFixturesBundle`: https://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html
27+
.. _`Doctrine Data Fixtures`: data_fixtures

docs/data_fixtures.rst

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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+

docs/index.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
DoctrineMongoDBBundle
22
=====================
33

4-
The `MongoDB`_ Object Document Mapper (ODM) is much like the Doctrine2 ORM
5-
in its philosophy and how it works. In other words, like the `Doctrine2 ORM`_,
4+
The `MongoDB`_ Object Document Mapper (ODM) is much like the Doctrine ORM
5+
in its philosophy and how it works. In other words, like the `Doctrine ORM`_,
66
with the Doctrine ODM, you deal only with plain PHP objects, which are then
77
persisted transparently to and from MongoDB.
88

@@ -15,7 +15,7 @@ helping you to configure and use it in your application.
1515

1616
.. note::
1717

18-
This documentation will feel a lot like the `Doctrine2 ORM chapter`_,
18+
This documentation will feel a lot like the `Doctrine ORM chapter`_,
1919
which talks about how the Doctrine ORM can be used to persist data to
2020
relational databases (e.g. MySQL). This is on purpose - whether you persist
2121
to a relational database via the ORM or to MongoDB via the ODM, the philosophies
@@ -27,6 +27,7 @@ helping you to configure and use it in your application.
2727
config
2828
first_steps
2929
form_validation
30+
data_fixtures
3031
security_bundle
3132
messenger
3233
events
@@ -45,8 +46,8 @@ For more information about them, see `available Doctrine extensions`_
4546
and use the `StofDoctrineExtensionsBundle`_ to integrate them in your application.
4647

4748
.. _`MongoDB`: https://www.mongodb.com
48-
.. _`Doctrine2 ORM`: https://symfony.com/doc/current/book/doctrine.html
49+
.. _`Doctrine ORM`: https://symfony.com/doc/current/book/doctrine.html
4950
.. _`documentation`: https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/
50-
.. _`Doctrine2 ORM chapter`: https://symfony.com/doc/current/book/doctrine.html
51+
.. _`Doctrine ORM chapter`: https://symfony.com/doc/current/book/doctrine.html
5152
.. _`available Doctrine extensions`: https://github.com/Atlantic18/DoctrineExtensions
5253
.. _`StofDoctrineExtensionsBundle`: https://symfony.com/doc/current/bundles/StofDoctrineExtensionsBundle/index.html

0 commit comments

Comments
 (0)