zendframework2資料庫網關及分頁簡化

來源:互聯網
上載者:User
鑒於zendframework2在國內的教程比較少,本人有感而發,寫下此篇關於zf2架構的技術文章,希望能協助到需要的人。

一、config\autoload\global.php

<?php//php中文網 www.php.cnreturn array(    'db' => array(            'driver' => 'pdo',            'driver_options' => array(                \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''            )    ));

二、config\autoload\local.php

<?php//php中文網 www.php.cnreturn array(    'db' => array(        'dsn' => 'mysql:dbname=testdb;hostname=localhost',        'username' => 'root',        'password' => 'root',        'prefix' => 'phpcn_'    ),);

三、module\Application\Module.php

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

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

<?php if ($this->pageCount): ?>    <p class="pageX">        <?php if (isset($this->previous)): ?>            <a href="<?php echo $this->url().'?p='.$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().'?p='.$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().'?p='.$this->next;?>">下一頁 ></a>        <?php else: ?>            <span>下一頁 ></span>        <?php endif; ?>    </p><?php endif; ?>

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

<?php//php中文網 www.php.cnnamespace Application\Services;use Zend\ServiceManager\AbstractFactoryInterface;use Zend\ServiceManager\ServiceLocatorInterface;class CommonDbAbstractFactory implements AbstractFactoryInterface{    public function canCreateServiceWithName(ServiceLocatorInterface $locator, $name, $requestedName)    {        $a=explode(':',$name);        if(!empty($a[0]) && $a[0]=='db'){            return true;        }        return false;    }         public function createServiceWithName(ServiceLocatorInterface $locator, $name, $requestedName)    {        $a=explode(':',$name);        return $locator->get('Application\Model\Db')->get($a[1]);    }     }

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

<?php//php中文網 www.php.cnnamespace 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('Config');        $adapter=$this->sm->get('Db\Adapter');        $dbFeature=$this->sm->get('Db\Feature');        $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中文網 www.php.cnnamespace 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('db:model');$p=intval($this->getRequest()->getQuery('p',1));$per_page=1;$result=$this->model->where('id > 2')->order('id DESC')->limit($per_page)->offset(($p-1)*$per_page)->fetch()->toArray();$paginator=$this->sm->get('Db\Paginator');$paginator->setCurrentPageNumber($p);$paginator->setItemCountPerPage($per_page);\Zend\Debug\Debug::dump($result);echo $this->sm->get('ViewRenderer')->paginationcontrol($paginator, 'Sliding', 'pagination/tmpl.phtml');

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.