codeedu / zendexpr-doctrine-fixture

提供Doctrine数据固定功能功能的Zend Expressive库

0.1.1 2020-06-08 23:48 UTC

This package is auto-updated.

Last update: 2024-09-09 09:17:40 UTC


README

Build Status

关于

此库提供了与Zend Expressive和Doctrine数据固定的集成。还支持PSR-11。

开始使用

安装
composer require codeedu/zendexpr-doctrine-fixture:0.1.1
注册固定数据

要注册固定数据,请将固定数据添加到您的配置中。

[
      'doctrine' => [
            'fixture' => [
                  'MyFixtures' => __DIR__ . '/../src/Fixture',
            ]
      ]
];

注册工厂以创建命令

[
      'factories' => [
         'doctrine:fixtures_cmd:load'   => \CodeEdu\FixtureFactory::class
      ]
];

我们建议使用以下代码片段来配置Doctrine ORM和命令,使其与Zend Expressive一起使用。此配置使用DoctrineModule。DoctrineModule提供了方便的配置,以便将Doctrine ORM集成到Zend Framework 2应用程序中,因此这种方法非常愉快。

现在在 doctrine.config.php 中,添加 doctrine:fixtures_cmd:load

$command = [
   //.....,
   'doctrine:fixtures_cmd:load'
];

用法

命令行

以下是如何访问Doctrine命令行

导入

./vendor/bin/doctrine-module data-fixture:import

与固定数据一起进行依赖注入

此库提供了在固定数据中注入服务容器的功能。因此,添加接口 FixtureContainerInterface,见下文

class MyFixture implements FixtureInterface, FixtureContainerInterface
{
    private $container;

    public function load(ObjectManager $manager){
        $myService = $this->container->get(MyService::class);
    }

    public function getContainer()
    {
        return $this->container;
    }

    public function setContainer(ContainerInterface $container)
    {
        $this->container = $container;
    }
}