Zend 框架 - 不同的資料庫



如上一章節所述,Zend 框架提供了用於訪問資料庫的通用方式,並應用了**資料庫驅動**概念。資料庫處理完全取決於驅動資訊,因此,連線到不同資料庫時,只需更改驅動資訊即可。

現在按照以下步驟,修改**book**示例,以連線到**postgresql**資料庫。

步驟 1 − 使用以下命令在本地 postgresql 資料庫中建立一個名為 tutorials 的資料庫 −

CREATE DATABASE tutorials

步驟 2 − 新增**book**表。轉到新資料庫並執行建立表的指令碼。

\c tutorials 
CREATE TABLE book ( 
   id SERIAL NOT NULL, 
   author varchar(100) NOT NULL, 
   title varchar(100) NOT NULL, 
   PRIMARY KEY (id) 
); 

步驟 3 − 使用以下指令碼新增示例圖書資訊 −

INSERT INTO book (author, title) VALUES ('Dennis Ritchie', 'C Programming'); 
INSERT INTO book (author, title) VALUES ('James gosling', 'Java Programming'); 
INSERT INTO book (author, title) VALUES ('Rasmus Lerdorf', 'Programming PHP');

步驟 4 − 在**global.config 檔案**中更改驅動資訊。

<?php 
return array ( 
   'db' => array ( 
      'driver' => 'Pdo', 
      'dsn' => 'pgsql:dbname = tutorials;host = localhost', 
      'driver_options' => array ( 
      ), 
   ), 
); 

步驟 5 − 在**local.config**檔案中更改資料庫憑證。

return array ( 
   'db' => array( 
      'username' => '<username>', 
      'password' => '<password>', 
   ), 
);

步驟 6 − 最後,執行應用程式 **https://:8080/tutorial**。結果與 MySQL 應用程式相同。

廣告
© . All rights reserved.