/** * @Route("/create", name="create_objects") * * @Rest\View() * * @return Response */ public function createAction() { // We need an entity manager here $em = $this->getDoctrine()->getManager(); // First, we try to create a post $post = new Post(); $post->setTitle('Hello World'); $post->setContent('This is a hello world post'); $post->setCreated(new \DateTime()); $em->persist($post); // Create new post log object $postLog = new PostLog(); $postLog->setMessage('A new post was created'); $postLog->setCreated(new \DateTime()); $postLog->setParent($post); $em->persist($postLog); // Try to create a category $category = new Category(); $category->setTitle('A Category'); $category->setDescription('A category to created'); $em->persist($category); // Create new category log object $categoryLog = new CategoryLog(); $categoryLog->setMessage('A new category was created'); $categoryLog->setCreated(new \DateTime()); $categoryLog->setParent($category); $em->persist($categoryLog); // Actually store all the entities to the database // to get id of the post and the category $em->flush(); return ['Done']; }
public function load(ObjectManager $manager) { $locales = $this->container->get('locales'); $category = new Category($locales->getLocaleActive()); $category->setName('Light side of the Force'); $category->setDescription('The Light Side of the Force was aligned with calmness and was used for knowledge and defense'); $manager->persist($category); $this->addReference('category', $category); foreach ($locales->getLocales() as $locale) { if (!$locale['active']) { $translationCategory = new Category($locale['code']); $translationCategory->setName($locale['code'] . ' Light side of the Force'); $translationCategory->setDescription($locale['code'] . ' The Light Side of the Force was aligned with calmness and was used for knowledge and defense'); $translationCategory->setParentMultilangue($category); $manager->persist($translationCategory); $reference = 'translationcategory-' . $locale['code']; $this->addReference($reference, $translationCategory); } } $manager->flush(); }
/** * {@inheritDoc} */ public function setDescription($description) { $this->__initializer__ && $this->__initializer__->__invoke($this, 'setDescription', array($description)); return parent::setDescription($description); }
/** * 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)); } }