高效Web开发的10个jQuery代码片段
411
2024-02-24
本文实例讲述了ZendFramework2连接数据库操作。分享给大家供大家参考,具体如下:
相对于zf1,来说,zf2让我们对于数据库这方面的操作我的个人感觉是对于字段起别名简单了,但是对数据库的操作虽然配置写好的就基本不需要动了,但是还是比1的配置要繁琐,
还是那句话,大家可以去看看源码。。。
Module.php 里面添加
public function getServiceConfig() { return array( 'factories' => array( 'StudentModelStudentTable' => function($sm) { $tableGateway = $sm->get('StudentTableGateway'); $table = new StudentTable($tableGateway); return $table; }, 'StudentTableGateway' => function ($sm) { $dbAdapter = $sm->get('ZendDbAdapterAdapter'); $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
namespace StudentModel; class Student { public $id; public $name; public $phone; public $mark; public $email; public function exchangeArray($data)//别名 { $this->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
<?php namespace StudentModel; use ZendDbResultSetResultSet; use ZendDbTableGatewayTableGateway; use ZendDbSqlSelect; use ZendPaginatorAdapterDbSelect; use ZendPaginatorPaginator; class StudentTable { protected $tableGateway; protected $table='cc_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('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 调用数据库
public function indexAction(){ /* return new ViewModel(array( 'students' => $this->getStudentTable()->fetchAll(), //不分页 ));*/ $page=$this->params('page');//走分页 在model.config.php里面设置: /* model.config.php 'defaults' => array( 'controller' => 'StudentControllerIndex', '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='http://www.gimoo.net/t/1804/5adc4f11cf175.html' class='icol-bandaid editUserInfo'></a> <a href='http://www.gimoo.net/t/1804/5adc4f11cf175.html' class='icol-key changePwd'></a> <a herf='#' class='icol-cross deleteStud'></a> </td> </tr> <?php endforeach;?>
更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。
#免责声明#
本站[绿夏技术导航]提供的一切软件、教程和内容信息仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。如果您喜欢该程序或内容,请支持正版,购买注册,得到更好的正版服务。我们非常重视版权问题,如有侵权请邮件[admin@lxwl520.com]与我们联系进行删除处理。敬请谅解!