我正在尝试使用AliceBundle用于Symfony Framework生成虚拟数据。一切似乎都正常工作,除了我正在寻找一种将数据从数组中随机分配到称为type
的属性的方法。查看Faker库,我可以看到可以使用randomElement($array = array ('a','b','c'))
我试图将其转换为YML
,我认为这相当于
<randomElement(['a','b','c'])>
但这会产生错误
[nelmio alice throwable exception fixturebuilder expressionLanguage lexexception] 无法借出值" ['a'"。
这是我的完整yml
AppBundleEntityJob:
job{1..5}:
title: <jobTitle()>
description: <paragraph(3)>
length: "3_months_full_time"
type: <randomElement(['a','b','c'])>
bonus: <paragraph(3)>
expired_at: "2016-12-21"
job_user: "@emp*"
这对我有用:
parameters:
profileArray: ['PUBLIC', 'PRIVATE', 'AUTHENTICATED']
JobPlatformAppBundleEntityProfile:
profiles_{1..100}:
user: '@user_<current()>'
visibility: <randomElement($profileArray)>
我最终创建了一个自定义提供商
namespace AppBundleDataFixturesFakerProvider;
class JobTypeProvider
{
public static function jobType()
{
$types = array("paid", "unpaid", "contract");
$typeIndex = array_rand($types);
return $types[$typeIndex];
}
}
将其添加到services.yml
app.data_fixtures_faker_provider.job_type_provider:
class: AppBundleDataFixturesFakerProviderJobTypeProvider
tags: [ { name: nelmio_alice.faker.provider } ]
然后在yml文件中使用它
AppBundleEntityJob:
job{1..50}:
title: <jobTitle()>
description: <paragraph(3)>
length: <jobLength()>
job_industry: "@title*"
type: <jobType()>
bonus: <paragraph(3)>
expired_at: "2016-12-21"
job_user: "@emp*"
通知类型:,现在正在从服务生成。