一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - ZendFramework2連接數據庫操作實例

ZendFramework2連接數據庫操作實例

2021-05-10 14:25e路相扶 PHP教程

這篇文章主要介紹了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);//table Name is cc_user
        },
      ),
    );
}

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

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) {
      // 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 調用數據庫

?
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(){
    /* 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());
}

在模板頁面的調用

?
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>//應用了在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;?>

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99精品久久久久久 | 成人国产一区二区 | 亚洲高清在线精品一区 | 色无月 | 欧美成狂野欧美在线观看 | 臀精插宫NP文| www.爱情岛论坛 | 成人综合网站 | 国产福利在线观看永久视频 | 国产精品露脸国语对白河北 | 日韩视频免费 | 亚洲精品色婷婷在线影院麻豆 | 日韩中文字幕在线不卡 | 天堂一区二区在线观看 | 逼逼狗影院 | 亚洲福利一区二区三区 | 日本制服丝袜 | 91po国产在线高清福利 | 91免费高清视频 | 天天排行网 | 青青国产精品 | 亚洲免费视频在线 | 红色毛片| 精品久久一 | 水多多凹凸福利视频导航 | 国产 国语对白 露脸正在播放 | 婚前试爱全集免费观看 | 久久精品熟女亚洲AV国产 | 国产在线观看91 | 亚洲午夜精品久久久久久抢 | 奇米7777第四色 | 国产99久久九九精品免费 | 精新精新国产自在现拍 | 国产视频中文字幕 | 我要看黄色毛片 | 国产小视频免费看 | 日本美女xx| 亚州vs欧州vs日 | 久久精品18| 国产成人精品一区二三区 | 亚洲精品片 |