php - 如何处理 doctrine2 数据 fixture (平面文件)

标签 php zend-framework fixtures doctrine-orm

我正在研究 doctrine2 以及如何处理数据固定装置。我对从平面文件(csv、yaml、xls)中读取它们特别感兴趣。

在 doctrine 1.2 中,数据固定装置是这样处理的:http://www.doctrine-project.org/projects/orm/1.2/docs/manual/data-fixtures/en#data-fixtures

关于如何在 doctrine2 中处理这个问题有什么建议吗?

最佳答案

正如 Steven 已经提到的,fixture-feature 作为一个单独的 repo 提供。 我花了一些时间来弄清楚如何在 Symfony2 中安装数据 fixture 功能,所以我是这样做的:

将源添加到您的 deps 文件:

[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',
    //..
));

添加子类 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);
    }
//...

通过控制台命令加载数据 fixture

./app/console doctrine:data:load

关于php - 如何处理 doctrine2 数据 fixture (平面文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5070625/

相关文章:

php - json_encode/json_decode 与 Zend_Json::encode/Zend_Json::decode

php - 添加多个相同类型的子表单

Symfony2 测试 : Why should I use fixtures instead of managing data directly in test?

Django 模型中的 Python factory_boy 库 m2m?

php - 通过 ftp 双向同步 Netbeans 中的项目

php - Symfony 2 中 CSS 文件中的资源路径

zend-framework - Zend URL 帮助程序 $this->url 的问题

python - pytest:以 DRY 方式参数化装置

javascript - 我怎样才能在这个购物车php中插入数量

php - Laravel 通过电子邮件回复论坛