Ron Ron - 1 year ago 157
PHP Question

Zend Framework 2 - Doctrine 2 - How to use fixtures

I'm in the need of using fixtures for putting an initial user into my database.

I had a look at this module which is said to be pretty good:

Unfortunately I don't understand how to use it. Alright, I build a class with my fixtures. And then? I'm used to fixtures in the way that when I create my database (

) that the data is automatically written into it.

But where is this connection? It seems that you have to call the Loader at some point in the code?

Would be great if someone could give me a short tutorial how to utilize these (or any other working) fixture module!


Answer Source

The fixtures are not available yet in the DoctrineModule. There is a distinct module for specifically fixture loading with Doctrine and it's called the DoctrineDataFixtureModule and can be found on GitHub.

Install it via composer: hounddog/doctrine-data-fixture-module and enable DoctrineDataFixtureModule in your application.config.php. If you configured doctrine already, just add this to your FooModule's module.config.php:

'data-fixture' => array(
    'location' => __DIR__ . '/../fixtures',

And in that module's directory create a subdirectory called "fixtures". Put your classes there and run vendor/bin/doctrine-module data-fixture:import to import the fixtures. An example of a fixture class could look like this

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download