问题
I'm using PhoneNumberBundle for validates phone number on my application. I'm using also NelmioAliceBundle together with AliceFixtureBundle. Having that as start point I'm writing a fixture for a entity that has a PhoneNumberBundle
assert for validate the phone number. Here is a snippet of that file:
/**
* @AssertPhoneNumber(defaultRegion="VE")
* @ORM\Column(name="phone", type="phone_number", length=11)
*/
protected $phone;
I don't know how to use external libraries on the fixture itself so the only solution I see if to write my own faker and return the well formated number phone and pass back to the fixture. Then I did this:
TananeFakerProvider.php
class TananeFakerProvider {
public function formatPhoneNumber($fakePhoneNumber)
{
return $this->container->get('libphonenumber.phone_number_util')->parse($fakePhoneNumber);
}
}
services.yml
services:
tanane.faker.provider:
class: CommonBundle\Tools\TananeFakerProvider
arguments: ["@service_container"]
tags:
- { name: h4cc_alice_fixtures.provider }
And finally Orders.yml (the fixture):
FrontendBundle\Entity\Orders:
Orders{1..50}:
nickname: <text(15)>
# trying to pass the fake number back to the custom faker
phone: <formatPhoneNumber(phoneNumber())>
email: <companyEmail()>
fiscal_address: <address()>
shipping_address: <address()>
shipping_from: <randomElement(array('MRW','DOMESA', 'ZOOM'))>
payment_type: @PaymentType*
order_amount: <randomFloat(2)>
bank: @Bank*
transaction: <randomNumber()>
comments: <sentence(15)>
secure: <boolean(35)>
person: <randomElement(array(@Natural*, @Legal*))>
status: @OrderStatus*
But I got this error:
[Symfony\Component\Debug\Exception\ContextErrorException] Notice: Use of undefined constant phoneNumber - assumed 'phoneNumber' in /var/www/html/vendor/nelmio/alice/src/Nelmio/Alice/Loader/Base.php(630) : eval()'d code line 1
So I'm passing the value in the wrong way, could any give me some help on this? Or maybe give me another idea in how to achieve this?
回答1:
(Settled on github, but for posterity here is a copy)
You can use the following in Alice 1.x:
FrontendBundle\Entity\Orders:
Orders{1..50}:
nickname: <text(15)>
# trying to pass the fake number back to the custom faker
phone: <formatPhoneNumber($this->fake('phoneNumber'))>
And this in the 2.x line:
FrontendBundle\Entity\Orders:
Orders{1..50}:
nickname: <text(15)>
# trying to pass the fake number back to the custom faker
phone: <formatPhoneNumber($fake('phoneNumber'))>
回答2:
As an alternative you can pass a Faker Provider object as a second parameter when instantiating a Nelmio/Alice/Fixtures/Loader class.
1.- Create a Provider Class
class MyCustomFakeProvider
{
public function tell_me_something()
{
return 'something';
}
}
2.-
use Nelmio\Alice\Fixtures\Loader;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
class ExampleDataLoader extends AbstractFixture implements OrderedFixtureInterface
{
public function loadMyDataFixtures() {
$myCustomFakeProvider = new MyCustomFakeProvider();
$loader = new Loader('es_ES', $myCustomFakeProvider);
$objects = $loader->load(__DIR__ . '/example.yml');
$persister = new \Nelmio\Alice\Persister\Doctrine($manager);
$persister->persist($objects);
3.- Finally create a example.yml file (at the same level than the Loader class in this case) with some data and a custom call to tell_me_something()
AppBundle\Entity\Example:
examples_1{1..101}:
title: <tell_me_something()> # MY CUSTOM METHOD
description: <sentence()>
来源:https://stackoverflow.com/questions/26104851/how-to-pass-faker-data-result-to-a-custom-function