Example #1
0
 /**
  * This method is executed after interact() and initialize(). It usually
  * contains the logic to execute to complete this command task.
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $startTime = microtime(true);
     $name = $input->getArgument('name');
     $urlName = $input->getArgument('urlName');
     $description = $input->getArgument('description');
     $parent = $input->getArgument('parent');
     $parentObj = null;
     if (null !== $parent) {
         $parentObj = $this->em->getRepository('AppBundle:Category')->findOneBy(array('name' => $parent));
     }
     $existingCategory = $this->em->getRepository('AppBundle:Category')->findOneBy(array('name' => $name));
     if (null !== $existingCategory) {
         throw new \RuntimeException(sprintf('There is already a category "%s".', $name));
     }
     // create the user and encode its password
     $category = new Category();
     $category->setName($name);
     if (empty($urlName)) {
         $urlName = $name;
     }
     $category->setUrlName($urlName);
     $category->setDescription($description);
     $category->setParent($parentObj);
     $this->em->persist($category);
     $this->em->flush($category);
     $output->writeln('');
     $output->writeln(sprintf('[OK] %s was successfully created', $category->getName()));
     if ($output->isVerbose()) {
         $finishTime = microtime(true);
         $elapsedTime = $finishTime - $startTime;
         $output->writeln(sprintf('[INFO] New user database id: %d / Elapsed time: %.2f ms', $category->getId(), $elapsedTime * 1000));
     }
 }