2

我是 symfony 4 的新手,并试图为 yml nelmio/alice 编写我自己的函数,但在我运行之后bin/console doctrine:fixtures:load,我得到了这个错误:

在 DeepCopy.php 第 177 行:

“ReflectionClass”类是不可克隆的。

这是我的 fixtures.yml 文件:

App\Entity\Post:
post_{1..10}:
    title: <customFunction()>

这是我的 AppFixture.php 文件:

<?php

namespace App\DataFixtures;

use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Nelmio\Alice\Loader\NativeLoader;



class AppFixtures extends Fixture
{
    public function load(ObjectManager $manager)
    {
        $loader = new NativeLoader();

        $objectSet = $loader->loadFile(__DIR__.'/Fixtures.yml',
            [
                'providers' => [$this]
            ]
        )->getObjects();

        foreach($objectSet as $object) {
            $manager->persist($object);
        }

        $manager->flush();
    }

    public function customFunction() {

        // Some Calculations

        return 'Yep! I have got my bonus';
    }

}
4

2 回答 2

4

在调查了关于 nelmio/alice 新版本之后,我找到了解决方案:

我们应该首先创建一个提供者,它是一个包含我们新的自定义函数的类。其次,扩展NativeLoader类来注册我们的新提供者。第三,使用我们新的 NativeLoader(这里是 CustomNativeLoader),它允许我们使用我们的新格式化程序。

这是 CustomFixtureProvider.php 中的提供程序:

<?php


namespace App\DataFixtures;
use Faker\Factory;

class CustomFixtureProvider
{
    public function title()
    {
        $faker = Factory::create();

        $title = $faker->text($faker->numberBetween(20,100));

        return $title;

    }
}

作为旁注,该title函数会为您的文章或帖子等生成一些虚拟标题,其动态长度在 20..100 个字符之间。此功能使用 Faker Alice 内置格式化程序。

这是 CustomNativeLoader.php:

<?php

namespace App\DataFixtures;

use Nelmio\Alice\Faker\Provider\AliceProvider;
use Nelmio\Alice\Loader\NativeLoader;
use Faker\Factory as FakerGeneratorFactory;
use Faker\Generator as FakerGenerator;

class CustomNativeLoader extends NativeLoader
{
    protected function createFakerGenerator(): FakerGenerator
    {
        $generator = FakerGeneratorFactory::create(parent::LOCALE);
        $generator->addProvider(new AliceProvider());
        $generator->addProvider(new CustomFixtureProvider());
        $generator->seed($this->getSeed());

        return $generator;
    }
}

这是 LoadFixture.php:

<?php
namespace App\DataFixtures;

use App\DataFixtures\CustomNativeLoader;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;

class LoadFixtures extends Fixture
{
    public function load(ObjectManager $manager)
    {
        $loader = new CustomNativeLoader();

        $objectSet = $loader->loadFile(__DIR__ . '/fixtures.yml')->getObjects();
        foreach($objectSet as $object) {
            $manager->persist($object);
        }
        $manager->flush();
    }
}

最后是使用我们的新title格式化程序的示例:

App\Entity\Post:
    post_{1..10}:
        title: <title()>

我知道这对于编写一个小函数来说还有很长的路要走,但根据我的调查,这是在新版本中扩展格式化程序的标准方法。

于 2018-04-02T10:26:26.827 回答
1

你可以这样做:

1.创建自己的提供者(例如,在/src/DataFixtures/Faker/CustomProvider.php中):

 <?php    
    namespace App\DataFixtures\Faker;


    class CustomProvider
    {
        public static function customFunction()
        {
            // Some Calculations
            return 'Yep! I have got my bonus';
        }
    }

2.在config/services.yaml中注册provider:

App\DataFixtures\Faker\CustomProvider:
    tags: [ { name: nelmio_alice.faker.provider } ]

而已。

PS您可以在此处查看更多详细信息:https ://github.com/hautelook/AliceBundle/blob/master/doc/faker-providers.md

于 2019-01-09T18:24:57.223 回答