这篇文章主要介绍了ZendFramework2连接数据库操作,结合完整实例形式分析了ZendFramework2连接数据库的具体步骤、配置方法、相关操作技巧与注意事项,需要的朋友可以参考下
本文实例讲述了ZendFramework2连接数据库操作。分享给大家供大家参考,具体如下:
相对于zf1,来说,zf2让我们对于数据库这方面的操作我的个人感觉是对于字段起别名简单了,但是对数据库的操作虽然配置写好的就基本不需要动了,但是还是比1的配置要繁琐,
还是那句话,大家可以去看看源码。。。
Module.php 里面添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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 );
},
),
);
}
|
Salin selepas log masuk
student.php 这个是Model/Student.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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;
}
}
|
Salin selepas log masuk
StudentTable.php Model/StudentTable.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | <?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 ) {
$select = new Select('cc_user');
$resultSetPrototype = new ResultSet();
$resultSetPrototype ->setArrayObjectPrototype( new Student());
$paginatorAdapter = new DbSelect(
$select ,
$this ->tableGateway->getAdapter(),
$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();
}
}
|
Salin selepas log masuk
Student/IndexController.php 调用数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public function indexAction(){
$page = $this ->params('page');
$paginator = $this ->getStudentTable()->fetchAll(true);
$paginator ->setCurrentPageNumber((int) $this ->params()->fromQuery('page', $page ));
$paginator ->setItemCountPerPage(10);
return new ViewModel( array (
'paginator' => $paginator
));
}
|
Salin selepas log masuk
在模板页面的调用
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?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>
<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 ;?>
|
Salin selepas log masuk
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
Zend
Framework中Bootstrap类的用法解析
Yii2框架实现数据库常用操作解析
关于Zend Framework如何实现将session存储在memcache中
Atas ialah kandungan terperinci 关于ZendFramework2连接数据库的操作. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!