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()>
James Crinkley
  • 1,398
  • 1
  • 13
  • 33

3 Answers3

5

I've got it to work by manually defining the groups for each level of the nested set like so;

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

  group_level_1{1..50}:
    parent: '@group_root*'
    title: <word()>
James Crinkley
  • 1,398
  • 1
  • 13
  • 33
1

You can generate your entity (in my example it will be User) like this

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;
    }
}

and then use it as a dependency:

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() - controlls wich entity will be generated first and second

arogachev
  • 33,150
  • 7
  • 114
  • 117
D.Dimitrioglo
  • 3,413
  • 2
  • 21
  • 41
  • 1
    Thanks, but this isn't really what I'm looking for. I could manually define fixtures in YAML files and refer to other groups without needing to create PHP fixtures (which is what Alice is for anyway) but I was wondering if there was a way that Alice could handle Nested Set relationships automatically. – James Crinkley Jul 02 '16 at 15:53
  • I do understand your question, but it was just an example how i did it. – D.Dimitrioglo Jul 02 '16 at 16:43
  • Fair enough, I've gone with the method in my answer anyway which works as I wanted – James Crinkley Jul 02 '16 at 17:11
0

To build a Binary_tree I use this:

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))>'

You can of course choose sth. different than x/2. Just be sure to provide an integer (round, floor, ceil, ...).

auipga
  • 300
  • 3
  • 10