鉴于zendframework2在国内的教程比较少,本人有感而发,写下此篇关于zf2框架的技术文章,希望能帮助到需要的人。
一、config\autoload\global.php
<?php //php中文网 www.php.cn return array( 'db' => array( 'driver' => 'pdo', 'driver_options' => array( \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'' ) ) );
二、config\autoload\local.php
<?php //php中文网 www.php.cn return 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.cn namespace 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.cn 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('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.cn 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('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日】
以上是zendframework2数据库网关及分页简化的详细内容。更多信息请关注PHP中文网其他相关文章!

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于索引优化器工作原理的相关内容,其中包括了MySQL Server的组成,MySQL优化器选择索引额原理以及SQL成本分析,最后通过 select 查询总结整个查询过程,下面一起来看一下,希望对大家有帮助。

sybase是基于客户/服务器体系结构的数据库,是一个开放的、高性能的、可编程的数据库,可使用事件驱动的触发器、多线索化等来提高性能。

visual foxpro数据库文件是管理数据库对象的系统文件。在VFP中,用户数据是存放在“.DBF”表文件中;VFP的数据库文件(“.DBC”)中不存放用户数据,它只起将属于某一数据库的 数据库表与视图、连接、存储过程等关联起来的作用。

数据库系统由4个部分构成:1、数据库,是指长期存储在计算机内的,有组织,可共享的数据的集合;2、硬件,是指构成计算机系统的各种物理设备,包括存储所需的外部设备;3、软件,包括操作系统、数据库管理系统及应用程序;4、人员,包括系统分析员和数据库设计人员、应用程序员(负责编写使用数据库的应用程序)、最终用户(利用接口或查询语言访问数据库)、数据库管理员(负责数据库的总体信息控制)。

microsoft sql server是Microsoft公司推出的关系型数据库管理系统,是一个全面的数据库平台,使用集成的商业智能(BI)工具提供了企业级的数据管理,具有使用方便可伸缩性好与相关软件集成程度高等优点。SQL Server数据库引擎为关系型数据和结构化数据提供了更安全可靠的存储功能,使用户可以构建和管理用于业务的高可用和高性能的数据应用程序。

结构层次是“数据库→数据表→记录→字段”;字段构成记录,记录构成数据表,数据表构成了数据库。数据库是一个完整的数据的记录的整体,一个数据库包含0到N个表,一个表包含0到N个字段,记录是表中的行。

go语言可以写数据库。Go语言和其他语言不同的地方是,Go官方没有提供数据库驱动,而是编写了开发数据库驱动的标准接口,开发者可以根据定义的接口来开发相应的数据库驱动;这样做的好处在于,只要是按照标准接口开发的代码,以后迁移数据库时,不需要做任何修改,极大方便了后期的架构调整。

mysql查询为什么会慢,关于这个问题,在实际开发经常会遇到,而面试中,也是个高频题。遇到这种问题,我们一般也会想到是因为索引。那除开索引之外,还有哪些因素会导致数据库查询变慢呢?


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。