From 1c6ab53bbed4d41cab8199086e1616ac1221a1a2 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 26 Mar 2019 17:03:22 +0100 Subject: [PATCH] Updated docs to match the style of the other Symfony docs --- Resources/doc/index.rst | 57 ++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/Resources/doc/index.rst b/Resources/doc/index.rst index 285fc3cb..510653ef 100644 --- a/Resources/doc/index.rst +++ b/Resources/doc/index.rst @@ -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 @@ -35,6 +37,14 @@ 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 ---------------- @@ -42,9 +52,7 @@ 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; @@ -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 @@ -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; @@ -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 // ... @@ -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; @@ -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 @@ -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 @@ -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