幽灵资源网 Design By www.bzswh.com
本文实例讲述了ZendFramework2连接数据库操作。分享给大家供大家参考,具体如下:
相对于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
},
),
);
}
student.php 这个是Model/Student.php
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'])) "htmlcode">
<"Could not find row $id");
}
return $row;
}
public function deleteStudent($id)
{
$this->tableGateway->delete(array('id' => $id));
}
public function getLIValue(){
return $this->tableGateway->getLastInsertValue();
}
}
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'=>'1',
),
*/
$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(10);
return new ViewModel(array(
'paginator' => $paginator //模板页面调用的时候的名字
));
//print_r($this->getStudentTable()->fetchAll());
}
在模板页面的调用
<"<">
<td><"_blank" href="https://www.jb51.net/Special/546.htm">Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。
幽灵资源网 Design By www.bzswh.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
幽灵资源网 Design By www.bzswh.com
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?