博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
zendframework 2 链接数据库
阅读量:7173 次
发布时间:2019-06-29

本文共 5247 字,大约阅读时间需要 17 分钟。

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

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

1  Module.php 里面添加 public function getServiceConfig() 2     { 3         return array( 4             'factories' => array( 5                 'Student\Model\StudentTable' =>  function($sm) { 6                     $tableGateway = $sm->get('StudentTableGateway'); 7                     $table = new StudentTable($tableGateway); 8                     return $table; 9                 },10                 'StudentTableGateway' => function ($sm) {11                     $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');12                     $resultSetPrototype = new ResultSet();13                     $resultSetPrototype->setArrayObjectPrototype(new Student());14                     return new TableGateway('cc_user', $dbAdapter, null, $resultSetPrototype);//table Name is cc_user15                 },16             ),17         );18     }
1 namespace Student\Model; 2  3 class Student 4 { 5     public $id; 6     public $name; 7     public $phone; 8     public $mark; 9     public $email;10 11     public function exchangeArray($data)//别名12     {13         $this->id     = (!empty($data['cc_u_id'])) ? $data['cc_u_id'] : null;14         $this->name = (!empty($data['cc_u_name'])) ? $data['cc_u_name'] : null;15         $this->phone  = (!empty($data['cc_u_phone'])) ? $data['cc_u_phone'] : null;16         $this->mark  = (!empty($data['cc_u_mark'])) ? $data['cc_u_mark'] : null;17         $this->email  = (!empty($data['cc_u_email'])) ? $data['cc_u_email'] : null;18     }19 }
student.php 这个是Model/Student.php
 
1 
tableGateway = $tableGateway;18 }19 20 public function fetchAll($paginated)21 {
//分页22 if($paginated) {23 // create a new Select object for the table album24 $select = new Select('cc_user');25 // create a new result set based on the Student entity26 $resultSetPrototype = new ResultSet();27 $resultSetPrototype->setArrayObjectPrototype(new Student());28 // create a new pagination adapter object29 $paginatorAdapter = new DbSelect(30 // our configured select object31 $select,32 // the adapter to run it against33 $this->tableGateway->getAdapter(),34 // the result set to hydrate35 $resultSetPrototype36 );37 $paginator = new Paginator($paginatorAdapter);38 return $paginator;39 }40 $resultSet = $this->tableGateway->select();41 return $resultSet;42 }43 44 public function getStudent($id)45 {46 $id = (int) $id;47 $rowset = $this->tableGateway->select(array('id' => $id));48 $row = $rowset->current();49 if (!$row) {50 throw new \Exception("Could not find row $id");51 }52 return $row;53 }54 55 56 public function deleteStudent($id)57 {58 $this->tableGateway->delete(array('id' => $id));59 }60 61 public function getLIValue(){62 return $this->tableGateway->getLastInsertValue();63 }64 65 66 }
StudentTable.php Model/StudentTable.php
 
1    Student/IndexController.php 调用数据库 public function indexAction(){ 2         /* return new ViewModel(array( 3             'students' => $this->getStudentTable()->fetchAll(), //不分页 4         ));*/ 5         $page=$this->params('page');//走分页  在model.config.php里面设置
1       model.config.php       'defaults' => array(2                         'controller' => 'Student\Controller\Index',3                         'action'     => 'index',4                           'page'=>'1',5                     ),
 

 

6         $paginator = $this->getStudentTable()->fetchAll(true); 7         // set the current page to what has been passed in query string, or to 1 if none set 8         $paginator->setCurrentPageNumber((int)$this->params()->fromQuery('page', $page)); 9         // set the number of items per page to 1010         $paginator->setItemCountPerPage(10);11 12         return new ViewModel(array(13             'paginator' => $paginator //模板页面调用的时候的名字14         ));15     //print_r($this->getStudentTable()->fetchAll());16         17     }

 

1在模板页面的调用   
paginator as $student) : ?> 2 3
escapeHtml($student->id);?> 4
escapeHtml($student->name);?> 5
escapeHtml($student->phone);?> 6
escapeHtml($student->email);?>//应用了在Student.php的别名 7
escapeHtml($student->mark);?> 8    9   10 11 12 13

 

 

转载于:https://www.cnblogs.com/zhangjun516/p/3385706.html

你可能感兴趣的文章
电信网络拓扑图自动布局之总线
查看>>
数据库启动时报ORA-00845错误解决方法
查看>>
查询阿里云存储文件并导出excle 保存到本地
查看>>
WebService-—调用第三方提供的webService服务
查看>>
LVM报错:resize2fs: Bad magic number in super-block
查看>>
从开发到部署会用到的 Docker 命令
查看>>
access数据库转mysql数据库
查看>>
CISCO服务器配置RAID步骤
查看>>
利用makefile文件编译c++源文件
查看>>
VS 0xC0000005 运行错误分析
查看>>
ASP.NET中TextBox控件设置ReadOnly="true"后台取不到值
查看>>
找出Java进程ID pid的N种方法
查看>>
SSH和SFTP简介
查看>>
借助JRebel使Tomcat支持热部署
查看>>
基于Mozilla Thunderbird的扩展开发(八)---进程间通信之Socket篇(续)
查看>>
让eclipse像idea一样炫起来
查看>>
函数上下文 this 判断技巧。
查看>>
Flutter如何实现网易云音乐tabbar嵌套呢
查看>>
Flutter入门进阶之旅(一)-初识Flutter
查看>>
HTTP 入门
查看>>