相对于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. 微信公众账号开发之N个坑(一)

    我这人干活没有前奏,喜欢直接开始.完了,宝宝已经被你们带污了.. 微信公众账号开发文档,官方版(https://mp.weixin.qq.com/wiki),相信我,我已经无力吐槽写这个文档的人了,我 ...

  2. oracle 秒

    select case when  deptno=10 then 'aaaa' when  deptno=20 then 'bbbb' when deptno=30 then 'cccc'     e ...

  3. android接收短信——framework处理流程(android 5.1)

    modem层不懂,所以直接从RIL.java开始.以电信卡接收短信为例 modem通知RIL.java中的 RILReceiver处理接收信息 class RILReceiver implements ...

  4. db2设置共享内存

    db2 UPDATE DBM CFG USING INSTANCE_MEMORY 5242880 IMMEDIATEdb2 UPDATE DBM CFG USING INSTANCE_MEMORY A ...

  5. Oracle统计函数之Lead

    一,Lead 语法及例子 Lead函数是十分的好用的一个函数.它的语法如下图: 简单地说,lead是个奇特函数,在允许不使用自连接的情况下,一次返回多行. 参数说明: value_expr 值表达式, ...

  6. Android应用第一次安装成功点击“打开”后Home键切出应用后再点击桌面图标返回导致应用重启问题

    最近项目中遇到一个问题,用户第一次安装应用在系统的安装器安装完成界面有“完成”和“打开”两个按钮. 当用户点击“打开”按钮进入用户注册页面进行手机号验证码发送和验证码输入等操作界面,若此时用户点击Ho ...

  7. 前台js与后台方法互调

    一:后台调用前台js的方法 ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", " ...

  8. nginx搭建高性能流媒体技术

    一. 系统环境 系统版本: CentOS 5.8x86_64 Nginx版本: Nginx-1.4.2.tar.gz Yamdi版本:Yamdi-1.9.tar.gz 二. 自动化安装 #!/bin/ ...

  9. 【ImageView】ImageView点击事件报错空指针

    今天在使用自定义圆形imageview的时候,想利用其点击事件来实现查看个人资料功能,但是该空间在Activity中的onCreate方法中调用点击事件总是出现空指针异常,每次程序都进不去主页面,到处 ...

  10. [LeetCode] 435 Non-overlapping Intervals

    Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...