20.08.11 - Fixtures in Symfony 2

How to create fixtures in our Symfony 2 project

The fixtures is a data configuration for our Symfony 2 project, is a simple but a powerful way to load into the database default or required information for the correct execution of the application. Often, in the development phase, the model changes or even dropped, as the recovery comes tedious the fixtures can save us hours of manual SQL insertions.

To install a Doctrine 2 fixtures system in our Symfony 2 app, we have to add the next lines to the "deps" file.
[doctrine-fixtures]
    git=http://github.com/doctrine/data-fixtures.git

[DoctrineFixturesBundle]
    git=http://github.com/symfony/DoctrineFixturesBundle.git
    target=/bundles/Symfony/Bundle/DoctrineFixturesBundle
Installation:
$ bin/vendors install
We have to activate it in the app/autoload.php:
$loader->registerNamespaces(array(
    // ...
    'Doctrine\\Common\\DataFixtures' => __DIR__.'/../vendor/doctrine-fixtures/lib',
    'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib',
    // ...
));
And in the app/AppKernel.php:
public function registerBundles()
{
    $bundles = array(
        // ...
        new Symfony\Bundle\DoctrineFixturesBundle\DoctrineFixturesBundle(),
        // ...
    );
    // ...
}
Generally, we also have to create a fixture file per entity defined in the model. We can create a fixture to load the default admin users of our app:
<?php
# Test/MyBundle/DataFixtures/ORM/LoadUserData.php

namespace Test\MyBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;

use Test\MyBundle\Entity\User;

class LoadUserData extends AbstractFixture implements OrderedFixtureInterface
{
    /**
     * Main method for fixtures insertion
     *
     * @param Doctrine\Manager $manager
     */

    public function load($manager)
    {
        //Save the user
        $user = new User();
        $user->setUsername('admin');
        $user->setPassword('admin');
        $user->setLastLogin(new \DateTime('now'));
        $user->setCreatedAt(new \DateTime('now'));
        $user->setUpdatedAt(new \DateTime('now'));
        $user->setIsActive(true);
        $user->setRoleName(User::ROLE_SUPER_ADMIN);
        $manager->persist($user);
        $manager->flush();
       
        //Associate a reference for other fixtures
        $this->addReference('user-admin', $user);
    }
   
    /**
     * Get the order of this execution
     *
     * @return int
     */

    public function getOrder()
    {
        return 1;
    }
}
Finally, in order to load the fixtures configuration, we have to execute the next command:
$ php app/console doctrine:fixtures:load
More information: Symfony
More news about:  Symfony2
Tags:  Doctrine,  fixtures

Comments

  • Gravatar
    24.12.11 - 10:19  Inidofelicita

    you love this? for more to your friends

  • Gravatar
    26.01.12 - 10:33  Víctor Hugo

    root@xxx:/???/???/symblog.dev# php app/console doctrine:fixtures:load PHP Fatal error: Declaration of Blogger\BlogBundle\DataFixtures\ORM\BlogFixtures::load() must be compatible with that of Doctrine\Common\DataFixtures\FixtureInterface::load() in /???/???/symblog.dev/src/Blogger/BlogBundle/DataFixtures/ORM/BlogFixtures.php on line 10 Fatal error: Declaration of Blogger\BlogBundle\DataFixtures\ORM\BlogFixtures::load() must be compatible with that of Doctrine\Common\DataFixtures\FixtureInterface::load() in /???/???/symblog.dev/src/Blogger/BlogBundle/DataFixtures/ORM/BlogFixtures.php on line 10 Hi dude do you have any idea of how to solve this error i have follow several tuts but have no find a solution yet

  • 27.01.12 - 10:10  kiwwito
    You must follow the declaration of the method FixtureInterface::load like public function load($manager)
  • Gravatar
    28.01.12 - 11:41  Roman Marintsenko

    @Victor Hugo Update your BlogFixtures::load() method to look like this: public function load(\Doctrine\Common\Persistence\ObjectManager $manager)

  • Gravatar
    30.01.12 - 07:44  Tk

    I'm also having the same issue i.e "...must be compatible with that of Doctrine\Common\DataFixtures\FixtureInterface::load()". Please help.

  • Gravatar
    31.01.12 - 01:51  Jonathan Petitcolas

    @Victor Hugo: simply modify your load method like following: public function load(ObjectManager $manager) And add the related namespace: use Doctrine\Common\Persistence\ObjectManager; It should work then.

Comment