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的身份验证: 两种的方法不同,但是主要是通过复制创建数据库的字符串来链接 ...
随机推荐
- Windows Server 2003 服务器备份和恢复技巧
1.系统备份与恢复 现在很多中小型企业服务器并没有采用专业的服务器备份机制进行备份,对于个人站长而言这似乎更不可能.但是再好的服务器维护人员总有失误的时候,为了防止万一我们最好还是采用一些备 ...
- .Net中的加密解密
返回博客列表 转 .Net中的加密解密 李朝强 发布时间: 2015/11/23 12:55 阅读: 33 收藏: 3 点赞: 0 评论: 0 在一些比较重要的应用场景中,通过网络传递数据需要进行加密 ...
- css3之文本新属性
- Being a Good Boy in Spring Festival 尼姆博弈
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Descr ...
- 如何更改OS系统下截图生成图片格式 jpg pdf
Mac系统下快速截屏的快捷键: 1.截全屏: shift + command + 3 2.选取截屏 shift + command + 4 生成的图片,系统默认格式忘了,反正不好用,用下面命令更改生成 ...
- WebView cookie 管理
在Android应用程序中经常会加载一个WebView页,如果需要客户端向WebView传递信息,比如Cookie,也是可以的. 需要应用程序先将Cookie注入进去,打开该网页时,WebView会将 ...
- Mvc模板页
以前学过WEB的应该都知道母版页这个东西,在这里呢,mvc的模板页和WEB中的模板页也是一样的效果和用途. 首先:创建一个项目,控制器,生成Index视图,在生成视图的时候我们选择Razor视图引擎, ...
- SpringMVC文件上传下载
在Spring MVC的基础框架搭建起来后,我们测试了spring mvc中的返回值类型,如果你还没有搭建好springmvc的架构请参考博文->http://www.cnblogs.com/q ...
- 通过navigationController跳转界面时隐藏navigationBar上的元素
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- 黑马程序员_Java基础:网络编程总结
------- android培训.java培训.期待与您交流! ---------- Java语言是在网络环境下诞生的,它是第一个完全融入网络的语言,虽然不能说它是对支持网络编程做得最好的语言,但是 ...