Rumah > pembangunan bahagian belakang > tutorial php > 关于ZendFramework2连接数据库的操作

关于ZendFramework2连接数据库的操作

不言
Lepaskan: 2023-04-01 11:00:02
asal
1633 orang telah melayarinya

这篇文章主要介绍了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

        },

      ),

    );

}

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=&#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();

  }

}

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(){

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

}

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

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!

Label berkaitan:
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan