鉴于zendframework2在国内的教程比较少,本人有感而发,写下此篇关于zf2框架的技术文章,希望能帮助到需要的人。
一、config\autoload\global.php
array(
'driver' => 'pdo',
'driver_options' => array(
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
)
)
);二、config\autoload\local.php
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
pageCount): ?>
previous)): ?>
< 前一页 |
< 前一页 |
pagesInRange as $page): ?>
current): ?>
|
|
next)): ?>
下一页 >
下一页 >
五、module\Application\src\Application\Services\CommonDbAbstractFactory.php
部分功能简介:商品收藏夹功能热门商品最新商品分级价格功能自选风格打印结算页面内部短信箱商品评论增加上一商品,下一商品功能增强商家提示功能友情链接用户在线统计用户来访统计用户来访信息用户积分功能广告设置用户组分类邮件系统后台实现更新用户数据系统图片设置模板管理CSS风格管理申诉内容过滤功能用户注册过滤特征字符IP库管理及来访限制及管理压缩,恢复,备份数据库功能上传文件管理商品类别管理商品添加/修改/
get('Application\Model\Db')->get($a[1]);
}
}六、module\Application\src\Application\Model\Db.php
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
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日】









