当前位置:Gxlcms > PHP教程 > zendframework2数据库网关及分页简化

zendframework2数据库网关及分页简化

时间:2021-07-01 10:21:17 帮助过:25人阅读

鉴于zendframework2在国内的教程比较少,本人有感而发,写下此篇关于zf2框架的技术文章,希望能帮助到需要的人。

一、config\autoload\global.php

  1. <?php
  2. //Gxl网 www.gxlcms.com
  3. return array(
  4. 'db' => array(
  5. 'driver' => 'pdo',
  6. 'driver_options' => array(
  7. \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
  8. )
  9. )
  10. );

二、config\autoload\local.php

  1. <?php
  2. //Gxl网 www.gxlcms.com
  3. return array(
  4. 'db' => array(
  5. 'dsn' => 'mysql:dbname=testdb;hostname=localhost',
  6. 'username' => 'root',
  7. 'password' => 'root',
  8. 'prefix' => 'phpcn_'
  9. ),
  10. );

三、module\Application\Module.php

  1. public function getServiceConfig()
  2. {
  3. return array(
  4. 'factories'=>array(
  5. 'Application\Model\Db'=>function($sm){
  6. $service=new Model\Db($sm);
  7. return $service;
  8. },
  9. 'Db\Adapter'=>function($sm){
  10. $configs=$sm->get('Config');
  11. $adapter = new \Zend\Db\Adapter\Adapter($configs['db']);
  12. return $adapter;
  13. },
  14. 'Db\Feature'=>function($sm){
  15. $configs=$sm->get('Config');
  16. $Feature=new \Application\Model\TableNamePrefixFeature($configs['db']['prefix']);
  17. return $Feature;
  18. },
  19. 'Db\Padapter'=>function($sm){
  20. $Padapter=new \Zend\Paginator\Adapter\DbSelect($sm->get('Application\Model\Db')->select,$sm->get('Db\Adapter'));
  21. return $Padapter;
  22. },
  23. 'Db\Paginator'=>function($sm){
  24. $Paginator=new \Zend\Paginator\Paginator($sm->get('Db\Padapter'));
  25. return $Paginator;
  26. },
  27. ),
  28. 'abstract_factories'=>array('Application\Services\CommonDbAbstractFactory')
  29. );
  30. }

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

  1. <?php if ($this->pageCount): ?>
  2. <p class="pageX">
  3. <?php if (isset($this->previous)): ?>
  4. <a href="<?php echo $this->url().'?p='.$this->previous;?>">< 前一页</a> |
  5. <?php else: ?>
  6. <span>< 前一页</span> |
  7. <?php endif; ?>
  8. <?php foreach ($this->pagesInRange as $page): ?>
  9. <?php if ($page != $this->current): ?>
  10. <a href="<?php echo $this->url().'?p='.$page;?>"><?php echo $page;?></a> |
  11. <?php else: ?>
  12. <?php echo $page; ?> |
  13. <?php endif; ?>
  14. <?php endforeach; ?>
  15. <?php if (isset($this->next)): ?>
  16. <a href="<?php echo $this->url().'?p='.$this->next;?>">下一页 ></a>
  17. <?php else: ?>
  18. <span>下一页 ></span>
  19. <?php endif; ?>
  20. </p>
  21. <?php endif; ?>

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

  1. <?php
  2. //Gxl网 www.gxlcms.com
  3. namespace Application\Services;
  4. use Zend\ServiceManager\AbstractFactoryInterface;
  5. use Zend\ServiceManager\ServiceLocatorInterface;
  6. class CommonDbAbstractFactory implements AbstractFactoryInterface
  7. {
  8. public function canCreateServiceWithName(ServiceLocatorInterface $locator, $name, $requestedName)
  9. {
  10. $a=explode(':',$name);
  11. if(!empty($a[0]) && $a[0]=='db'){
  12. return true;
  13. }
  14. return false;
  15. }
  16. public function createServiceWithName(ServiceLocatorInterface $locator, $name, $requestedName)
  17. {
  18. $a=explode(':',$name);
  19. return $locator->get('Application\Model\Db')->get($a[1]);
  20. }
  21. }

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

  1. <?php
  2. //Gxl网 www.gxlcms.com
  3. namespace Application\Model;
  4. use Zend\Db\TableGateway\TableGateway;
  5. use Zend\ServiceManager\ServiceManager;
  6. class Db
  7. {
  8. public function construct(ServiceManager $sm){
  9. $this->sm=$sm;
  10. }
  11. public function get($tablename=null)
  12. {
  13. $configs=$this->sm->get('Config');
  14. $adapter=$this->sm->get('Db\Adapter');
  15. $dbFeature=$this->sm->get('Db\Feature');
  16. $this->db=new TableGateway($tablename,$adapter,$dbFeature);
  17. $this->select=$this->db->getSql()->select();
  18. return $this;
  19. }
  20. public function fetch(){
  21. return $this->db->selectWith($this->select);
  22. }
  23. public function getSql(){
  24. return $this->select;
  25. }
  26. public function getTableGateway(){
  27. return $this->db;
  28. }
  29. public function select($where = null){
  30. return $this->db->select($where);
  31. }
  32. public function insert($set){
  33. return $this->db->insert($set);
  34. }
  35. public function update($set, $where = null){
  36. return $this->db->update($set,$where);
  37. }
  38. public function delete($where){
  39. return $this->db->delete($where);
  40. }
  41. public function call($functionName,$args){
  42. $this->select=call_user_func_array(array($this->select,$functionName),$args);
  43. return $this;
  44. }
  45. }

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

  1. <?php
  2. //Gxl网 www.gxlcms.com
  3. namespace Application\Model;
  4. use Zend\Db\TableGateway\Feature\AbstractFeature;
  5. class TableNamePrefixFeature extends AbstractFeature
  6. {
  7. protected $prefix=null;
  8. public function construct($prefix=null)
  9. {
  10. if(null!==$prefix) {
  11. $this->setPrefix($prefix);
  12. }
  13. }
  14. public function setPrefix($prefix)
  15. {
  16. $this->prefix=$prefix;
  17. }
  18. public function postInitialize()
  19. {
  20. if(null!==$this->prefix){
  21. $this->tableGateway->getSql()->setTable($this->prefix.$this->tableGateway->table);
  22. }
  23. }
  24. }

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

  1. $this->sm=$this->getServiceLocator();
  2. $this->model=$this->sm->get('db:model');
  3. $p=intval($this->getRequest()->getQuery('p',1));
  4. $per_page=1;
  5. $result=$this->model->where('id > 2')->order('id DESC')->limit($per_page)->offset(($p-1)*$per_page)->fetch()->toArray();
  6. $paginator=$this->sm->get('Db\Paginator');
  7. $paginator->setCurrentPageNumber($p);
  8. $paginator->setItemCountPerPage($per_page);
  9. \Zend\Debug\Debug::dump($result);
  10. echo $this->sm->get('ViewRenderer')->paginationcontrol($paginator, 'Sliding', 'pagination/tmpl.phtml');

【本文由“Ty80账号”发布,2017年7月13日】

以上就是zendframework2数据库网关及分页简化的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行