Weitere ähnliche Inhalte
Ähnlich wie Doctrine fixtures (20)
Kürzlich hochgeladen (20)
Doctrine fixtures
- 2. Doctrine Fixtures
● Installation
○ composer.json
{
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "dev-master"
}
}
○ php composer.phar update doctrine/doctrine-fixtures-
bundle
○ app/appKernel.php
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
/* ... other bundles */
$bundles[] = new DoctrineBundleFixturesBundleDoctrineFixturesBundle();
}
- 3. DoctrineFixtures Structure
● Source folder : {BundlesName}DataFixturesORMLoad{EntityName}Data.php
● class naming : Load{EntityName}Data
● extends class:
○ DoctrineCommonDataFixturesAbstractFixture
○ method: Load(DoctrineCommonPersistenceObjectManager $em)
● Implements interface:
○ DoctrineCommonDataFixturesOrderedFixtureInterface
○ method: getOrder()
● Internal method
○ addReference($name,$entityObject);
○ hasReference($name)
○ getReference($name)
● Command: php app/console doctrine:fixtures:load
- 5. fzaninotto/Faker - Formats
Localation
● Person
● Address
● Company
● PhoneNumber
General
● Lorem
● Internet / UserAgent
● File
● Color
● Uuid
● DateTime
● Base(Random, Regex)
● Miscellaneous
● CreditCard
- 7. fzaninotto/Faker
Unique and Optional modifiers
● required value
$faker->randomDigit;
● non-required values (default 0.5)
$faker->optional($weight = 0.1)->randomDigit; // 10% chance to get null
$faker->optional($weight = 0.9)->randomDigit; // 90% chance to get null
● unique value();
$faker->unique()->randomDigit;
$faker->unique($reset=true)->randomDigit;
- 8. fzaninotto/Faker (work with ORM entity)
● Orm adapters : Propel, Doctrine2,
Mandango
$generator = FakerFactory::create();
$populator = new FakerORMPropelPopulator($generator);
$populator->addEntity('Author', 5);
$populator->addEntity('SomeOneBundleEntityBook', 10);
$insertedPKs = $populator->execute();
● Populator by guessing
○ column name
○ column type
○ column length
- 9. fzaninotto/Faker (work with ORM entity)
custom populating value
● Null Value
$populator->addEntity('Book', 5, array(
'CreatedAt' => null
));
● anonyomous function
$populator->addEntity('SomeOneBundleEntityPerson', 10,array(
'birthday' => function() use ($gen) {
return $gen->dateTimeBetween($startDate = '-80 years', $endDate
= '-10 years')->format("Y-m-d") ;
}
));
- 11. Other Agenda
● Alice Faker Library
○ https://github.com/nelmio/alice
○ https://github.com/hautelook/AliceBundle
● Image Faker ?
○ http://image-faker.rmhdev.net/
○ http://fakeimg.pl/