zendframework 2 链接数据库
相对于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>
<a href='#' class='icol-key changePwd'></a>
<a herf='#' class='icol-cross deleteStud'></a>
</td>
</tr>
<?php endforeach;?>
zendframework 2 链接数据库的更多相关文章
- PHP学习-链接数据库
链接数据库文件:conn.php <?php $conn = mysql_connect("localhost:3306","root","us ...
- PHP 链接数据库1(连接数据库&简单的登录注册)
对 解析变量的理解 数据库的名称和表的名称不能重复 从结果中取出的数据 都是以数组的形式取出的 1.PHP查询数据库中的某条信息 //PHP链接数据库 /*1.造链接对象 IP地址 用户名 密码 ...
- JDBC的使用(一):引用外部jar;代码链接数据库
一:引用外部jar 1.首先不jar文件放到项目下: 2.在Eclipse中,右键相应的项目--构建路径--配置构建路径--库--添加外部jar:选中-打开-应用-确定. 二:代码链接数据库 1.加载 ...
- Connect to Database Using Custom params链接数据库配置参数说明
使用RF的关键字Connect to Database Using Custom params链接数据库,对应的参数说明: a) 第一个参数我使用的是cx_Oracle,就写这个 b) ...
- php链接数据库
1:找到 ySQL服务器 $conn = mysql_connect("localhost","","") or die("链 ...
- 安装vs2013以后,链接数据库总是报内存损坏,无法写入的错误
安装vs2013以后,链接数据库总是报内存损坏,无法写入的错误 这个错误几个月以前解决过一次,但是到又碰到的时候,竟然完全忘记当时怎么解决的了, 看来上了年纪记忆真是越来越不行了... 解决方案很简单 ...
- jsp链接数据库
数据库表代码: /*Navicat MySQL Data Transfer Source Server : localhost_3306Source Server Version : 50528Sou ...
- 本地开发 localhost链接数据库比127.0.0.1慢
自己手写一段代码的时候发现一个问题 链接数据库的时候 用 127.0.0.1比localhost明显的快,localhost要等一下才会有响应 而127.0.0.1就是瞬间响应.一番排查,发现了一个 ...
- 2017-3-2 C#链接数据库实现登陆
只是链接一个数据库就有好多的知识:) 实际操作下来,主要是两种登陆方式: 1.Windows的身份验证: 2.Sql Sever的身份验证: 两种的方法不同,但是主要是通过复制创建数据库的字符串来链接 ...
随机推荐
- Test注解的两个属性(转)
Test注解的两个属性:expected和timeout Junit的Test注解支持两个可选的参数expected和timeout.expected声明一个测试方法必须抛出一个异常.如果不抛出异常或 ...
- VS2010--canot determine the locationof the vs common tools folder
在vcvars32.bat第一行后复制 @SET VSINSTALLDIR=c:\Program Files\Microsoft Visual Studio 10.0 @SET VCINSTALLDI ...
- 1236 - Pairs Forming LCM -- LightOj1236 (LCM)
http://lightoj.com/volume_showproblem.php?problem=1236 题目大意: 给你一个数n,让你求1到n之间的数(a,b && a<= ...
- System.Security.SecurityException The source was not found, but some or all event logs could not be searched.Inaccessible logs Security.
An exception occurred during the Install phase. System.Security.SecurityException The source was not ...
- OD使用教程11
首先把安装好的软件拖入PEID,看看它是用什么语言写的 然后用OD载入程序,查找关键字,步骤看上一个笔记 双击到达代码处,发现这在一个跳转里面.可能第一反应是修改跳转,经试验后发现这是没用的所以 ...
- LoopBackJS 之 文件上传下载——使用loopback-component-storage
参考链接: http://loopback.io/doc/en/lb2/Storage-component.html#creating-a-storage-component-data-source ...
- java继承的构造函数
1.在构造函数的继承中,子类不能继承父类的构造函数.但是子类却可以用super()或super(参数)使用父类的构造函数, <1>无参的构造函数: //父类 public class An ...
- XAF ListView 移除顶部工具栏
此方法适用于C/S及B/S,无需分别写在web和win中. Module下新建ViewController,代码如下: public partial class GongZuoJiaoShen_Yin ...
- 使用joi来验证数据模型
我们用nodejs实现一些功能时,往往需要对用户输入的数据进行验证.然而,验证是一件麻烦的事情,很有可能你需要验证数据类型,长度,特定规则等等,在前端做表单验证时,我们常用的做法是使用正则,正则表达式 ...
- SQL分页常用的两个存储过程
在做数据绑定时,我们常常会遇到分页事件,下面记录一下分页常用到SQL的两个存储过程 ①/****分页数据总数****//****** 对象: StoredProcedure [dbo].[GetRe ...