creakiwi / yaml-fixtures
在 DoctrineFixturesBundle 上构建的 Symfony YamlFixtures
Requires
- doctrine/doctrine-fixtures-bundle: ~2.2
- symfony/yaml: ~2.3
This package is not auto-updated.
Last update: 2024-09-28 16:10:05 UTC
README
概述
这是一个简单的抽象类,用于在 DoctrineFixturesBundle 上使用 yaml fixtures,而不是 PHP 类。它还允许使用特定的关键字,如 %self%(yaml 定义的引用键,在 key => mixed 值声明的情况下)或 %field%(其中 "field" 指的是先前定义的字段)。最后,此捆绑包通过 @ 前缀处理一对一、一对多和多对多关系。
警告
当前此捆绑包未进行单元测试,使用它存在风险!您可能需要它来引导数据库,但在生产模式下永远不要使用它(我认为这也是 DoctrineFixturesBundle 的规则)。
示例
1. 关键字使用示例
假设您有一个由以下字段组成的唯一实体 User
- id : 自动生成,此处不使用
- username
首先,您必须扩展 YamlFixtures 类
// src/Acme/DemoBundle/DataFixtures/ORM
<?php
namespace Acme\DemoBundle\DataFixtures\ORM;
use Ck\Component\YamlFixtures\YamlFixture;
use Acme\DemoBundle\Entity\User;
class LoadUserData extends YamlFixture
{
protected function getEntity()
{
return new User();
}
protected function getFilePath()
{
return sprintf('%s/../Fixtures/users.yml', __DIR__);
}
protected function getReferencePrefix()
{
return 'user';
}
public function getOrder()
{
return 1;
}
}
现在,我们需要一些 yaml fixtures
# src/Acme/DemoBundle/DataFixtures/Fixtures/users.yml
alex:
username: alex-ception
email: alexandre@creakiwi.com
hubert:
username: %self% # aka "hubert"
email: hubert@creakiwi.com
foo:
username: foo@example.com
email: %username% # aka "foo@example.com"
"bar@example.com":
username: %self%
email: %self% # or %username% in this case is also possible
然后就是这样。您可以通过 DoctrineFixturesBundle 运行标准命令来生成您的 fixtures。
请注意,与每个根元素的关键值标识相比,您也可以使用一个简单的列表,但您将失去使用 %self% 关键字或管理关系的功能,就像我们稍后将要看到的。
-
username: alex-ception
email: alexandre@creakiwi.com
-
username: hubert
email: hubert@creakiwi.com
2. 一对一和一对多使用示例
在这个例子中,我将只展示一对多关系,因为它对我们来说与一对一行为相同(ORM 负责检查一对一的特殊性)
现在我们将添加一个 Comment 实体(允许用户添加评论),它有两个字段
- owner : 与 User 的 M2O 关系
- comment : 评论的内容 此外,我们假设实体已创建,并具有处理关系的正确参数。例如:
- id : 自动生成,此处不使用
- owner (评论的用户)
- comment : 文本
让我们编写这个类:// src/Acme/DemoBundle/DataFixtures/ORM <?php
namespace Acme\DemoBundle\DataFixtures\ORM;
use Ck\Component\YamlFixtures\YamlFixture;
use Acme\DemoBundle\Entity\Comment;
class LoadCommentData extends YamlFixture
{
protected function getEntity()
{
return new Comment();
}
protected function getFilePath()
{
return sprintf('%s/../Fixtures/comments.yml', __DIR__);
}
protected function getReferencePrefix()
{
return 'comment';
}
public function getOrder()
{
// We want to load these fixtures after, since we depends on users fixtures
return 2;
}
}
以及 fixtures
# src/Acme/DemoBundle/DataFixtures/Fixtures/comments.yml
comment1:
description: "my very nice comment"
@owner: user-alex
mycomment:
description: "I'm an elephant"
@owner: user-hubert
another-comment:
description: "Boo !!!!"
@owner: user-foo
概念如下,为了使 YamlFixture 理解 owner 是一个关系,您必须使用 @ 字符前缀。但是,为了将其链接到某个实体,您需要知道是哪个实体,这就是 getReferencePrefix() 方法的目的,然后您需要实体的键。总结如下:
@field: referencePrefix-entityKey
3. 多对多使用示例
这部分将会很快,因为您已经理解了机制,在关系的单个字符串上,您可以使用一个列表。
现在我们有一个与评论通过多对多关系相关的 Tag 实体。我们假设您已经创建了具有正确参数的 Tag 实体以及 LoadTagData。
我们只需要交换评论和标签的顺序
- 标签的顺序是 2(并且前缀设置为 "tag")
- 评论的顺序是 3
Fixtures
# src/Acme/DemoBundle/DataFixtures/Fixtures/tags.yml
tag1:
name: Tag 1
tag2:
name: Tag 2
tag3:
name: Tag 3
# src/Acme/DemoBundle/DataFixtures/Fixtures/comments.yml
comment1:
description: "my very nice comment"
@owner: user-alex
@tag:
- tag-tag1
- tag-tag2
mycomment:
description: "I'm an elephant"
@owner: user-hubert
@tag:
- tag-tag2
- tag-tag3
another-comment:
description: "Boo !!!!"
@owner: user-foo
@tag:
- tag-tag1
- tag-tag3
关于多对多关系的唯一要了解的事情是,它不是复数的,您必须让它成为单数,YamlFixture 将使用 "add" 方法。