gongzhonghao.png
扫码关注我们
最新赞助活动温馨提示:自愿赞助服务器费用,学生和没有工作的整站资源免费下载!
头像

zendframework2数据库网关及分页简化

来源:http://erdangjiade.com/topic/1673.html 你好,世界。 2017-09-25 22:42浏览(31)

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

一、configautoloadglobal.php

<?php
//二当家的素材网 www.erdangjiade.com
return array(
    'db' => array(
            'driver' => 'pdo',
            'driver_options' => array(
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES 'UTF8''
            )
    )
);

二、configautoloadlocal.php

<?php
//二当家的素材网 www.erdangjiade.com
return array(
    'db' => array(
        'dsn' => 'mysql:dbname=testdb;hostname=localhost',
        'username' => 'root',
        'password' => 'root',
        'prefix' => 'phpcn_'
    ),
);

三、moduleApplicationModule.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
//二当家的素材网 www.erdangjiade.com
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
//二当家的素材网 www.erdangjiade.com
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
//二当家的素材网 www.erdangjiade.com
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日】

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

评论0
头像

友情提示:垃圾评论一律封号 加我微信:826096331拉你进VIP群学习群

1 2