Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: https://github.com/doctrine/data-fixtures

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 (schema-tool:create) 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!

Thanks!

like image 675
Ron Avatar asked Dec 20 '22 11:12

Ron


1 Answers

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

like image 73
Jurian Sluiman Avatar answered Dec 23 '22 01:12

Jurian Sluiman