首页 技术 正文
技术 2022年11月14日
0 收藏 894 点赞 2,689 浏览 3666 个字

相对于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;?>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290