ZendFramework2连接数据库操作实例
网络编程 2021-07-05 08:23www.168986.cn编程入门
这篇文章主要介绍了ZendFramework2连接数据库操作,结合完整实例形式分析了ZendFramework2连接数据库的具体步骤、配置方法、相关操作技巧与注意事项,需要的朋友可以参考下
本文实例讲述了ZendFramework2连接数据库操作。分享给大家供大家参考,具体如下
相对于zf1,来说,zf2让我们对于数据库这方面的操作我的个人感觉是对于字段起别名简单了,对数据库的操作虽然配置写好的就基本不需要动了,还是比1的配置要繁琐,
还是那句话,大家可以去看看源码。。。
Module.php 里面添加
public function getServiceConfig() { return array( 'factories' => array( 'Student\Model\StudentTable' => function($sm) { $tableGateway = $sm->get('StudentTableGateway'); $table = new StudentTable($tableGateway); return $table; }, 'StudentTableGateway' => function ($sm) { $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); $resultSetPrototype = new ResultSet(); $resultSetPrototype->setArrayObjectPrototype(new Student()); return new TableGateway('_user', $dbAdapter, null, $resultSetPrototype);//table Name is _user }, ), ); }
student.php 这个是Model/Student.php
namespace Student\Model; class Student { public $id; public $name; public $phone; public $mark; public $email; public function exchangeArray($data)//别名 { $this->id = (!empty($data['_u_id'])) ? $data['_u_id'] : null; $this->name = (!empty($data['_u_name'])) ? $data['_u_name'] : null; $this->phone = (!empty($data['_u_phone'])) ? $data['_u_phone'] : null; $this->mark = (!empty($data['_u_mark'])) ? $data['_u_mark'] : null; $this->email = (!empty($data['_u_email'])) ? $data['_u_email'] : null; } }
StudentTable.php Model/StudentTable.php
<?php namespace Student\Model; use Zend\Db\ResultSet\ResultSet; use Zend\Db\TableGateway\TableGateway; use Zend\Db\Sql\Select; use Zend\Paginator\Adapter\DbSelect; use Zend\Paginator\Paginator; class StudentTable { protected $tableGateway; protected $table='_user'; public function __construct(TableGateway $tableGateway) { $this->tableGateway = $tableGateway; } public function fetchAll($paginated) {//分页 if($paginated) { // create a new Select object for the table album $select = new Select('_user'); // create a new result set based on the Student entity $resultSetPrototype = new ResultSet(); $resultSetPrototype->setArrayObjectPrototype(new Student()); // create a new pagination adapter object $paginatorAdapter = new DbSelect( // our configured select object $select, // the adapter to run it against $this->tableGateway->getAdapter(), // the result set to hydrate $resultSetPrototype ); $paginator = new Paginator($paginatorAdapter); return $paginator; } $resultSet = $this->tableGateway->select(); return $resultSet; } public function getStudent($id) { $id = (int) $id; $rowset = $this->tableGateway->select(array('id' => $id)); $row = $rowset->current(); if (!$row) { throw new \Exception("Could not find row $id"); } return $row; } public function deleteStudent($id) { $this->tableGateway->delete(array('id' => $id)); } public function getLIValue(){ return $this->tableGateway->getLastInsertValue(); } }
Student/IndexController.php 调用数据库
public function indexAction(){ / return new ViewModel(array( 'students' => $this->getStudentTable()->fetchAll(), //不分页 ));/ $page=$this->params('page');//走分页 在model.config.php里面设置: / model.config.php 'defaults' => array( 'controller' => 'Student\Controller\Index', 'action' => 'index', 'page'=>'1', ), / $paginator = $this->getStudentTable()->fetchAll(true); // set the current page to what has been passed in query string, or to 1 if none set $paginator->setCurrentPageNumber((int)$this->params()->fromQuery('page', $page)); // set the number of items per page to 10 $paginator->setItemCountPerPage(10); return new ViewModel(array( 'paginator' => $paginator //模板页面调用的时候的名字 )); //print_r($this->getStudentTable()->fetchAll()); }
在模板页面的调用
<?php foreach ($this->paginator as $student) : ?> <tr id="<?php echo $this->escapeHtml($student->id);?>"> <td><?php echo $this->escapeHtml($student->id);?></td> <td><?php echo $this->escapeHtml($student->name);?></td> <td><?php echo $this->escapeHtml($student->phone);?></td> <td><?php echo $this->escapeHtml($student->email);?></td>//应用了在Student.php的别名 <td><?php echo $this->escapeHtml($student->mark);?></td> <td><a href='#' class='icol-bandaid editUserInfo'></a> <a href='#' class='icol-key changePwd'></a> <a herf='#' class='icol-cross deleteStud'></a> </td> </tr> <?php endforeach;?>
更多关于zend相关内容感兴趣的读者可查看本站专题《》、《》、《》、《》、《》、《》及《》
希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程