/** * Run the database seeds. * * @return void */ public function run() { // Genereate sample domain doctrine entities. $em = app('EntityManager')->getFacadeRoot(); $generator = \Faker\Factory::create(); $populator = new Faker\ORM\Doctrine\Populator($generator, $em); $populator->addEntity('Lifestutor\\Data\\Entities\\User\\User', 3, array('password' => function () use($generator) { return bcrypt(str_random(10)); }, 'token' => function () use($generator) { return str_random(10); })); $insertedPKs = $populator->execute(); }
protected function execute(InputInterface $input, OutputInterface $output) { $number = (int) $input->getOption('qtd'); if ($number == 0) { throw new \Exception("Passar quantidade como paramentro"); } $em = $this->getContainer()->get('doctrine')->getManager(); $entities = array(); $metaFactory = $em->getMetadataFactory()->getAllMetadata(); foreach ($metaFactory as $meta) { $entities[] = $meta->getName(); } $entities[] = 'no'; //Remove o BaseEntity do array autocomplete unset($entities[array_search('Bacon\\Bundle\\CoreBundle\\Entity\\BaseEntity', $entities)]); $validation = function ($entity) use($entities) { if (!in_array($entity, array_values($entities))) { throw new \InvalidArgumentException(sprintf('A entity "%s" não é valida.', $entity)); } return $entity; }; $dialog = $this->getHelperSet()->get('dialog'); while (true) { $returnEntity = $dialog->askAndValidate($output, '<info>Deseja remover alguma entidade? [no] : </info>', $validation, false, 'no', $entities); if ($returnEntity == 'no') { break; } if (in_array($returnEntity, array_values($entities))) { unset($entities[array_search($returnEntity, $entities)]); } $output->writeln(sprintf('Você removeu a entidade: %s', $returnEntity)); } //Remove a opção no do array autocomplete unset($entities[array_search('no', $entities)]); $output->writeln("<comment>========== PROCESSANDO O FAKER ===========</comment>"); $generator = Factory::create(); $populate = new \Faker\ORM\Doctrine\Populator($generator, $this->getContainer()->get('doctrine')->getManager()); foreach ($entities as $entity) { $populate->addEntity($entity, $number); } $populate->execute(); $output->writeln("<comment>========== FINALIZADO ====================</comment>"); }
/** * Run the database seeds. * * @return void */ public function run() { /* * Generate Items */ $em = app('EntityManager')->getFacadeRoot(); $generator = \Faker\Factory::create(); $populator = new Faker\ORM\Doctrine\Populator($generator, $em); $populator->addEntity('Services\\Inventory\\Data\\Entities\\Item\\Item', 20, array('title.value' => function () use($generator) { return ucwords(implode(' ', $generator->words(3))); }, 'description.value' => function () use($generator) { return $generator->paragraph(3); }, 'price.value' => function () use($generator) { return $generator->randomFloat(2, 1, 50000); }, 'quantity.value' => function () use($generator) { return $generator->randomDigitNotNull; })); $generatedItems = $populator->execute(); }
/** * Run the database seeds. * * @return void */ public function run() { /* * Generate Users */ // Generate first user for admin role DB::table('users')->insert(['firstname' => 'Peter', 'lastname' => 'Parker', 'password' => bcrypt('pass'), 'contact_email' => '*****@*****.**']); // Generate users for member roles $em = app('EntityManager')->getFacadeRoot(); $generator = \Faker\Factory::create(); $populator = new Faker\ORM\Doctrine\Populator($generator, $em); $populator->addEntity('Services\\User\\Data\\Entities\\User\\User', 20, array('password' => function () use($generator) { return bcrypt('pass'); }, 'contact.email' => function () use($generator) { return $generator->email; })); $generatedUsers = $populator->execute(); /* * Generate Roles */ DB::table('roles')->insert([['id' => 1, 'name' => 'Admin'], ['id' => 2, 'name' => 'Member']]); /* * Generate Users' Roles */ // Generate the first user's admin role DB::table('role_user')->insert(['user_id' => 1, 'role_id' => 1]); foreach ($generatedUsers['Services\\User\\Data\\Entities\\User\\User'] as $user) { DB::table('role_user')->insert(['user_id' => $user->id, 'role_id' => 2]); } /* * Generate Carts for each User */ DB::table('carts')->insert(['user_id' => 1, 'modified_on' => '2015-11-08 12:00:000']); foreach ($generatedUsers['Services\\User\\Data\\Entities\\User\\User'] as $user) { $generator = \Faker\Factory::create(); $populator = new Faker\ORM\Doctrine\Populator($generator, $em); $populator->addEntity('Services\\Cart\\Data\\Entities\\Cart\\Cart', 1, array('user' => $user)); $generatedUsers = $populator->execute(); } }
/** * Gets the 'faker.populator' service. * * This service is shared. * This method always returns the same instance of the service. * * @return \Faker\ORM\Doctrine\Populator A Faker\ORM\Doctrine\Populator instance. */ protected function getFaker_PopulatorService() { $this->services['faker.populator'] = $instance = new \Faker\ORM\Doctrine\Populator($this->get('faker.generator'), $this->get('doctrine.orm.default_entity_manager')); $instance->addEntity($this->get('faker.entities.0'), 10, array('name' => $this->get('faker.entities.0.formatters.0'), 'price' => $this->get('faker.entities.0.formatters.1'), 'active' => $this->get('faker.entities.0.formatters.2')), array(), false); return $instance; }