最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ZendFramework2連接數(shù)據(jù)庫操作實例

 更新時間:2017年04月18日 11:00:30   作者:e路相扶  
這篇文章主要介紹了ZendFramework2連接數(shù)據(jù)庫操作,結(jié)合完整實例形式分析了ZendFramework2連接數(shù)據(jù)庫的具體步驟、配置方法、相關(guān)操作技巧與注意事項,需要的朋友可以參考下

本文實例講述了ZendFramework2連接數(shù)據(jù)庫操作。分享給大家供大家參考,具體如下:

相對于zf1,來說,zf2讓我們對于數(shù)據(jù)庫這方面的操作我的個人感覺是對于字段起別名簡單了,但是對數(shù)據(jù)庫的操作雖然配置寫好的就基本不需要動了,但是還是比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 調(diào)用數(shù)據(jù)庫

public function indexAction(){
    /* return new ViewModel(array(
      'students' => $this->getStudentTable()->fetchAll(), //不分頁
    ));*/
    $page=$this->params('page');//走分頁 在model.config.php里面設(shè)置:
/*      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 //模板頁面調(diào)用的時候的名字
    ));
  //print_r($this->getStudentTable()->fetchAll());
}

在模板頁面的調(diào)用

<?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>//應(yīng)用了在Student.php的別名
  <td><?php echo $this->escapeHtml($student->mark);?></td>
    <td><a href='#'  class='icol-bandaid editUserInfo'></a>&nbsp;&nbsp;
      <a href='#' class='icol-key changePwd'></a>&nbsp;&nbsp;
      <a herf='#'  class='icol-cross deleteStud'></a>
    </td>
  </tr>
<?php endforeach;?>

更多關(guān)于zend相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Zend FrameWork框架入門教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《Yii框架入門及常用技巧總結(jié)》、《ThinkPHP入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家基于Zend Framework框架的PHP程序設(shè)計有所幫助。

相關(guān)文章

  • Yii 框架入口腳本示例分析

    Yii 框架入口腳本示例分析

    這篇文章主要介紹了Yii 框架入口腳本,結(jié)合實例形式分析了Yii 框架入口腳本基本功能、原理及相關(guān)操作技巧,需要的朋友可以參考下
    2020-05-05
  • php metaphone()函數(shù)的定義和用法

    php metaphone()函數(shù)的定義和用法

    下面小編就為大家?guī)硪黄猵hp metaphone()函數(shù)的定義和用法。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-05-05
  • php實現(xiàn)微信發(fā)紅包功能

    php實現(xiàn)微信發(fā)紅包功能

    這篇文章主要為大家詳細(xì)介紹了php實現(xiàn)微信發(fā)紅包功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • typecho插件編寫教程(一):Hello World

    typecho插件編寫教程(一):Hello World

    這篇文章主要介紹了typecho插件編寫教程(一):Hello World,本文講解了插件的文件結(jié)構(gòu)、插件信息、插件結(jié)構(gòu)、插件流程等內(nèi)容,需要的朋友可以參考下
    2015-05-05
  • 分享六個比較好用的php數(shù)組Array函數(shù)

    分享六個比較好用的php數(shù)組Array函數(shù)

    這篇文章給大家分享六個比較好用的php數(shù)組Array函數(shù),非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧
    2018-07-07
  • ThinkPHP之import方法實例詳解

    ThinkPHP之import方法實例詳解

    import方法是ThinkPHP框架用于類庫導(dǎo)入的封裝實現(xiàn),這篇文章主要介紹了ThinkPHP的import方法,需要的朋友可以參考下
    2014-06-06
  • PHP中trait使用方法詳細(xì)介紹

    PHP中trait使用方法詳細(xì)介紹

    本篇文章主要介紹了PHP中trait使用方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • Thinkphp5框架實現(xiàn)獲取數(shù)據(jù)庫數(shù)據(jù)到視圖的方法

    Thinkphp5框架實現(xiàn)獲取數(shù)據(jù)庫數(shù)據(jù)到視圖的方法

    這篇文章主要介紹了Thinkphp5框架實現(xiàn)獲取數(shù)據(jù)庫數(shù)據(jù)到視圖的方法,涉及thinkPHP5數(shù)據(jù)庫配置、讀取、模型操作及視圖調(diào)用相關(guān)操作技巧,需要的朋友可以參考下
    2019-08-08
  • PHP實現(xiàn)圖片旋轉(zhuǎn)效果實例代碼

    PHP實現(xiàn)圖片旋轉(zhuǎn)效果實例代碼

    這篇文章主要介紹了PHP實現(xiàn)圖片旋轉(zhuǎn)效果實例代碼,本文給出代碼示例,代碼中包含一些說明注釋,需要的朋友可以參考下
    2014-10-10
  • thinkPHP商城公告功能開發(fā)問題分析

    thinkPHP商城公告功能開發(fā)問題分析

    這篇文章主要介紹了thinkPHP商城公告功能開發(fā)問題,結(jié)合實例形式分析了基于thinkPHP實現(xiàn)商城公告功能所涉及的ajax交互及數(shù)據(jù)庫操作相關(guān)技巧,需要的朋友可以參考下
    2016-12-12

最新評論

贵定县| 澄江县| 宁夏| 轮台县| 化德县| 德惠市| 翼城县| 襄汾县| 酒泉市| 临漳县| 康定县| 铜川市| 麻栗坡县| 延吉市| 宿州市| 汕尾市| 榆林市| 许昌市| 平安县| 广丰县| 安徽省| 博乐市| 信丰县| 新泰市| 固阳县| 利川市| 周至县| 秦皇岛市| 隆安县| 卫辉市| 嘉峪关市| 兰溪市| 延津县| 理塘县| 花莲县| 江川县| 连山| 凌源市| 南阳市| 岳阳县| 县级市|