Home php教程 PHP开发 zend framework implements operation methods that support sql server

zend framework implements operation methods that support sql server

Dec 26, 2016 pm 03:19 PM

The example in this article describes the operation method of zend framework to support sql server. Share it with everyone for your reference, the details are as follows:

1. Modify the connect method in Zend/Db/Adapter/Pdo/Abstract.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

57

58

protected function _connect()

{

  // if we already have a PDO object, no need to re-connect.

  if ($this->_connection) {

    return;

  }

  // get the dsn first, because some adapters alter the $_pdoType

  $dsn = $this->_dsn();

  // check for PDO extension

  if (!extension_loaded('pdo')) {

    /**

     * [url=home.php?mod=space&uid=86763]@see[/url] Zend_Db_Adapter_Exception

     */

    require_once 'Zend/Db/Adapter/Exception.php';

    throw new Zend_Db_Adapter_Exception('The PDO extension is required for this adapter but the extension is not loaded');

  }

  // check the PDO driver is available

  if (!in_array($this->_pdoType, PDO::getAvailableDrivers())) {

    /**

     * @see Zend_Db_Adapter_Exception

     */

    require_once 'Zend/Db/Adapter/Exception.php';

    throw new Zend_Db_Adapter_Exception('The ' . $this->_pdoType . ' driver is not currently installed');

  }

  // create PDO connection

  $q = $this->_profiler->queryStart('connect', Zend_Db_Profiler::CONNECT);

  // add the persistence flag if we find it in our config array

  if (isset($this->_config['persistent']) && ($this->_config['persistent'] == true)) {

    $this->_config['driver_options'][PDO::ATTR_PERSISTENT] = true;

  }

  try {

    //print_r($this->_config);exit;

    if($this->_config['pdoType']=='sqlsrv'){

      $this->_connection = new PDO( "sqlsrv:Server=".$this->_config['host'].";Database = ".$this->_config['dbname'], $this->_config['username'], $this->_config['password']);

      $this->_connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

      $this->_connection->setAttribute( PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_UTF8 );

      $this->_profiler->queryEnd($q);

    }elseif ($this->_config['pdoType']=='dblib') {

      $this->_connection = new PDO(

        $dsn,

        $this->_config['username'],

        $this->_config['password'],

        $this->_config['driver_options']

      );

      $this->_profiler->queryEnd($q);

    }

    // set the PDO connection to perform case-folding on array keys, or not

    $this->_connection->setAttribute(PDO::ATTR_CASE, $this->_caseFolding);

    // always use exceptions.

    $this->_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  } catch (PDOException $e) {

    /**

     * @see Zend_Db_Adapter_Exception

     */

    require_once 'Zend/Db/Adapter/Exception.php';

    throw new Zend_Db_Adapter_Exception($e->getMessage());

  }

}

Copy after login

There are two connections provided for linux and windows. Way. The web.xml database configuration file of

2.mssql.php is protected $_pdoType = 'sqlsrv';

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

protected function _dsn()

{

    // baseline of DSN parts

    $dsn = $this->_config;

    // don't pass the username and password in the DSN

    unset($dsn['username']);

    unset($dsn['password']);

    unset($dsn['driver_options']);

    if (isset($dsn['port'])) {

      $seperator = ':';

      if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {

        $seperator = ',';

      }

      $dsn['host'] .= $seperator . $dsn['port'];

      unset($dsn['port']);

    }

    // this driver supports multiple DSN prefixes

    // @see http://www.php.net/manual/en/ref.pdo-dblib.connection.php

    //print_r($dsn);exit;

    if (isset($dsn['pdoType'])) {

      switch (strtolower($dsn['pdoType'])) {

        case 'freetds':

        case 'sybase':

          $this->_pdoType = 'sybase';

          break;

        case 'mssql':

          $this->_pdoType = 'mssql';

          break;

        case 'sqlsrv':

          $this->_pdoType = 'sqlsrv';

          break;

        case 'dblib':

        default:

          $this->_pdoType = 'dblib';

          break;

      }

      unset($dsn['pdoType']);

    }

    // use all remaining parts in the DSN

    foreach ($dsn as $key => $val) {

      $dsn[$key] = "$key=$val";

    }

    $dsn = $this->_pdoType . ':' . implode(';', $dsn);

   // print_r($dsn);exit;

    return $dsn;

}

Copy after login

3.ZF to:

1

2

3

4

5

6

7

8

9

10

<db>

  <adapter>PDO_MSSQL</adapter>

<config>

    <host>localhost</host>

    <username>sa</username>

    <password>123456</password>

    <dbname>testdb </dbname>

    <pdoType>sqlsrv</pdoType>

  </config>

</db>

Copy after login

period When encountering Chinese garbled characters

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

function convert2utf8($string)

{

    $config = $this->getCfg();

    $pdoType = $config->db->config->pdoType;

    if($pdoType == &#39;dblib&#39;){

      return iconv("gbk","utf-8",$string);

    }elseif($pdoType == &#39;sqlsrv&#39;){

      return mb_convert_encoding($string,"UTF-8","auto");

    }

}

function convert2gbk($string)

{

    $config = $this->getCfg();

    $pdoType = $config->db->config->pdoType;

    if($pdoType == &#39;dblib&#39;){

      return iconv("utf-8","gbk",$string);

    }elseif($pdoType == &#39;sqlsrv&#39;){

      return mb_convert_encoding($string,"GBK","auto");

    }

}

protected function &getCfg() {

    if ($this->cfg_ === null) {

      $registry = Zend_Registry::getInstance();

      $this->cfg_ = $registry->get(&#39;web_config&#39;);

    }

    return $this->cfg_;

}

Copy after login

, we will handle different types of problems in different ways.

I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.

For more related articles on how to implement the zend framework to support sql server, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)