首頁 >後端開發 >php教程 >關於ZendFramework2連接資料庫的操作

關於ZendFramework2連接資料庫的操作

不言
不言原創
2018-06-15 15:16:341542瀏覽

這篇文章主要介紹了ZendFramework2連接資料庫操作,結合完整實例形式分析了ZendFramework2連接資料庫的具體步驟、配置方法、相關操作技巧與注意事項,需要的朋友可以參考下

本文實例講述了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=&#39;cc_user&#39;;
  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(&#39;cc_user&#39;);
      // 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(&#39;id&#39; => $id));
    $row = $rowset->current();
    if (!$row) {
      throw new \Exception("Could not find row $id");
    }
    return $row;
  }
  public function deleteStudent($id)
  {
    $this->tableGateway->delete(array(&#39;id&#39; => $id));
  }
  public function getLIValue(){
    return $this->tableGateway->getLastInsertValue();
  }
}

Student/IndexController.php 呼叫資料庫

public function indexAction(){
    /* return new ViewModel(array(
      &#39;students&#39; => $this->getStudentTable()->fetchAll(), //不分页
    ));*/
    $page=$this->params(&#39;page&#39;);//走分页 在model.config.php里面设置:
/*      model.config.php      
&#39;defaults&#39; => array(
 &#39;controller&#39; => &#39;Student\Controller\Index&#39;,
 &#39;action&#39;   => &#39;index&#39;,
 &#39;page&#39;=>&#39;1&#39;,
),
*/
    $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(&#39;page&#39;, $page));
    // set the number of items per page to 10
    $paginator->setItemCountPerPage(10);
    return new ViewModel(array(
      &#39;paginator&#39; => $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=&#39;#&#39;  class=&#39;icol-bandaid editUserInfo&#39;></a>  
      <a href=&#39;#&#39; class=&#39;icol-key changePwd&#39;></a>  
      <a herf=&#39;#&#39;  class=&#39;icol-cross deleteStud&#39;></a>
    </td>
  </tr>
<?php endforeach;?>

以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!

相關推薦:

Zend Framework中Bootstrap類別的用法解析

Yii2框架實作資料庫常用操作解析

##關於Zend Framework如何實作將session儲存在memcache中

以上是關於ZendFramework2連接資料庫的操作的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn