mgdsoft/ fixtures-generator-bundle
本Bundle提供了一种简单的Doctrine实体的 fixture 生成器
Requires
- php: ~7.0
- doctrine/doctrine-fixtures-bundle: ^3.1||^2.3
- doctrine/orm: ^2.5
- symfony/console: ^2.6||^3.0||^4.1.0
- symfony/process: ^3.0||^4.0
README
此bundle为Doctrine生成 fixtures 代码,您可以无问题覆盖所有代码
安装
composer require --dev "mgdsoft/fixtures-generator-bundle"
添加Bundle
对于Symfony 4,bundles.php
MGDSoft\FixturesGeneratorBundle\MgdsoftFixturesGeneratorBundle::class => ['dev' => true],
对于Symfony 3,AppKernel.php
if (in_array($this->getEnvironment(), ['dev', 'test'], true)) { $bundles[] = new MGDSoft\FixturesGeneratorBundle\MgdsoftFixturesGeneratorBundle() }
对于Symfony 4,跳过此部分,对于Symfony 3配置默认路径
mgdsoft_fixtures_generator: fixture_path_default: '%kernel.root_dir%/../src/AppBundle/DataFixtures/ORM' # Default %kernel.root_dir%/DataFixtures/ORM
给我一个示例 ¬¬
执行命令以生成Fixtures,默认情况下,它将获取您的项目的所有实体并生成开发和测试的所有 fixtures。如果您想为一个实体创建,请使用 --entity 选项,如果您想创建具有所有依赖项的 fixtures,请使用 -r 选项
bin/console mgdsoft:fixtures:generate
此执行将创建 3 个文件。 src/DataFixtures/ORM/LibsAuto/AbstractLoadUserFixture.php,此文件是一个抽象类,您可以在子类中覆盖所有方法,我们建议不要修改此类。
namespace App\DataFixtures\ORM\LibsAuto; use App\Entity\User; use MGDSoft\FixturesGeneratorBundle\LoaderFixtures\AbstractFixture; use Doctrine\Common\DataFixtures\DependentFixtureInterface; abstract class AbstractLoadUserFixture extends AbstractFixture implements DependentFixtureInterface { /** * (Skipped) all parameters to auto complete IDE */ protected function loadRow($key, array $overrideDefaultValues = []) { $obj = new User(); $defaultValues = $this->getDefaultValues(); $properties = array_merge($defaultValues, $overrideDefaultValues); foreach ($properties as $property => $value) { $this->propertyAccessor->setValue($obj, $property, $value); } $this->om->persist($obj); $this->addReference("user-".$key, $obj); } protected function getDefaultValues() { return [ // ---[ required values ]--- , 'username' => 'username', 'usernameCanonical' => 'usernameCanonical', 'email' => 'email', 'emailCanonical' => 'emailCanonical', 'password' => 'password', 'roles' => ["ROLE_SUPER_ADMIN"], 'colour' => 'colour', 'isOnline' => true, 'createdAt' => new \DateTime(), 'updatedAt' => new \DateTime(), 'salt' => 'salt', 'enabled' => true, 'plan' => $this->getReference("plan-1"), // ---[ required with default values ]--- , // 'showTips' => true, // ---[ non-mandatory fields ]--- , // 'lastLogin' => new \DateTime(), // 'confirmationToken' => 'confirmationToken', // 'passwordRequestedAt' => new \DateTime(), // 'name' => 'name', // 'lastName' => 'lastName', // 'avatar' => 'avatar', // 'initials' => 'initials', // 'lang' => 'en', // 'planDateEnd' => new \DateTime(), // 'stripeSubscriptionId' => 'stripeSubscriptionId', // 'userHasEmails' => $this->getReference("user_has_email-1"), // 'webPushes' => $this->getReference("user_web_push-1"), // 'user_resources' => $this->getReference("user_resources-1"), // 'tags' => $this->getReference("tag-1"), // 'guest' => $this->getReference("guest-1") ]; } public function getDependencies() { return [ 'App\DataFixtures\ORM\LoadPlanFixture', // ---[ non-mandatory fields ]---, // 'App\DataFixtures\ORM\LoadUserHasEmailFixture', // 'App\DataFixtures\ORM\LoadUserWebPushFixture', // 'App\DataFixtures\ORM\LoadUserResourcesFixture', // 'App\DataFixtures\ORM\LoadTagFixture', // 'App\DataFixtures\ORM\LoadGuestFixture' ]; } }
src/DataFixtures/ORM/LoadUserFixture.php,在这里您可以自定义您想要的内容。此类将在执行 doctrine fixtures (bin/console doctrine:fixtures:load) 时被加载
namespace App\DataFixtures\ORM; use App\DataFixtures\ORM\LibsAuto\AbstractLoadUserFixture; class LoadUserFixture extends AbstractLoadUserFixture { protected function loadRows() { $this->loadRow('1', []); } }
为了测试目的,还创建了 tests/Fixtures/General/LoadTestUserFixture.php。如果您不想创建此类,可以在配置中禁用
namespace Tests\Fixtures\General; use App\DataFixtures\ORM\LibsAuto\AbstractLoadUserFixture; class LoadTestUserFixture extends AbstractLoadUserFixture { protected function loadRows() { $this->loadRow('1', []); } }
如何插入多行?
src/DataFixtures/ORM/LoadUserFixture.php
namespace App\DataFixtures\ORM; use App\DataFixtures\ORM\LibsAuto\AbstractLoadUserFixture; class LoadUserFixture extends AbstractLoadUserFixture { protected function loadRows() { $this->loadRow('1', ['username' => 'Miguel1', 'email' => 'mgd1@mgdsoftware.com']); $this->loadRow('2', ['username' => 'Miguel2', 'email' => 'mgd2@mgdsoftware.com']); $this->loadRow('3', ['username' => 'Miguel3', 'email' => 'mgd3@mgdsoftware.com']); } }
每行插入都有一个 doctrine 引用,格式为 "class Prefix"-"$key"
如果您想为数组添加多个值,必须使用 "|" 符号
namespace App\DataFixtures\ORM; use App\DataFixtures\ORM\LibsAuto\AbstractLoadUserFixture; class LoadUserFixture extends AbstractLoadUserFixture { protected function loadRows() { $this->loadRow('1', ['comments|1' => $this->getReference('comment-2'), 'comments|2' => $this->getReference('comment-1') ]); } }
对于自动完成字段,请使用 phpstorm 中的 deep-assoc-completion(推荐)
要查看所有选项,请执行
bin/console mgdsoft:fixtures:generate -h
配置
mgdsoft_fixtures_creator: abstract_fixture_class: MGDSoft\FixturesGeneratorBundle\LoaderFixtures\AbstractFixture entity_path_default: App\Entity template: /<absolute>/MGDSoft/FixturesGeneratorBundle/DependencyInjection/../Generator/templates/basic.tpl template_lib: /<absolute>/MGDSoft/FixturesGeneratorBundle/DependencyInjection/../Generator/templates/lib.tpl fixture_path_default: '%kernel.root_dir%/DataFixtures/ORM' php_cs_fixer: php-cs-fixer generate_autocomplete_array_options: true test: template: /<absolute>/MGDSoft/FixturesGeneratorBundle/DependencyInjection/../Generator/templates/basic_test.tpl enabled: true fixture_path_default: '%kernel.root_dir%/../tests/Fixtures/General'
所有拉取请求都欢迎 😎