欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ZendFramework2连接数据库操作实例

程序员文章站 2024-03-08 17:52:28
本文实例讲述了zendframework2连接数据库操作。分享给大家供大家参考,具体如下: 相对于zf1,来说,zf2让我们对于数据库这方面的操作我的个人感觉是对于字段起...

本文实例讲述了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'])) ? $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.php model/studenttable.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();
  }
}

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());
}

在模板页面的调用

<?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;?>

更多关于zend相关内容感兴趣的读者可查看本站专题:《zend framework框架入门教程》、《php优秀开发框架总结》、《yii框架入门及常用技巧总结》、《thinkphp入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家基于zend framework框架的php程序设计有所帮助。