问题
I'm new to symfony 4 and tried to write my own function for yml nelmio/alice, but after I ran bin/console doctrine:fixtures:load
, I got this error:
In DeepCopy.php line 177:
The class "ReflectionClass" is not cloneable.
Here is my fixtures.yml file:
App\Entity\Post:
post_{1..10}:
title: <customFunction()>
Here is my AppFixture.php file:
<?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';
}
}
回答1:
After investigating about nelmio/alice new version, I found the solution:
We should first create a provider which is a class that contains our new custom functions. Second, extend the NativeLoader
class to register our new provider. Third, use our new NativeLoader (Here is CustomNativeLoader) which allows us to use our new formatters.
Here is the provider in 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;
}
}
As a side note, the title
function generates some dummy titles for your articles or posts or etc. which have dynamic length between 20..100 character. This function use Faker Alice built-in formatters.
Here is 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;
}
}
Here is 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();
}
}
And finally an example for using our new title
formatter:
App\Entity\Post:
post_{1..10}:
title: <title()>
I know this is a long way for writing a small function but according to my investigation, it's a standard way for extending the formatters in new version.
回答2:
You can do it this simple way:
1.Create your own provider (for example, in /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.Register provider in config/services.yaml:
App\DataFixtures\Faker\CustomProvider:
tags: [ { name: nelmio_alice.faker.provider } ]
That's it.
P.S. You can check for more details here: https://github.com/hautelook/AliceBundle/blob/master/doc/faker-providers.md
来源:https://stackoverflow.com/questions/49607510/how-to-create-custom-function-formatter-nelmio-alice-v-3