ホームページ php教程 PHP开发 ZendFramework フレームワークは、2 つ以上のデータベースを接続するメソッドを実装します。

ZendFramework フレームワークは、2 つ以上のデータベースを接続するメソッドを実装します。

Dec 14, 2016 am 10:34 AM

この記事の例では、ZendFramework フレームワークを使用して 2 つ以上のデータベースを接続する方法について説明します。参考のために皆さんと共有してください。詳細は次のとおりです:

設定ファイル:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<db>

   <adapter>PDO_MSSQL</adapter>

 <config>

     <host>localhost</host>

     <port>1433</port>

     <username>sa</username>

     <password>123456</password>

     <dbname>edudb</dbname>

     <pdoType>sqlsrv</pdoType>

   </config>

 </db>

 <!-- 测试多数据库 -->

 <db2>

   <adapter>PDO_MSSQL</adapter>

     <config>

     <host>localhost</host>

     <port>1433</port>

     <username>sa</username>

     <password>123456</password>

     <dbname>test</dbname>

     <pdoType>sqlsrv</pdoType>

   </config>

 </db2>

ログイン後にコピー

インレットファイル

1

2

3

4

5

6

7

8

9

//配置数据库连接

$db_config = $web_config->db->config->toArray();

//var_dump($db_config);

$db = Zend_Db::factory($web_config->db->adapter, $db_config);

//var_dump($db);

//exit;

//$db->query(&#39;set NAMES utf8&#39;);

//$db->getProfiler()->setEnabled(false);

Zend_Db_Table::setDefaultAdapter($db);

ログイン後にコピー

これがデフォルトのデータベースです

dao.php はデフォルトのデータベース

1

$db = &$this->getAdapter();

ログイン後にコピー
ログイン後にコピー

を呼び出します

dao2.php は他のデータベースに接続します

1

2

3

4

5

6

7

8

9

10

function init() {

    $web_config = $this->getCfg();

    $this->db2_config = $web_config->db2->config->toArray();

    //var_dump($this->db_config);

    $this->db = Zend_Db::factory($web_config->db2->adapter, $this->db2_config);

    Zend_Db_Table::setDefaultAdapter($this->db);

}

public function returnDb(){

    return $this->db;

}

ログイン後にコピー

Call

1

$db = &$this->getAdapter();

ログイン後にコピー
ログイン後にコピー

は引き続きデフォルトのデータベースに接続します。

使い方は

1

$this->db

ログイン後にコピー

完全な dao2.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

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

<!--?php

class dao_dao2 extends Zend_Db_Table {

  protected $cfg_;

  function init() {

    $web_config = $this--->getCfg();

    $this->db2_config = $web_config->db2->config->toArray();

    //var_dump($this->db_config);

    $this->db = Zend_Db::factory($web_config->db2->adapter, $this->db2_config);

    Zend_Db_Table::setDefaultAdapter($this->db);

  }

  public function returnDb(){

    return $this->db;

  }

  public function getData($table,$where = false, $order = 'id ASC', $pagesize = false, $offset = false, $count = false, $from = false, $join = false, $group = false) {

    //$this->db = &$this->getAdapter();

    $select = $this->db->select();

    if ($where && is_array($where)) {

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

        //print_r($where);

        if($val['type']==1){

          $select->where($key, $this->convert2gbk($val['val']));

        }else{

          $select->orwhere($key, $this->convert2gbk($val['val']));

        }

      }

    }

    if (!$from)

      $from = '*';

    //echo $select."<br>";

    if ($pagesize) {

      $select->limit($pagesize, $offset);

    }

    //echo $select."<br>";

    if (is_array($order)) {

      foreach ($order as $value) {

        $select->order($value);

      }

    } else {

      $select->order($order);

    }

    //echo $select."<br>";

    $select->from($table, $count ? "COUNT(".$table.".id)" : $from);

    if (is_array($group)) {

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

        $select->group($val);

      }

      if ($count) {

        $result = $this->db->fetchAll($select);

        //echo $select."<br>";

        return $result;

      }

    } else {

      if ($count) {

        $result = $this->db->fetchOne($select);

        //echo $select."<br>";

        return $result;

      }

    }

    if (is_array($join)) {

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

        //$select->join($key, $val[0], $val[1]);

        $select->joinleft($key, $val[0], $val[1]);

      }

    }

    //echo $select."<br>";

    //echo $select;exit;

    $result = $this->db->fetchAll($select);

    foreach ($result as $key => $value) {

      foreach ($value as $key2 => $value2) {

        $result[$key][$key2] = $this->convert2utf8($value2);

      }

    }

    return $result;

  }

  /**

   * 向表中插入数据

   * array $adata 数据

   * string $table 表名

   * int $insterid 是否需要返回插入ID

   * @return true or false or int

   */

   // @bianding 2013.11.04 更改了pdo中mssql.php的lastInsertId()函数

   // @bianding 2013.11.04 经测试 mssql.php中的lastInsertId()函数中的SELECT两种方式都行

  function SaveData($adata, $table, $insterid = 0, $aLog = false) {

    //$this->db = &$this->getAdapter();

    foreach ($adata as $key => $value) {

      $adata[$key] = $this->convert2gbk($value);

    }

    if ($this->db->insert($table, $adata)) {

      //var_dump($this->db->getProfiler());

      $insertedID = $this->db->lastInsertId();     

      if ($insterid) {

        return $insertedID;

      } else {

        return TRUE;

      }

    } else {

      return false;

    }

  }

  /**

   * 删除表中数据

   *

   * @param string $table 表名

   * @param string $where 'id ='.$id 条件

   * @return true or false

   */

  function DelData($table, $where, $aLog = false) {

    //$this->db = & $this->getAdapter();

    if ($this->db->delete($table, $where)) {

      return TRUE;

    } else {

      return FALSE;

    }

  }

  /**

   * 更新表中数据

   *

   * @param string $table

   * @param array $adata

   * @param string $where 'id ='.$id

   * @return true or false

   */

  function UpdateData($table, $adata, $cond, $aLog = false) {

    //$this->db = & $this->getAdapter();

    foreach ($adata as $key => $value) {

      $adata[$key] = $this->convert2gbk($value);

    }

    if ($this->db->update($table, $adata, $cond)) {

      return TRUE;

    } else {

      return false;

    }

  }

  public function clearTable($table) {

    //$this->db = &$this->getAdapter();

    $result = $this->db->query('TRUNCATE TABLE ' . $table);

  }

  public function executeSql($strSql) {

    //$this->db = &$this->getAdapter();

    $result = $this->db->query($strSql);

  }

  function convert2utf8($string)

  {

    $config = $this->getCfg();

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

    if($pdoType == 'dblib'){

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

    }elseif($pdoType == 'sqlsrv'){

      //$encode = mb_detect_encoding($string, array('UTF-8',"GB2312",'GBK','BIG5'));

      //echo $encode;

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

      //return $string;

    }

  }

  function convert2gbk($string)

  {

    $config = $this->getCfg();

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

    if($pdoType == 'dblib'){

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

    }elseif($pdoType == 'sqlsrv'){

      //$encode = mb_detect_encoding($string, array('UTF-8',"GB2312",'GBK','BIG5'));

      //echo $encode;

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

      //return $string;

    }

  }

  protected function &getCfg() {

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

      $registry = Zend_Registry::getInstance();

      $this->cfg_ = $registry->get('web_config');

    }

    return $this->cfg_;

  }

}

ログイン後にコピー

詳しい内容については、PHP 中国語 Web サイト (www.php.cn) に注目してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)