相对于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('cc_user', $dbAdapter, null, $resultSetPrototype);//table Name is cc_user
},
),
);
}
 namespace Student\Model;

 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;
}
}

student.php 这个是Model/Student.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='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();
} }

StudentTable.php Model/StudentTable.php


    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'=>'',
),

         $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(); return new ViewModel(array(
'paginator' => $paginator //模板页面调用的时候的名字
));
//print_r($this->getStudentTable()->fetchAll()); }
 1在模板页面的调用  
<?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>&nbsp;&nbsp;
<a href='#' class='icol-key changePwd'></a>&nbsp;&nbsp;
<a herf='#' class='icol-cross deleteStud'></a>
</td>
</tr>
<?php endforeach;?>

zendframework 2 链接数据库的更多相关文章

  1. PHP学习-链接数据库

    链接数据库文件:conn.php <?php $conn = mysql_connect("localhost:3306","root","us ...

  2. PHP 链接数据库1(连接数据库&简单的登录注册)

    对 解析变量的理解 数据库的名称和表的名称不能重复 从结果中取出的数据   都是以数组的形式取出的 1.PHP查询数据库中的某条信息 //PHP链接数据库 /*1.造链接对象 IP地址 用户名 密码 ...

  3. JDBC的使用(一):引用外部jar;代码链接数据库

    一:引用外部jar 1.首先不jar文件放到项目下: 2.在Eclipse中,右键相应的项目--构建路径--配置构建路径--库--添加外部jar:选中-打开-应用-确定. 二:代码链接数据库 1.加载 ...

  4. Connect to Database Using Custom params链接数据库配置参数说明

    使用RF的关键字Connect to Database Using Custom params链接数据库,对应的参数说明: a)     第一个参数我使用的是cx_Oracle,就写这个 b)     ...

  5. php链接数据库

      1:找到 ySQL服务器 $conn = mysql_connect("localhost","","") or die("链 ...

  6. 安装vs2013以后,链接数据库总是报内存损坏,无法写入的错误

    安装vs2013以后,链接数据库总是报内存损坏,无法写入的错误 这个错误几个月以前解决过一次,但是到又碰到的时候,竟然完全忘记当时怎么解决的了, 看来上了年纪记忆真是越来越不行了... 解决方案很简单 ...

  7. jsp链接数据库

    数据库表代码: /*Navicat MySQL Data Transfer Source Server : localhost_3306Source Server Version : 50528Sou ...

  8. 本地开发 localhost链接数据库比127.0.0.1慢

    自己手写一段代码的时候发现一个问题  链接数据库的时候 用 127.0.0.1比localhost明显的快,localhost要等一下才会有响应 而127.0.0.1就是瞬间响应.一番排查,发现了一个 ...

  9. 2017-3-2 C#链接数据库实现登陆

    只是链接一个数据库就有好多的知识:) 实际操作下来,主要是两种登陆方式: 1.Windows的身份验证: 2.Sql Sever的身份验证: 两种的方法不同,但是主要是通过复制创建数据库的字符串来链接 ...

随机推荐

  1. RTP在。net中的使用(资料)

    开源组件:lumisoft 网址:http://www.lumisoft.ee/lswww/download/downloads/Examples/ 非开源的免费组建:rtp.net (微软推荐)

  2. Maven之自定义archetype生成项目骨架(一)

      Maven之自定义archetype生成项目骨架(一) 标签: mavennexus插件 2015-07-15 16:40 2443人阅读 评论(0) 收藏 举报  分类: Maven技术(9)  ...

  3. PS:蓝天白云的制作

    方法一: 1.新建(ctrl+N),根据自己的需求设置画面大小: 2.设置前景色为蓝天颜色,alt+delete,填充为天空蓝颜色: 3.滤镜-渲染-云彩,得出蓝天白云效果: 4.根据需求再调整亮度. ...

  4. C# Susan边缘检测(Susan Edge Detection)

    Susan边缘检测,方法简单,效率高,具体参照 The SUSAN Edge Detector in Detail, 修改dThreshold值,可以改变检测效果,用参照提供的重心法.力矩法可得到边缘 ...

  5. css3中的字体样式

    text-overform:ellipsis省略号/clip裁剪. overform:hidden溢出隐藏文字. 但是text-overflow只是用来说明文字溢出时用什么方式显示,要实现溢出时产生省 ...

  6. Sublime Text 安装Emmet

    1.简单的安装方法 从菜单 View - Show Console 或者 ctrl + ~ 快捷键,调出 console.将以下 Python 代码粘贴进去并 enter 执行,不出意外即完成安装.以 ...

  7. 简单jquery实现select三级联动

    简单的jquery实现select三级联动 代码如下: <!DOCTYPE html> <html> <head> <meta charset="u ...

  8. C/C++编译链接过程详解

    有些人写C/C++(以下假定为C++)程序,对unresolved external link或者duplicated external simbol的错误信息不知所措(因为这样的错误信息不能定位到某 ...

  9. 【学习】JAVA的第一天(补)

    补上1024第一天的JAVA学习,也刚好是程序员节.今天学习了JAVA的一些历史和基础环境的配置,以及HelloWorld入门程序. JAVA体系:JAVAME(一些简单的移动端开发).JAVAEE( ...

  10. JS 前端格式化JSON字符串工具

    JSON格式化工具,简易实现.作为技术宅,直接上代码,供大家使用.前提:一定要引入jquery哦. <!DOCTYPE html> <html lang="en" ...