/** * @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) { // TODO: Implement load() method. $category = new Category(); $category->setName('Default'); $manager->persist($category); foreach (range(1, 100) as $i) { $post = new Post(); $post->setTitle($this->getPostTitle()); $post->setSlug($this->container->get('slugger')->slugify($post->getTitle())); $post->setImage('post.jpeg'); $post->setContent($this->getPostContent()); $post->setAuthor('gunyem'); $post->setCreated(new \DateTime('now - ' . $i . 'days')); $post->setUpdated(new \DateTime('now - ' . $i . 'days')); $post->setCategory($category); foreach (range(1, 10) as $j) { $comment = new Comment(); $comment->setPost($post); $comment->setContent($this->getPostTitle()); $comment->setAuthor('gunyem'); $comment->setCreated(new \DateTime('now + ' . ($i + $j) . 'seconds')); $manager->persist($comment); $post->createComment($comment); } $manager->persist($post); } $manager->flush(); }
public function load(ObjectManager $manager) { foreach ($this->getData() as $payload) { $post = new Entity\Post(); $post->setTitle($payload[0]); $post->setContent($payload[1]); $post->setCreated($payload[2]); $manager->persist($post); } $manager->flush(); }