I'm using Alice in a Symfony 2 bundle to load fixtures. I'm trying to customize the name of an entity using the name of a parent entity. Specifically, I have an entity, @Report1
, whose @Report1->name
property returns Test Report 1
.
I'm trying to create a child entity with the name Test Report 1 Scenario 1
. Here's my fixture file snippet:
AppBundle\Entity\Scenario:
Scenario1:
report_id: @Report1->id
name: @Report1->name 'Scenario 1'
All I get back is the literal @Report1->name 'Scenario 1'
.
If I remove the 'Scenario 1'
string from thename:
property, I get the parent report's actual name.
I am using hautelook/alice-bundle 1.3.1 (nelmio/alice 2.2.0 and fzaninotto/faker 1.6.0) with php 5.6.
You can configure a custom data provider with a variable-length arguments list and use it in your fixtures files.
In AppBundle/DataFixtures/ORM/DataLoader.php:
namespace AppBundle\DataFixtures\ORM;
use Hautelook\AliceBundle\Doctrine\DataFixtures\AbstractLoader;
class DataLoader extends AbstractLoader
{
/**
* {@inheritdoc}
*/
public function getFixtures()
{
return [
__DIR__.'/../Fixtures/user.yml',
];
}
public function concat(...$strings)
{
$result = '';
foreach ($strings as $string) {
$result .= $string;
}
return $result;
}
}
Now you can use the custom data provider like this.
In AppBundle/DataFixtures/Fixtures/user.yml:
AppBundle\Entity\User:
user-{1..10}:
firstname: <firstname()>
lastname: <lastname()>
email: <concat(@self->firstname, ".", @self->lastname, "@gmail.com")>
plainPassword: 123
username: <concat(@self->firstname, ".", @self->lastname)>
It's a bit tedious to write but it works.
And for php versions prior to php 5.6, you can use func_get_args()
to get the list of arguments in DataLoader.php, like this:
public function concat()
{
$result = '';
foreach (func_get_args() as $string) {
$result .= $string;
}
return $result;
}