ZendFramework2连接数据库操作实例

本文实例讲述了ZendFramework2连接数据库操作。分享给大家供大家参考,具体如下:

相对于zf1,来说,zf2让我们对于数据库这方面的操作我的个人感觉是对于字段起别名简单了,但是对数据库的操作虽然配置写好的就基本不需要动了,但是还是比1的配置要繁琐,

还是那句话,大家可以去看看源码。。。

Module.PHP 里面添加

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('cc_user',$dbAdapter,null,$resultSetPrototype);//table Name is cc_user },),); }

student.PHP 这个是Model/Student.PHP

id = (!empty($data['cc_u_id'])) ? $data['cc_u_id'] : null; $this->name = (!empty($data['cc_u_name'])) ? $data['cc_u_name'] : null; $this->phone = (!empty($data['cc_u_phone'])) ? $data['cc_u_phone'] : null; $this->mark = (!empty($data['cc_u_mark'])) ? $data['cc_u_mark'] : null; $this->email = (!empty($data['cc_u_email'])) ? $data['cc_u_email'] : null; } }

StudentTable.PHP Model/StudentTable.PHP

rush:PHP;"> tableGateway = $tableGateway; } public function fetchAll($paginated) {//分页 if($paginated) { // create a new Select object for the table album $select = new Select('cc_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 调用数据库

$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()); }

在模板页面调用

rush:PHP;"> paginator as $student) : ?> escapeHtml($student->id);?>escapeHtml($student->name);?>escapeHtml($student->phone);?>escapeHtml($student->email);?>PHP的别名 escapeHtml($student->mark);?>

更多关于zend相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》及《PHP常见数据库操作技巧汇总》

希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


服务器优化必备:深入了解PHP8底层开发原理
Golang的网络编程:如何快速构建高性能的网络应用?
Golang和其他编程语言的对比:为什么它的开发效率更高?
PHP8底层开发原理揭秘:如何利用新特性创建出色的Web应用
将字符重新排列以形成回文(如果可能)在C++中
掌握PHP8底层开发原理和新特性:创建高效可扩展的应用程序
服务器性能优化必学:掌握PHP8底层开发原理
PHP8新特性和底层开发原理详解:优化应用性能的终极指南
将 C/C++ 代码转换为汇编语言
深入研究PHP8底层开发原理:创建高效可扩展的应用程序
C++程序查找法向量和迹
PHP8底层开发原理实战指南:提升服务器效能
重排数组,使得当 i 为偶数时,arr[i] >= arr[j],当 i 为奇数时,arr[i] <= arr[j],其中 j < i,使用 C++ 语言实现
Golang的垃圾回收:为什么它可以减少开发人员的负担?
C++程序:将一个数组的所有元素复制到另一个数组中
Golang:构建智能系统的基石
为什么AI开发者应该关注Golang?
在C和C++中,逗号(comma)的用法是用来分隔表达式或语句
PHP8底层开发原理解析及新特性应用实例
利用PHP8底层开发原理解析新特性:如何构建出色的Web应用