一起源码网

  • www.171739.xyz
  • 全球最大的互联网技术和资源下载平台
搜索
猜你喜欢
查看: 6444|回复: 1
打印 上一主题 下一主题

zendframework2数据库网关及分页简化

[复制链接]

0

主题

0

帖子

1万

积分

钻石会员

Rank: 8Rank: 8

积分
17424
QQ
跳转到指定楼层
楼主
发表于 2020-3-23 06:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
鉴于zendframework2在国内的教程比较少,本人有感而发,写下此篇关于zf2框架的技术文章,希望能帮助到需要的人。

一、configautoloadglobal.php

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

二、configautoloadlocal.php

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

三、ApplicationModule.php

public function getServiceConfig()
{
    return array(
                  'factories'=>array(
                            'ApplicationModelDb'=>function($sm){
                                                        $service=new ModelDb($sm);
                                                        return $service;
                            },
                            'DbAdapter'=>function($sm){
                                                        $configs=$sm->get('Config');
                                                        $adapter = new endDbAdapterAdapter($configs['db']);
                                                        return $adapter;
                            },
                            'DbFeature'=>function($sm){
                                                        $configs=$sm->get('Config');
                                                        $Feature=new ApplicationModelTableNamePrefixFeature($configs['db']['prefix']);
                                                        return $Feature;
                            },
                            'DbPadapter'=>function($sm){
                                                        $Padapter=new endPaginatorAdapterDbSelect($sm->get('ApplicationModelDb')->select,$sm->get('DbAdapter'));
                                                        return $Padapter;
                            },
                            'DbPaginator'=>function($sm){
                                                        $Paginator=new endPaginatorPaginator($sm->get('DbPadapter'));
                                                        return $Paginator;
                            },
                ),
                  'abstract_factories'=>array('ApplicationServicesCommonDbAbstractFactory')
    );
}

四、moduleApplicationviewpagination mpl.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; ?>

五、moduleApplicationsrcApplicationServicesCommonDbAbstractFactory.php

<?php
//php中文网 www.php.cn
namespace ApplicationServices;
use ZendServiceManagerAbstractFactoryInterface;
use ZendServiceManagerServiceLocatorInterface;
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('ApplicationModelDb')->get($a[1]);
    }
     
}

六、moduleApplicationsrcApplicationModelDb.php

<?php
//php中文网 www.php.cn
namespace ApplicationModel;
use ZendDbTableGatewayTableGateway;
use ZendServiceManagerServiceManager;
class Db
{
    public function construct(ServiceManager $sm){
        $this->sm=$sm;
    }
    public function get($tablename=null)
    {
        $configs=$this->sm->get('Config');
        $adapter=$this->sm->get('DbAdapter');
        $dbFeature=$this->sm->get('DbFeature');
        $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;
    }
}

七、moduleApplicationsrcApplicationModelTableNamePrefixFeature.php

<?php
//php中文网 www.php.cn
namespace ApplicationModel;
use ZendDbTableGatewayFeatureAbstractFeature;
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);
        }
    }
}

八、moduleApplicationsrcApplicationControllerIndexController.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('DbPaginator');
$paginator->setCurrentPageNumber($p);
$paginator->setItemCountPerPage($per_page);
endDebugDebug::dump($result);
echo $this->sm->get('ViewRenderer')->paginationcontrol($paginator, 'Sliding', 'pagination/tmpl.phtml');

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

分享到:  QQ好友和群QQ好友和群
收藏收藏
回复

使用道具 举报

0

主题

14

帖子

44

积分

等待验证会员

积分
44
沙发
发表于 2022-9-10 22:05 来自手机 | 只看该作者
源代码电影免费观看
回复

使用道具 举报

一起源码让程序更轻更快

www.171739.xyz

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表