2

I've created a fixtures loader class in Symfony with custom fakers.

One of this custom functions should return a name from an array of names depending on a non-random value. I'd like this value to be the $current variable when creating a collection of entities with Alice 2.x

As we can do something like this in the fixtures.yml file:

user{1..10}:
  name: someName<current()>

I would like to pass that current value in to my custom function like so:

user{1..10}:
  name: pickFromArray($current)

I've tried $current, current(), , , .... without success.

Thanks!

2 Answers2

1

You need to use the returned value of <current()> function as parameter. Tested on hautelook/AliceBundle.

user{1..10}: 
  name: <pickFromArray(<current()>)>
0

I don't think you can do that. In my opinion, the best option is pick the name in the Processor:

public function postProcess($object)
{
    if (!$object instanceof User) {
        return false;
    }

    $object->setName($this->pickFromArray($object));

    return true;
}

Where pickFromArray() is a method in your Processor.

  • Thanks Guillaume! I've got a bunch of names and eventhough I don't care the order I don't want to use a random function because I want to avoid duplicates. I guess I can get this working by using a global static variable or sort of. – Victor Salvans Montesó Apr 13 '17 at 08:03
  • ok so in that case, you need to create your own Faker Formatter. See https://knpuniversity.com/screencast/alice-fixtures/fixtures, you can generate whatever data you want. Also, you can add the `(unique)` keyword in you yml files to avoid duplicates: https://github.com/nelmio/alice/blob/master/doc/complete-reference.md#handling-unique-constraints – Guillaume Renoult Apr 18 '17 at 04:24