zendframework2가 중국에 상대적으로 적은 튜토리얼이 있다는 사실을 고려하여, 도움이 필요한 사람들에게 도움이 되기를 바라면서 zf2 프레임워크에 대한 이 기술 기사를 작성하게 된 영감을 받았습니다.
1.configautoloadglobal.php
<?php //php中文网 m.sbmmt.com return array( 'db' => array( 'driver' => 'pdo', 'driver_options' => array( \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'' ) ) );
2.configautoloadlocal.php
<?php //php中文网 m.sbmmt.com return array( 'db' => array( 'dsn' => 'mysql:dbname=testdb;hostname=localhost', 'username' => 'root', 'password' => 'root', 'prefix' => 'phpcn_' ), );
3. moduleApplicationModule.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') ); }
4. ApplicationrcApplicationServicesCommonDbAbstractFactory.php
<?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; ?>
six , moduleApplicationsrcApplicationModelDb.php
<?php //php中文网 m.sbmmt.com 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]); } }
seven, moduleApplicationsrcApplicationModelTableNamePrefixFeature.php
<?php //php中文网 m.sbmmt.com 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; } }
eight, moduleApplicationsrcApplicationControllerIndexController.php
<?php //php中文网 m.sbmmt.com 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); } } }
[이 문서는 "Ty80 계정"에 게시됨, 2017년 7월 13일 2일]
위 내용은 zendframework2 데이터베이스 게이트웨이 및 페이징 단순화의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!