yyy123456
V2EX  ›  问与答

doctrine 分页组件 pagerfanta 单独使用完全攻略

  •  
  •   yyy123456 · Jul 6, 2017 · 2367 views
    This topic created in 3260 days ago, the information mentioned may be changed or developed.

    本文目的是 php 分页显示数据。

    为了独立使用 composer,不依赖框架。

    假设当前使用的 db 类库是 doctrine,则分页该怎么用?

    本代码完全脱离 symfony 环境。只加载对应的 db 类库,故意不使用模板,让代码含义更加清晰。

    composer

    {
        "require": {
            "doctrine/dbal":"2.5.12",
            "pagerfanta/pagerfanta":"1.0.5"
        }
    }
    

    建表

    CREATE TABLE `test_databases` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `db_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '库名',
      `user_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '测试用户 id',
      `created_at` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
      `updated_at` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '修改时间',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB
    

    请自行插入一百条数据。

    假设本机项目域名 www.t3.com

    本代码网址 http://www.t3.com/paginator/doctrine

    首页只需输入上面网址即可,点击分页链接,会自动加 page 查询参数。

    <?php
    namespace app\control;
    use Doctrine\DBAL\Query\QueryBuilder;
    use Doctrine\DBAL\Configuration;
    use Doctrine\DBAL\DriverManager;
    
    use Pagerfanta\Adapter\DoctrineDbalAdapter;
    use Pagerfanta\Pagerfanta;
    use Pagerfanta\View\DefaultView;
    
    class Paginator {
        public function doctrine( $req, $res, $args) {
            $config = new Configuration();
            //..
            $connectionParams = array(
            		'dbname' => 'test1',
            		'user' => 'root',
            		'password' => 'root',
            		'host' => '127.0.0.1',
            		'driver' => 'pdo_mysql',
                    'charset'=>'UTF8',
            );
            $conn = DriverManager::getConnection($connectionParams, $config);
            
            //构造查询语句。
            $queryBuilder = new QueryBuilder($conn);
            $queryBuilder->select('p.*')->where("p.id < 100")->from('test_databases', 'p')
               ->orderBy("p.id","asc");
            
            $countQueryBuilderModifier = function ($queryBuilder) {
                $queryBuilder->select('COUNT(*) AS total_results')
                      ->setMaxResults(1);
            };
            $adapter = new DoctrineDbalAdapter($queryBuilder, $countQueryBuilderModifier);
            $pagerfanta = new Pagerfanta($adapter);
            
            $page = intval( $_GET["page"]);
            if (!$page) {
                $page=1;
            }
            
            //设置当前页,最大页面。
            $pagerfanta->setMaxPerPage(4)->setCurrentPage($page);
            
            //打印当前页面的结果
            foreach ($pagerfanta->getCurrentPageResults() as $v ) {
                echo $v['db_name'] .' = ' . $v['user_id']."<br>";
            }
            
            //打印分页链接。
            $routeGenerator = function($page) { // 匿名函数解决链接字符串
                return '/paginator/doctrine?page='.$page;
            };
            $view = new DefaultView();
            $options = array('proximity' => 3); // 这个数字干嘛用?中间的链接个数=这个数字*2+1,这个数字一般取 3.
            $html = $view->render($pagerfanta, $routeGenerator, $options);
            echo $this->default_css();
            echo  "<div class='pagerfanta'>" .$html."</div>";
            return $res;
        }
        
        private function default_css()
        {
            $css=<<<css
            <style>
            .pagerfanta {
    }
    
    .pagerfanta a,
    .pagerfanta span {
        display: inline-block;
        border: 1px solid blue;
        color: blue;
        margin-right: .2em;
        padding: .25em .35em;
    }
    
    .pagerfanta a {
        text-decoration: none;
    }
    
    .pagerfanta a:hover {
        background: #ccf;
    }
    
    .pagerfanta .dots {
        border-width: 0;
    }
    
    .pagerfanta .current {
        background: #ccf;
        font-weight: bold;
    }
    
    .pagerfanta .disabled {
        border-color: #ccf;
        color: #ccf;
    }
    
    
    
    .pagerfanta a,
    .pagerfanta span {
        border-color: blue;
        color: blue;
    }
    
    .pagerfanta a:hover {
        background: #ccf;
    }
    
    .pagerfanta .current {
        background: #ccf;
    }
    
    .pagerfanta .disabled {
        border-color: #ccf;
        color: #cf;
    }
            </style>
    css;
            return $css;
        }
    }
    

    效果展示

    图片 图片 图片 图片

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1017 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 19:12 · PVG 03:12 · LAX 12:12 · JFK 15:12
    ♥ Do have faith in what you're doing.