使用 Alice Fixtures 为 Nested Set 实体生成 Doctrine fixture 数据

Generating Doctrine fixture data for Nested Set entity with Alice Fixtures

我正在使用 hautelook/AliceBundle (which uses nelmio/alice and fzaninotto/Faker) to generate fixtures for an application. I have a Doctrine entity Group which is a nested set entity (using the Tree functionality provided by StofDoctrineExtensionsBundle)。我无法弄清楚的是如何为嵌套集实体生成夹具数据 - 确保将组生成为具有准确根 ID 和父项的树。感谢您的指导。

我当前的夹具文件很简单;

MyBundle\Entity\Group:
  group{1..25}:
    title: <word()>

您可以像这样生成您的实体(在我的示例中它将是 User

class LoadUserData extends AbstractFixture implements OrderedFixtureInterface
{
    public function load(ObjectManager $manager)
    {
        $admin = new User();
        $admin->setUsername('admin');
        $admin->setFirstName('John');
        $manager->persist($admin);
        $manager->flush();
        $this->addReference('test-user', $admin);
    }
    public function getOrder()
    {
        return 1;
    }
}

然后将其作为依赖使用:

class LoadQuestionData extends AbstractFixture implements OrderedFixtureInterface
{
    public function load(ObjectManager $manager)
    {
        $question = new Question();
        $question->setSubject('Test Question');

        /** @var User $user */
        $user = $this->getReference('test-user');
        $question->setUser($user);
        $manager->persist($question);
        $manager->flush();
    }
    public function getOrder()
    {
        return 2;
    }
}

getOrder() - 将首先和第二生成实体的控件

我通过像这样为嵌套集的每个级别手动定义组来让它工作;

MyBundle\Entity\Group:
  group_root{1..5}:
    title: <word()>

  group_level_1{1..50}:
    parent: '@group_root*'
    title: <word()>

要构建 Binary_tree 我使用这个:

App\Entity\Group:
    group1:
        title: <word()>
        # this is the tree root, do not set a parent
    group{2..100}:
        title: <word()>
        parent: '@group<(floor($current/2))>'

你当然可以选择某事。不同于 x/2。请务必提供一个整数(round、floor、ceil、...)。