public function load(ObjectManager $manager) { $user = $manager->getRepository('TaichiUserBundle:User')->findOneBy(['username' => 'heaven']); foreach (range(1, self::POST_NUMS) as $i) { $post = new Post(); $categories = $manager->getRepository('TaichiBlogBundle:Category')->findAll(); $categoryIds = []; foreach ($categories as $category) { $categoryIds[] = $category->getId(); } $cIdKey = array_rand($categoryIds, 1); $category = $manager->getRepository('TaichiBlogBundle:Category')->find($categoryIds[$cIdKey]); $post->setSubject(implode(' ', array_map('ucfirst', $this->faker->words(mt_rand(3, 5))))); $post->setAbstract($this->faker->paragraph(mt_rand(2, 4))); $post->setContent($this->faker->paragraph(mt_rand(6, 10))); $post->setUser($user); $post->setCategory($category); //Add tags $tags = $manager->getRepository('TaichiBlogBundle:Tag')->findAll(); $tagIds = []; foreach ($tags as $tag) { $tagIds[] = $tag->getId(); } $tIdKeys = array_rand($tagIds, mt_rand(2, 5)); foreach ($tIdKeys as $tIdKey) { $tag = $manager->getRepository('TaichiBlogBundle:Tag')->find($tagIds[$tIdKey]); $post->addTag($tag); } $post->setPictureUrl($this->faker->imageUrl(400, 240)); $post->setCreatedAt($this->faker->dateTimeBetween('-1 year', '-10 days')); $post->setUpdatedAt($post->getCreatedAt()); foreach (range(1, mt_rand(1, self::COMMENT_NUMS)) as $j) { $comment = new Comment(); $comment->setUser($user); $comment->setCreatedAt($this->faker->dateTimeBetween($post->getCreatedAt(), 'now')); $comment->setUpdatedAt($comment->getCreatedAt()); $comment->setContent($this->faker->paragraph(mt_rand(1, 3))); $comment->setPost($post); $manager->persist($comment); $post->addComment($comment); } $manager->persist($post); } $manager->flush(); }
/** * @param Post $post * @return \Symfony\Component\Form\Form */ private function createDeleteForm(Post $post) { return $this->createFormBuilder()->setAction($this->generateUrl('admin_post_delete', ['id' => $post->getId()]))->setMethod('DELETE')->getForm(); }