Symfony2 data fixtures setup

Featured Image

First time I was wondering: what will I do with this? Do I need data fixtures?

When started project, soon I realized that fixtures is a must in my project.

To explain, I implemented user authentication and authorization mechanism inside project and needed to add first user – myself inside database with password hash.

It’s easy to add name and other stuff manually, but how can I deal with password hash?

I found that data fixtures are solution for my problem.

But .. data fixtures are not part of standard Symfony2 package, at least .. not yet ….

Of course, official git manual didn’t work for me, so I had to download the:

Doctrine data fixtures

Doctrine fixtures bundle

Then unpack the archives into folders:

Doctrine data-fixtures: into vendor/doctrine-fixtures so I have folder structure now:

myproject
     /vendor
         /doctrine-fixtures
             /lib
                 /Doctrine
                     /Common
                         /DataFixtures
                              //vendor
                         /...

Doctrine fixtures bundle into: vendor/symfony/src/Symfony/Bundle/DoctrineFixturesBundle/ …

/vendor
   /symfony
      /src
         /Symfony
             /Bundle
                 /DoctrinefixturesBundle
                     / ...

Don’t forget to add into autoload.php BEFORE other Doctrine things:

//...
$loader->registerNamespaces(array(
'Symfony'          => array(__DIR__.'/../vendor/symfony/src', __DIR__.'/../vendor/bundles'),
'Sensio'           => __DIR__.'/../vendor/bundles',
'JMS'              => __DIR__.'/../vendor/bundles',
'Doctrine\\Common\\DataFixtures' => __DIR__.'/../vendor/doctrine-fixtures/lib',
'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib',
'Doctrine\\DBAL'   => __DIR__.'/../vendor/doctrine-dbal/lib',
//...
//AppKernel.php
//...
public function registerBundles()
{
// …
new Symfony\Bundle\DoctrineFixturesBundle\DoctrineFixturesBundle(),
//...

That’s it for now. In the next article I will show you how to write simple data fixture. Cheers.


9 comments

  1. I wonder what is the equivalent of DIR in symfony 2.1 . I looked onto the autoload_namespaces.php it uses $vendorDir

  2. Hi Darko, greetings from Argentina.!

    Thank you very much for this article.

    I´m currently having some troubles when executing the following fixture load command:

    php app/console doctrine:fixtures:load –fixture=<> –append –em=dbal

    The error I´m getting is this:

    [InvalidArgumentException] Could not find an entity manager configured whit the name “dbal”. Check your application to configure your Doctrine entity managers.

    I´m sure I´m making a mistake with the entity manager parameter. In the config.yml file I got this configuration:

    #Doctrine Configuration
    doctrine:
    dbal:
    driver: %database_driver%
    host: %database_host%
    port: %database_port%
    dbname: %database_name%
    user: %database_user%
    password: %database_password%
    charset: UTF8

    Of course that this configuration is mapped through the parameters.ini file, but it appears to be correctly configured a I don´t know why the fixture command does not recognize the “dbal” entry. Is this the right way to invoke the command?

    Thank you in advance for any kind of help 🙂

  3. @shaq, I put some locations schema in the article either for Fixures and also for FixturesBundle. So, if you install like I wrote, then it should work.
    2. Command for loading fixtures is:

    php console doctrine:fixtures:load

    3. you obviously have some syntax errors inside entity, so first fix them, so you can upgrade database schema, and then try to load fixtures.
    Cheers 🙂

  4. Hey Darko…

    i was just wondering about:
    1- the location of “Doctrine fixtures bundle” ??
    other websites say that it should be in
    “\vendor\bundles\symfony\Bundle\DoctrineFixturesBundle\”

    2- when running
    “php app/console doctrine:schema:update –force”
    i get the error:
    [PDOException]
    SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near “user”
    LINE 1: CREATE TABLE user (id INT NOT NULL, username VARCHAR(255) NO…
    ^

    3- when running
    “php app/console doctrine:data:load”
    i get the error
    [InvalidArgumentException]
    Command “doctrine:data:load” is not defined.

    any idea.. ??

  5. Thanks Darko. Great to see you are on top of things with Symfony 2. If they are true with their statements we can finally expect final version of Symfony 2 two days from now 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.