【问题标题】:How to handle doctrine2 data fixtures (flat file)如何处理理论 2 数据夹具(平面文件)
【发布时间】:2011-02-21 19:59:42
【问题描述】:

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

在原则 1.2 中,数据夹具的处理方式如下:http://www.doctrine-project.org/projects/orm/1.2/docs/manual/data-fixtures/en#data-fixtures

任何建议如何在学说2中处理这个问题?

【问题讨论】:

    标签: php zend-framework fixtures doctrine-orm


    【解决方案1】:

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

    将来源添加到您的 deps 文件中:

    [教义装置] git=http://github.com/doctrine/data-fixtures.git [DoctrineFixturesBundle] git=http://github.com/symfony/DoctrineFixturesBundle.git 目标=/bundles/Symfony/Bundle/DoctrineFixturesBundle

    更新您的供应商

    $ php bin/vendors 安装

    在 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);
        }
    //...
    

    通过控制台命令加载数据夹具

    ./app/console doctrine:data:load
    

    【讨论】:

    【解决方案2】:

    官方教义 git repo https://github.com/doctrine/data-fixtures 中有一个 git 子模块

    我目前正在使用它,效果很好。

    【讨论】:

      【解决方案3】:

      我使用基于类的fixture,这样更好,因为您可以直接使用EntityManager轻松处理关联和依赖关系,也易于在单元测试中使用。

      这是带有 Zend 框架模块的 library I use,但您可以编写自己的加载器。还有一个command line script

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多