首頁 > 後端開發 > php教程 > zendframework2資料庫閘道及分頁簡化

zendframework2資料庫閘道及分頁簡化

PHP中文网
發布: 2023-03-13 19:30:02
原創
1762 人瀏覽過

鑑於zendframework2在國內的教學比較少,本人有感而發,寫下這篇關於zf2框架的技術文章,希望能幫助到需要的人。

一、config\autoload\global.php

<?php
//php中文网 m.sbmmt.com
return array(
    &#39;db&#39; => array(
            &#39;driver&#39; => &#39;pdo&#39;,
            &#39;driver_options&#39; => array(
                \PDO::MYSQL_ATTR_INIT_COMMAND => &#39;SET NAMES \&#39;UTF8\&#39;&#39;
            )
    )
);
登入後複製

二、config\autoload\local.php

<?php
//php中文网 m.sbmmt.com
return array(
    &#39;db&#39; => array(
        &#39;dsn&#39; => &#39;mysql:dbname=testdb;hostname=localhost&#39;,
        &#39;username&#39; => &#39;root&#39;,
        &#39;password&#39; => &#39;root&#39;,
        &#39;prefix&#39; => &#39;phpcn_&#39;
    ),
);
登入後複製

三、module \Application\Module.php

public function getServiceConfig()
{
    return array(
                  &#39;factories&#39;=>array(
                            &#39;Application\Model\Db&#39;=>function($sm){
                                                        $service=new Model\Db($sm);
                                                        return $service;
                            },
                            &#39;Db\Adapter&#39;=>function($sm){
                                                        $configs=$sm->get(&#39;Config&#39;);
                                                        $adapter = new \Zend\Db\Adapter\Adapter($configs[&#39;db&#39;]);
                                                        return $adapter;
                            },
                            &#39;Db\Feature&#39;=>function($sm){
                                                        $configs=$sm->get(&#39;Config&#39;);
                                                        $Feature=new \Application\Model\TableNamePrefixFeature($configs[&#39;db&#39;][&#39;prefix&#39;]);
                                                        return $Feature;
                            },
                            &#39;Db\Padapter&#39;=>function($sm){
                                                        $Padapter=new \Zend\Paginator\Adapter\DbSelect($sm->get(&#39;Application\Model\Db&#39;)->select,$sm->get(&#39;Db\Adapter&#39;));
                                                        return $Padapter;
                            },
                            &#39;Db\Paginator&#39;=>function($sm){
                                                        $Paginator=new \Zend\Paginator\Paginator($sm->get(&#39;Db\Padapter&#39;));
                                                        return $Paginator;
                            },
                ),
                  &#39;abstract_factories&#39;=>array(&#39;Application\Services\CommonDbAbstractFactory&#39;)
    );
}
登入後複製

四、module\Application\view\pagination\tmpl.phtml

<?php if ($this->pageCount): ?>
    <p class="pageX">
        <?php if (isset($this->previous)): ?>
            <a href="<?php echo $this->url().&#39;?p=&#39;.$this->previous;?>">< 前一页</a> |
        <?php else: ?>
            <span>< 前一页</span> |
        <?php endif; ?>
        <?php foreach ($this->pagesInRange as $page): ?>
            <?php if ($page != $this->current): ?>
                <a href="<?php echo $this->url().&#39;?p=&#39;.$page;?>"><?php echo $page;?></a> |
            <?php else: ?>
                <?php echo $page; ?> |
            <?php endif; ?>
        <?php endforeach; ?>
        <?php if (isset($this->next)): ?>
            <a href="<?php echo $this->url().&#39;?p=&#39;.$this->next;?>">下一页 ></a>
        <?php else: ?>
            <span>下一页 ></span>
        <?php endif; ?>
    </p>
<?php endif; ?>
登入後複製

五、module\Application\src\Application\Services \CommonDbAbstractFactory.php

<?php
//php中文网 m.sbmmt.com
namespace Application\Services;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class CommonDbAbstractFactory implements AbstractFactoryInterface
{
    public function canCreateServiceWithName(ServiceLocatorInterface $locator, $name, $requestedName)
    {
        $a=explode(&#39;:&#39;,$name);
        if(!empty($a[0]) && $a[0]==&#39;db&#39;){
            return true;
        }
        return false;
    }
     
    public function createServiceWithName(ServiceLocatorInterface $locator, $name, $requestedName)
    {
        $a=explode(&#39;:&#39;,$name);
        return $locator->get(&#39;Application\Model\Db&#39;)->get($a[1]);
    }
     
}
登入後複製

六、module\Application\src\Application\Model\Db.php

<?php
//php中文网 m.sbmmt.com
namespace Application\Model;
use Zend\Db\TableGateway\TableGateway;
use Zend\ServiceManager\ServiceManager;
class Db
{
    public function construct(ServiceManager $sm){
        $this->sm=$sm;
    }
    public function get($tablename=null)
    {
        $configs=$this->sm->get(&#39;Config&#39;);
        $adapter=$this->sm->get(&#39;Db\Adapter&#39;);
        $dbFeature=$this->sm->get(&#39;Db\Feature&#39;);
        $this->db=new TableGateway($tablename,$adapter,$dbFeature);
        $this->select=$this->db->getSql()->select();
        return $this;
    }
    public function fetch(){
        return $this->db->selectWith($this->select);
    }
    public function getSql(){
        return $this->select;
    }
    public function getTableGateway(){
        return $this->db;
    }
    public function select($where = null){
        return $this->db->select($where);
    }
    public function insert($set){
        return $this->db->insert($set);
    }
    public function update($set, $where = null){
        return $this->db->update($set,$where);
    }
    public function delete($where){
        return $this->db->delete($where);
    }
    public function call($functionName,$args){
        $this->select=call_user_func_array(array($this->select,$functionName),$args);
        return $this;
    }
}
登入後複製

七、module\Application\src\Application\Model \TableNamePrefixFeature.php

<?php
//php中文网 m.sbmmt.com
namespace Application\Model;
use Zend\Db\TableGateway\Feature\AbstractFeature;
class TableNamePrefixFeature extends AbstractFeature
{
    protected $prefix=null;
    public function construct($prefix=null)
    {
        if(null!==$prefix) {
        $this->setPrefix($prefix);
        }
    }
    public function setPrefix($prefix)
    {
        $this->prefix=$prefix;
    }
    public function postInitialize()
    {
        if(null!==$this->prefix){
            $this->tableGateway->getSql()->setTable($this->prefix.$this->tableGateway->table);
        }
    }
}
登入後複製

八、module\Application\src\Application\Controller\IndexController.php

$this->sm=$this->getServiceLocator();
$this->model=$this->sm->get(&#39;db:model&#39;);
$p=intval($this->getRequest()->getQuery(&#39;p&#39;,1));
$per_page=1;
$result=$this->model->where(&#39;id > 2&#39;)->order(&#39;id DESC&#39;)->limit($per_page)->offset(($p-1)*$per_page)->fetch()->toArray();
$paginator=$this->sm->get(&#39;Db\Paginator&#39;);
$paginator->setCurrentPageNumber($p);
$paginator->setItemCountPerPage($per_page);
\Zend\Debug\Debug::dump($result);
echo $this->sm->get(&#39;ViewRenderer&#39;)->paginationcontrol($paginator, &#39;Sliding&#39;, &#39;pagination/tmpl.phtml&#39;);
登入後複製

【本文由「Ty80帳號」發布,2017年7月13日】

以上是zendframework2資料庫閘道及分頁簡化的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板