Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle doctrine2 data fixtures (flat file)

I am looking at doctrine2 and how to handle data fixtures. I am especially interested in reading them from flat files (csv, yaml, xls).

In doctrine 1.2 data fixtures are handled like this: http://www.doctrine-project.org/projects/orm/1.2/docs/manual/data-fixtures/en#data-fixtures

Any suggestion how to handle this in doctrine2?

like image 731
udo Avatar asked Feb 21 '11 19:02

udo


1 Answers

As already mentioned by Steven the fixture-feature comes as a separate repo. It took me some time to figure out how to install the data fixtures feature in Symfony2, so here is how I did it:

add sources to your 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

update your vendors

$ php bin/vendors install

register in in autoload.php:

$loader->registerNamespaces(array(
    //...
   'Doctrine\\Common\\DataFixtures' => __DIR__.'/../vendor/doctrine-fixtures/lib',
   'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib',
    //..
));

add class which subclasses FixtureInterface:

<?php
use Doctrine\ORM\EntityManager,
    Doctrine\Common\DataFixtures\FixtureInterface;
/**
 * 
 * setup of initial data for the unit- and functional tests
 * @author stephan
 */
class LoadTestingData implements FixtureInterface{
    /**
     *
     * @param EntityManager $manager 
     */
    public function load($manager) {
        $user = new User();
        $user->setUsername("testuser");

        $manager->persist($user);
    }
//...

load data fixtures via console command

./app/console doctrine:data:load
like image 75
stoefln Avatar answered Oct 03 '22 06:10

stoefln