Zend Framework - 不同的数据库
如上一章所述,Zend 框架提供了一种使用 数据库驱动程序 概念访问数据库的通用方法。使用数据库完全取决于驱动程序信息,因此,连接不同的数据库只需更改驱动程序信息即可。
现在让我们按照以下步骤更改 book 示例以连接到 postgresql 数据库。
步骤 1 − 使用以下命令在本地 postgresql 数据库中创建数据库、教程 −
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 − 最后,运行应用程序 http://localhost:8080/tutorial。结果与 MySQL 应用程序相同。