Created
November 8, 2017 13:43
-
-
Save ahmed-bhs/bcf6d82b57223395e979bc3ccf8bb1c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//https://www.wanadev.fr/123-grace-a-faker-generez-des-donnees-aleatoires-dans-votre-base-de-donnees/ | |
// composer require fzaninotto/faker | |
namespace AppBundle\DataFixtures\ORM; | |
use AppBundle\Entity\User; | |
use Doctrine\Common\DataFixtures\AbstractFixture; | |
use Doctrine\Common\DataFixtures\FixtureInterface; | |
use Doctrine\Common\DataFixtures\OrderedFixtureInterface; | |
use Doctrine\Common\Persistence\ObjectManager; | |
use Faker; | |
use Symfony\Component\DependencyInjection\ContainerAwareInterface; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
class LoadUsers extends AbstractFixture implements ContainerAwareInterface, FixtureInterface, OrderedFixtureInterface | |
{ | |
private $container; | |
public function setContainer(ContainerInterface $container = null) | |
{ | |
$this->container = $container; | |
} | |
public function load(ObjectManager $em) | |
{ | |
// initialisation de l'objet Faker | |
// on peut préciser en paramètre la localisation, | |
// pour avoir des données qui semblent "françaises" | |
$faker = Faker\Factory::create('fr_FR'); | |
$populator = new Faker\ORM\Doctrine\Populator($faker, $em); | |
$populator->addEntity(User::class, 10); | |
// le deuxième paramètre (10) correspond au nombre d'objets qui vont être créés | |
$insertedPKs = $populator->execute(); | |
} | |
public function getOrder() | |
{ | |
return 1; | |
} | |
} | |
// ------------------- | |
class LoadData extends AbstractFixture implements ContainerAwareInterface, FixtureInterface, OrderedFixtureInterface | |
{ | |
private $container; | |
public function setContainer(ContainerInterface $container = null) | |
{ | |
$this->container = $container; | |
} | |
public function load(ObjectManager $em) | |
{ | |
// initialisation de l'objet Faker | |
$faker = Faker\Factory::create('fr_FR'); | |
// créations des shops | |
$shops = []; | |
for ($i=0; $i < 10; $i++) { | |
$shops[$i] = new Shop(); | |
$shops[$i]->setName($faker->company) | |
->setSiret($faker->siret) | |
->setReference($faker->numberBetween(111111, 999999)) | |
->setContactName($faker->name) | |
->setAddress($faker->address) | |
; | |
$em->persist($shops[$i]); | |
} | |
// créations des customers | |
$customers = []; | |
for ($k=0; $k < 50; $k++) { | |
$customers[$k] = new Customer(); | |
$customers[$k]->setFirstName($faker->firstName) | |
->setLastName($faker->lastName) | |
->setEmail($faker->mail) | |
->setPhone($faker->phone) | |
->setAddress($faker->address) | |
; | |
// on récupère un nombre aléatoire de Shops dans un tableau | |
$randomShops = (array) array_rand($shops, rand(1, count($shops))); | |
// puis on les ajoute au Customer | |
foreach ($randomShops as $key => $value) { | |
$customers[$k]->addShop($shops[$key]); | |
} | |
$em->persist($customers[$k]); | |
} | |
$em->flush(); | |
} | |
public function getOrder() | |
{ | |
return 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment