4

我正在尝试使用AliceBundle为 Symfony 框架生成一个虚拟数据。除了我正在寻找一种方法将数据从数组随机分配给名为type. 查看faker 库,我可以看到我可以使用randomElement($array = array ('a','b','c'))

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

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

但这会产生错误

[Nelmio\Alice\Throwable\Exception\FixtureBuilder\ExpressionLanguage\LexException] 无法对值“['a'”进行 lex。

这是我的完整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*"
4

2 回答 2

5

这对我有用:

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

JobPlatform\AppBundle\Entity\Profile:
    profiles_{1..100}:
        user: '@user_<current()>'
        visibility: <randomElement($profileArray)>
于 2017-07-30T10:28:49.917 回答
1

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

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*"

通知类型:,这是现在从服务中生成的。

于 2016-12-21T13:26:52.753 回答