protected function execute(InputInterface $input, OutputInterface $output) { $app = $this->app; $removeExistingData = $input->getOption('remove-existing-data'); if ($removeExistingData) { try { $app['db']->query('SET foreign_key_checks = 0;'); $tables = $app['db']->getSchemaManager()->listTables(); foreach ($tables as $table) { $table = $table->getName(); $app['db']->query('TRUNCATE TABLE ' . $table . ';'); } $app['db']->query('SET foreign_key_checks = 1;'); $output->writeln('<info>All tables were successfully truncated!</info>'); } catch (\Exception $e) { $output->writeln('<error>' . $e->getMessage() . '</error>'); } } /***** Users *****/ $users = (include APP_DIR . '/fixtures/users.php'); foreach ($users as $user) { $userEntity = new UserEntity(); $profileEntity = new ProfileEntity(); // Profile $profileEntity->setFirstName($user['profile']['firstName'])->setLastName($user['profile']['lastName']); if (isset($user['profile']['gender'])) { $profileEntity->setGender($user['profile']['gender']); } if (isset($user['profile']['birthdate'])) { $profileEntity->setBirthdate($user['profile']['birthdate']); } // User $userEntity->setId($user['id'])->setUsername($user['username'])->setEmail($user['email'])->setPlainPassword($user['plainPassword'], $app['security.encoder_factory'])->setRoles($user['roles'])->setProfile($profileEntity)->enable(); $app['orm.em']->persist($userEntity); } try { $app['orm.em']->flush(); $output->writeln('<info>Data was successfully hydrated!</info>'); } catch (\Exception $e) { $output->writeln('<error>' . $e->getMessage() . '</error>'); } }
protected function execute(InputInterface $input, OutputInterface $output) { $app = $this->app; $removeExistingData = $input->getOption('remove-existing-data'); if ($removeExistingData) { try { $app['db']->query('SET foreign_key_checks = 0;'); $tables = $app['db']->getSchemaManager()->listTables(); foreach ($tables as $table) { $table = $table->getName(); $app['db']->query('TRUNCATE TABLE ' . $table . ';'); } $app['db']->query('SET foreign_key_checks = 1;'); $output->writeln('<info>All tables were successfully truncated!</info>'); } catch (\Exception $e) { $output->writeln('<error>' . $e->getMessage() . '</error>'); } } /***** Roles *****/ $roles = (include APP_DIR . '/fixtures/roles.php'); foreach ($roles as $role) { $roleEntity = new RoleEntity(); $roleEntity->setId($role[0])->setName($role[1])->setDescription($role[2])->setRole($role[3])->setPriority($role[4]); $app['orm.em']->persist($roleEntity); } /* * We already need to flush the first time here, * else the roles are not available later for the users, * who want to use them. */ $app['orm.em']->flush(); /***** Users *****/ $users = (include APP_DIR . '/fixtures/users.php'); foreach ($users as $user) { $userEntity = new UserEntity(); $profileEntity = new ProfileEntity(); // Profile $profileEntity->setFirstName($user['profile']['firstName'])->setLastName($user['profile']['lastName']); if (isset($user['profile']['gender'])) { $profileEntity->setGender($user['profile']['gender']); } if (isset($user['profile']['birthdate'])) { $profileEntity->setBirthdate($user['profile']['birthdate']); } // User Roles $userRolesCollection = new ArrayCollection(); if (!empty($user['roles'])) { $userRoles = $user['roles']; foreach ($userRoles as $userRole) { $roleEntity = $app['orm.em']->getRepository('Application\\Entity\\RoleEntity')->findOneByRole($userRole); if ($roleEntity) { $userRolesCollection->add($roleEntity); } } } // User $userEntity->setId($user['id'])->setUsername($user['username'])->setEmail($user['email'])->setPlainPassword($user['plainPassword'], $app['security.encoder_factory'])->setRoles($userRolesCollection)->setProfile($profileEntity)->enable(); $app['orm.em']->persist($userEntity); } try { $app['orm.em']->flush(); $output->writeln('<info>Data was successfully hydrated!</info>'); } catch (\Exception $e) { $output->writeln('<error>' . $e->getMessage() . '</error>'); } }