Symfony2框架下一个简单Ajax留言板-PHP源码
getDoctrine()->getEntityManager(); $query = $em->createQuery( 'SELECT m FROM DpDriveplusBundle:Messageboard m ORDER BY m.id DESC' ); $comments = $query->getResult(); $count = count($comments); return $this->render('DpDriveplusBundle:Message:index.html.twig', array('comments'=>$comments, 'count'=>$count)); } public function ajaxAction() { $request = $this->getRequest(); $ret = ''; if ('POST' === $request->getMethod()) { $name = $request->get('name'); $message = $request->get('message'); $ret = '姓名:'.$name.'
留言内容:'.$message.'
'; $comment = new Messageboard(); $comment->setName($name); $comment->setMessage($message); $em = $this->getDoctrine()->getEntityManager(); $em->persist($comment); $em->flush(); return new Response($ret); } } }
PHP之友评论