Alice Faker 库从数组中随机选择

Alice Faker library choose random from array

我正在尝试使用 AliceBundle for Symfony Framework. Everything seems to be working fine except I am looking for a way to randomly assign data from an array to a property called type. Looking at the faker library 生成虚拟数据 我可以看到我可以使用 randomElement($array = array ('a','b','c'))

生成它

我正在尝试将其转换为 YML,我认为这相当于

<randomElement(['a','b','c'])>

但这会产生错误

[Nelmio\Alice\Throwable\Exception\FixtureBuilder\ExpressionLanguage\LexException] Could not lex the value "['a'".

这是我的完整yml

AppBundle\Entity\Job:
    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*"

我最终创建了一个自定义提供程序

namespace AppBundle\DataFixtures\Faker\Provider;

    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: AppBundle\DataFixtures\Faker\Provider\JobTypeProvider
    tags: [ { name: nelmio_alice.faker.provider } ]

然后在yml文件中使用

AppBundle\Entity\Job:
    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*"

Notice type: , this is being generated from service now.

这对我有用:

parameters:
    profileArray: ['PUBLIC', 'PRIVATE', 'AUTHENTICATED']

JobPlatform\AppBundle\Entity\Profile:
    profiles_{1..100}:
        user: '@user_<current()>'
        visibility: <randomElement($profileArray)>