<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 倉庫 Repository(代碼倉庫)是SF中的另一個重要概念。 顧名思義,代碼倉庫就是存放(通常是通用的)代碼的地方。在SF中,一般用來堆放進行數據庫操作的代碼。 SF為什么要增加這么一個額外的層?有兩個原因: 1. 雖然一個[實體](https://taylorr.gitbooks.io/building-a-web-site-with-symfony/content/03.05%20entity.html)提供了一些基本的數據庫操作,如`findOneBy`之類的,但是肯定不能滿足我們定制搜索的需要; 2. 如上的這個要求既不應該是Model的任務,更不應該是Controller的任務(不符合DRY和代碼重用原則)。 所以,我們有必要加入一個新的層,形成這樣的一個關系圖: ![](https://box.kancloud.cn/a289ec170b394584ac29b4ca03d74ccf_699x570.png) 1. Controller只提出我要什么數據; 2. Repository只負責選擇數據; 3. Model存放真正的數據。 現在要馬上透徹理解這里的關系和區別還是比較困難的。我們會在后續文章中更深入地討論這些。 我們來看一個典型的倉庫的代碼: ~~~ <?php namespace AppBundle\Repository; use Doctrine\ORM\EntityRepository; use Symfony\VarDumper; /** * StatusRepo * * This class was generated by the Doctrine ORM. Add your own custom * repository methods below. */ class StatusRepo extends EntityRepository { public function getStatusIn($start, $end, $count = 4) { $inter_s = new \DateInterval($start); $inter_e = new \DateInterval($end); $cts = new \DateTime(); $cts->sub($inter_s); $cte = new \DateTime(); $cte->sub($inter_e); $em = $this->getEntityManager(); $repo = $em->getRepository('AppBundle:Status'); $q = $repo->createQueryBuilder('s') ->leftJoin('AppBundle:User', 'u', 'with', 'u=s.author') ->where('s.created>=:e') ->setParameter('e', $cte) ->andWhere('s.created<=:s') ->setParameter('s', $cts) ->setMaxResults($count) ->orderBy('s.created', 'desc') ->addOrderBy('s.id', 'desc') ->getQuery() ; $status = $q->getResult(); return $status; } } ~~~ `getStatusIn`方法會提供在開始和結束期間的數據,缺省是提供4個。顯然,這個數據的提供(搜索)不是簡單地由`findBy`之類的實體方法可以完成的。 上面我們看到的構建SQL的方法是兩種可用方法中的一種,即通過鏈接各個函數調用,在查詢構造中中加入`join`、`where`、`order by`等限定而得到我們需要的一個`select`語句。 對其的調用在Controller中一般這樣進行: ~~~ public function indexAction() { $em = $this->getDoctrine()->getManager(); $repo = $em->getRepository('AppBundle:Status'); $day = $repo->getStatusIn('P0D', 'P1D', 5); $week = $repo->getStatusIn('P1D', 'P7D'); $month = $repo->getStatusIn('P7D', 'P1M', 5); ... ... ~~~ 這幾乎是一個標準的調用模式。 重要更新:如今的一種開發方式將我們從這樣的重度耦合中解放了出來。因此不再需要Repository,而直接改用遠程RESTful API的調用。詳細的討論可以參見[03.01 MVC](https://taylorr.gitbooks.io/building-a-web-site-with-symfony/content/03.01%20mvc.html)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看