3

I'm using 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). What I can't figure out is how I can generate fixture data for the nested set entity - making sure that groups are generated as a tree with accurate root IDs and parents. Thanks for any guidance.

My current fixture file is as simple as;

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

3 回答 3

5

我已经通过手动定义嵌套集的每个级别的组来使其工作,如下所示;

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

  group_level_1{1..50}:
    parent: '@group_root*'
    title: <word()>
于 2016-07-02T17:11:09.683 回答
1

您可以像这样生成您的实体(在我的示例中它将是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()- 将首先和第二个生成控件的实体

于 2016-07-02T15:42:05.173 回答
0

要构建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. 只需确保提供一个整数(圆形、地板、天花板、...)。

于 2019-05-10T12:13:34.450 回答