PDO操作数据库

  1. 配置数据库

  2. 创建Bundle

     php bin/console generate:bundle
     Are you planning on this app?[no]:回车
     Bundle name [UserBundle]:BookBundle
     Target directory [src/]:回车
     Configuration format (annotation,yml,xml,php)[annotation]: annotation
    
  3. BookBundle/Controller/BookController.php

     <?php
     namespace BookBundle\Controller;
     use Symfony\Bundle\FrameworkBundle\Controller\Controller;
     use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
     use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
     use Symfony\Component\HttpFoundation\Request;
     use Symfony\Component\HttpFoundation\Response;
    
     class BookController extends Controller
     {
        /**
         * @Route("/index/{BookId}", name="Book",requirements={"BookId":"\d+"})
         * @Method({"GET","HEAD"}) * @return Response
         */
         public function indexAction($BookId)
         {
              $conn = $this->get('database_connection');
              $ArticleInfo = $conn->fetchAll(
                 'SELECT articlename FROM jieqi_article_article where articleid='.$BookId
              );
              $response = new Response();
              $response->setContent(json_encode($ArticleInfo));
              $response->headers->set('Content-Type', 'application/json');
              return $response;
         }
         /**
          * @Route("/chapters/{BookId}", name="chapters",requirements={"BookId":"\d+"})
          * @Method({"GET","HEAD"})
          * @return Response
          */
          public function getChapters($BookId)
          {
              $conn = $this->get('database_connection');
              $ArticleInfo = $conn->fetchAll(
                 'SELECT chapterorder,chaptername FROM jieqi_article_chapter where articleid='.$BookId.' order by chapterorder ASC'
             );
              $response = new Response();
              $response->setContent(json_encode($ArticleInfo));
              $response->headers->set('Content-Type', 'application/json');
              return $response;
          }
     }
    
  4. 注意

     # 大多数情况下'database_connection'服务是'doctrine.dbal.default_connection'的别名
     # 可以通过服务名'doctrine.dbal.[连接名]_connection'来进行指定数据库连接的PDO数据库操作
     # 服务名'doctrine.dbal.[连接名]_connection'获得方法如下:
     1. 执行php bin/console debug
     2. 提示:
         Command "debug" is not defined.                                 
         Did you mean one of these?                                      
             debug:event-dispatcher                                      
             debug:translation                                           
             debug:swiftmailer                                           
             debug:container                                             
             debug:config                                                
             debug:router                                                
             debug:twig                                                  
             swiftmailer:debug
     3. 执行php bin/console debug:container|grep 'doctrine.dbal'列出所有PDO服务
         database_connection                alias for"doctrine.dbal.default_connection"
         doctrine.dbal.connection_factory   Doctrine\Bundle\DoctrineBundle\ConnectionFactory
         doctrine.dbal.default_connection   Doctrine\DBAL\Connection
         doctrine.dbal.other_connection     Doctrine\DBAL\Connection
     4. 'doctrine.dbal.default_connection'和'doctrine.dbal.other_connection'即为我们需要的所有可进行PDO操作的服务名
    
@耿志环 2012-∞ 冀ICP备17033181号, powered by Gitbook修订: 2019-07-08 16:49:59

results matching ""

    No results matching ""