public function load(ObjectManager $manager) { $comment1 = new Comment(); $comment1->setTitle('Sample Comment 1'); $comment1->setUsername('blogger1'); $comment1->setComment('To make a long story short. You can\'t go wrong by choosing Symfony! And no one has ever been fired for using Symfony.'); $comment1->setApproved(true); $comment1->setCreated(new \DateTime()); $comment1->setCommentType('Blog'); $comment1->setBlogPost($manager->merge($this->getReference('blog1'))); $manager->persist($comment1); $comment2 = new Comment(); $comment2->setTitle('Sample Comment 2'); $comment2->setUsername('blogger2'); $comment2->setComment('To make a long story short. You can\'t go wrong by choosing Symfony! And no one has ever been fired for using Symfony 2.'); $comment2->setApproved(true); $comment2->setCreated(new \DateTime()); $comment2->setCommentType('Blog'); $comment2->setBlogPost($manager->merge($this->getReference('blog1'))); $manager->persist($comment2); $comment3 = new Comment(); $comment3->setTitle('Sample Comment 3'); $comment3->setUsername('blogger3'); $comment3->setComment('To make a long story short. You can\'t go wrong by choosing Symfony! And no one has ever been fired for using Symfony 3.'); $comment3->setApproved(true); $comment3->setCreated(new \DateTime()); $comment3->setCommentType('Blog'); $comment3->setBlogPost($manager->merge($this->getReference('blog1'))); $manager->persist($comment3); $manager->flush(); }
protected function createComment($commentType, $associated_object, $associated_object_id) { // Create new comment object and associate with the desired object $comment = new Comment(); if ($commentType == 'Blog') { $comment->setBlogPost($associated_object); $comment->setCommentType('Blog'); $comment->setApproved(false); } else { throw $this->createNotFoundException('Commenting is not available.'); } // get the request and check if it was ajax based $request = $this->getRequest(); $ajaxForm = $request->get('isAjax'); if (!isset($ajaxForm) || !$ajaxForm) { $ajaxForm = false; } // Bind the request to the comment form $form = $this->createForm(new CommentType(), $comment); $form->handleRequest($request); //Prepare the responce data $errorList = array(); $successMsg = ''; $formhasErrors = true; if ($form->isValid()) { // Persist (save) the form data to the database $em = $this->getDoctrine()->getManager(); $em->persist($comment); $em->flush(); // The responce for the user upon successful submission $successMsg = 'Thank you submitting your comment.'; $formMessage = $successMsg; $formhasErrors = false; } else { // Validate the data and get errors $errorList = $this->getFormErrorMessages($form); $formMessage = 'There was an error submitting your comment. Please try again.'; } // Return the responce to the user if ($ajaxForm) { $ajaxFormData = array('errors' => $errorList, 'formMessage' => $formMessage, 'hasErrors' => $formhasErrors); // Return the responce in json format $ajaxFormResponce = new Response(json_encode($ajaxFormData)); $ajaxFormResponce->headers->set('Content-Type', 'application/json'); return $ajaxFormResponce; } else { if ($commentType == 'Blog') { // Clear the form data object if it was submited successfully if (!$formhasErrors) { $comment = new Comment(); } // Retrieving the comments the view $postComments = $this->getBlogPostComments($associated_object_id); // get the blog post details (similar to the blog bundle) $settings = $this->get('bardiscms_settings.load_settings')->loadSettings(); $page = $this->setBlogSettings($settings, $associated_object); // Return the responce as the blog post with form data return $this->render('BlogBundle:Default:page.html.twig', array('page' => $page, 'form' => $form->createView(), 'comments' => $postComments, 'comment' => $comment, 'ajaxform' => $ajaxForm, 'formMessage' => $formMessage)); } else { throw $this->createNotFoundException('Commenting is not available.'); } } }