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 f开发者_Go百科lat 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?
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
There is a git submodule for that in the official doctrine git repo https://github.com/doctrine/data-fixtures
I am currently using it and it works pretty well.
I use class-based fixtures, much better this way because you can handle associations and dependencies easily with the EntityManager directly, also easy for using in unit tests.
Here is the library I use with Zend Framework modules, but you can just write your own loader. There is a command line script too.
精彩评论