Skip to content

Commit

Permalink
Merge pull request #275 from doctrine/update-docs
Browse files Browse the repository at this point in the history
Updated docs to match the style of the other Symfony docs
  • Loading branch information
alcaeus authored Apr 29, 2019
2 parents bd4336b + 1b77512 commit db7bc66
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions Resources/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ supports the `ORM`_ (MySQL, PostgreSQL, SQLite, etc.).
Installation
------------

Open a command console, enter your project directory and run the
following command to download the latest stable version of this bundle:
In Symfony 4 or higher applications that use `Symfony Flex`_, open a command
console, enter your project directory and run the following command:

.. code-block:: bash
.. code-block:: terminal
composer require --dev doctrine/doctrine-fixtures-bundle
$ composer require --dev orm-fixtures
.. tip::
That's all! You can skip to the next section and start writing fixtures.

If you are using flex you just need to do:
``composer req --dev orm-fixtures``
In Symfony 3 applications (or when not using Symfony Flex), run this other
command instead:

.. code-block:: terminal
If you're *not* using Symfony Flex (i.e. Symfony 3 and lower), you will
also need to enable the bundle in your ``AppKernel`` class:
$ composer require --dev doctrine/doctrine-fixtures-bundle
.. code-block:: php
You will also need to enable the bundle. In Symfony 3 and earlier applications,
update the ``AppKernel`` class::

// app/AppKernel.php

Expand All @@ -35,16 +37,22 @@ also need to enable the bundle in your ``AppKernel`` class:
$bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
}

In Symfony 4 applications, update the ``config/bundles.php`` file::

// config/bundles.php
return [
// ...
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
];

Writing Fixtures
----------------

Data fixtures are PHP classes where you create objects and persist them to the
database.

Imagine that you want to add some ``Product`` objects to your database. No problem!
Just create a fixtures class and start adding products!

.. code-block:: php
Create a fixtures class and start adding products::

// src/DataFixtures/AppFixtures.php
namespace App\DataFixtures;
Expand Down Expand Up @@ -78,7 +86,7 @@ Loading Fixtures

Once your fixtures have been written, load them by executing this command:

.. code-block:: bash
.. code-block:: terminal
# when using the ORM
$ php bin/console doctrine:fixtures:load
Expand All @@ -104,9 +112,7 @@ Accessing Services from the Fixtures

In some cases you may need to access your application's services inside a fixtures
class. No problem! Your fixtures class is a service, so you can use normal dependency
injection:

.. code-block:: php
injection::

// src/DataFixtures/AppFixtures.php
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
Expand Down Expand Up @@ -155,9 +161,7 @@ Sharing Objects between Fixtures
When using multiple fixtures files, you can reuse PHP objects across different
files thanks to the **object references**. Use the ``addReference()`` method to
give a name to any object and then, use the ``getReference()`` method to get the
exact same object via its name:

.. code-block:: php
exact same object via its name::

// src/DataFixtures/UserFixtures.php
// ...
Expand Down Expand Up @@ -204,9 +208,7 @@ Instead of defining the exact order in which all fixture files must be loaded,
Doctrine uses a smarter approach to ensure that some fixtures are loaded before
others. Implement the ``DependentFixtureInterface`` and add a new
``getDependencies()`` method to your fixtures class. This will return
an array of the fixture classes that must be loaded before this one:

.. code-block:: php
an array of the fixture classes that must be loaded before this one::

// src/DataFixtures/UserFixtures.php
namespace App\DataFixtures;
Expand Down Expand Up @@ -249,7 +251,9 @@ to execute *some* of your fixture classes, you can organize them into
groups.

The simplest way to organize a fixture class into a group is to
make your fixture implement ``FixtureGroupInterface``::
make your fixture implement ``FixtureGroupInterface``:

.. code-block:: diff
// src/DataFixtures/UserFixtures.php
Expand All @@ -269,7 +273,7 @@ make your fixture implement ``FixtureGroupInterface``::
To execute all of your fixtures for a given group, pass the ``--group``
option:

.. code-block:: bash
.. code-block:: terminal
$ php bin/console doctrine:fixtures:load --group=group1
Expand All @@ -285,10 +289,11 @@ fixture loader always adds the short name of the class as a separate group so
you can load a single fixture at a time. In the example above, you can load the
fixture using the ``UserFixtures`` group:

.. code-block:: bash
.. code-block:: terminal
$ php bin/console doctrine:fixtures:load --group=UserFixtures
.. _`ORM`: https://symfony.com/doc/current/doctrine.html
.. _`installation chapter`: https://getcomposer.org/doc/00-intro.md
.. _`Symfony Flex`: https://symfony.com/doc/current/setup/flex.html
.. _`default service configuration`: https://symfony.com/doc/current/service_container.html#service-container-services-load-example

0 comments on commit db7bc66

Please sign in to comment.