ZendFramework2连接数据库操作实例
本文实例讲述了ZendFramework2连接数据库操作。分享给大家供大家参考,具体如下:
相对于zf1,来说,zf2让我们对于数据库这方面的操作我的个人感觉是对于字段起别名简单了,但是对数据库的操作虽然配置写好的就基本不需要动了,但是还是比1的配置要繁琐,
还是那句话,大家可以去看看源码。。。
Module.php里面添加
publicfunctiongetServiceConfig()
{
returnarray(
'factories'=>array(
'Student\Model\StudentTable'=>function($sm){
$tableGateway=$sm->get('StudentTableGateway');
$table=newStudentTable($tableGateway);
return$table;
},
'StudentTableGateway'=>function($sm){
$dbAdapter=$sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype=newResultSet();
$resultSetPrototype->setArrayObjectPrototype(newStudent());
returnnewTableGateway('cc_user',$dbAdapter,null,$resultSetPrototype);//tableNameiscc_user
},
),
);
}
student.php这个是Model/Student.php
namespaceStudent\Model;
classStudent
{
public$id;
public$name;
public$phone;
public$mark;
public$email;
publicfunctionexchangeArray($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;
}
}
StudentTable.phpModel/StudentTable.php
tableGateway=$tableGateway;
}
publicfunctionfetchAll($paginated)
{//分页
if($paginated){
//createanewSelectobjectforthetablealbum
$select=newSelect('cc_user');
//createanewresultsetbasedontheStudententity
$resultSetPrototype=newResultSet();
$resultSetPrototype->setArrayObjectPrototype(newStudent());
//createanewpaginationadapterobject
$paginatorAdapter=newDbSelect(
//ourconfiguredselectobject
$select,
//theadaptertorunitagainst
$this->tableGateway->getAdapter(),
//theresultsettohydrate
$resultSetPrototype
);
$paginator=newPaginator($paginatorAdapter);
return$paginator;
}
$resultSet=$this->tableGateway->select();
return$resultSet;
}
publicfunctiongetStudent($id)
{
$id=(int)$id;
$rowset=$this->tableGateway->select(array('id'=>$id));
$row=$rowset->current();
if(!$row){
thrownew\Exception("Couldnotfindrow$id");
}
return$row;
}
publicfunctiondeleteStudent($id)
{
$this->tableGateway->delete(array('id'=>$id));
}
publicfunctiongetLIValue(){
return$this->tableGateway->getLastInsertValue();
}
}
Student/IndexController.php调用数据库
publicfunctionindexAction(){
/*returnnewViewModel(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);
//setthecurrentpagetowhathasbeenpassedinquerystring,orto1ifnoneset
$paginator->setCurrentPageNumber((int)$this->params()->fromQuery('page',$page));
//setthenumberofitemsperpageto10
$paginator->setItemCountPerPage(10);
returnnewViewModel(array(
'paginator'=>$paginator//模板页面调用的时候的名字
));
//print_r($this->getStudentTable()->fetchAll());
}
在模板页面的调用
paginatoras$student):?>escapeHtml($student->id);?>"> escapeHtml($student->id);?> escapeHtml($student->name);?> escapeHtml($student->phone);?> escapeHtml($student->email);?> //应用了在Student.php的别名escapeHtml($student->mark);?>
更多关于zend相关内容感兴趣的读者可查看本站专题:《ZendFrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于ZendFramework框架的PHP程序设计有所帮助。