Pass current variable into custom faker in Alice Nelmio Bundle Fixtures 2.x

1.7k Views Asked by At

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

There are 2 best solutions below

0
On

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

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

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.