創建模型和數據表
在我們開始之前,讓我們來思考幾點:這些類將放在什麼地方,我們如何找到它們?我們創建的默認的項目實例化一個自動加載(autoloader)。我們可以給它附加其他的自動加載(autoloader),所以它知道在哪裡可以找到不同的類。典型的,我們想讓我們各種各樣的MVC類歸入同一個目錄樹――在這個例子中是application/ ――並且大多數經常使用共同的前綴(common prefix)。
Zend_Controller_Front 有一個“模塊(modules)”概念,它是一個單獨的小應用程序。模塊模擬在application/目錄下用zf工具建立的目錄結構,且裡面所有的類假定有相同的前綴――模塊名。“Application/”本身就是一個模塊――“default”或者“application” 模塊。因此,我們將去設置在此目錄下資源的自動導入。
Zend_Application_Module_Autoloader提供將模塊下各種資源映射到相應目錄的功能,並且也提供一個標准的命名機制。在引導對象(bootstrap object)初始化時,這個類的實例被默認創建。你的應用程序引導程序(bootstrap)將默認的使用“Application” 作為模塊前綴。這樣,我們的模型、表單和數據表類將以“Applicatin_”為前綴。
Now, let's consider what makes up a guestbook. Typically, they are simply a list of entries with a comment, timestamp, and, often, email address. Assuming we store them in a database, we may also want a unique identifier for each entry. We'll likely want to be able to save an entry, fetch individual entries, and retrieve all entrIEs. As such, a simple guestbook model API might look something like this:
現在,我們來看看是什麼組成了留言簿,典型的,他們簡單的由評論(comment),戳(timestamp),經常還包括郵件地址的條目列表。假若將他們存儲在數據庫中,還要為每一個條目建立一個唯一的標識符。我們想要保存條目、查詢單個條目,檢索所有條目。因此,一個簡單的留言薄模型API可能看起來像這樣:
// application/models/Guestbook.PHP
class Application_Model_Guestbook
{
protected $_comment;
protected $_created;
protected $_email;
protected $_id;
public function __set($name, $value);
public function __get($name);
public function setComment($text);
public function getComment();
public function setEmail($email);
public function getEmail();
public function setCreated($ts);
public function getCreated();
public function setId($id);
public function getId();
}
class Application_Model_GuestbookMapper
{
public function save(Application_Model_Guestbook $guestbook);
public function find($id);
public function fetchAll();
}
__get()和__set()將提供一個提供了一個便捷的機制去訪問單個條目屬性,代理其他的getter和setter方法(proxy to the other getters and setters),他們也幫助確保對象中只有白名單屬性才是可用的
Find()和fetchAll()提供獲取單個條目和能力,而save()負責將條目存儲到數據庫。
從現在開始,我們能夠開始建立我們的數據庫
首先我們需要初始化我們的數據源。就像布局和視圖資源那樣,可以為數據源提供配置。我們可以使用“zf configure db-adapter”命令:
% zf configure db-adapter \
> 'adapter=PDO_SQLITE&dbname=APPLICATION_PATH "/../data/db/guestbook.db"' \
> production
A db configuration for the production has been written to the application config file.
% zf configure db-adapter \
> 'adapter=PDO_SQLITE&dbname=APPLICATION_PATH "/../data/db/guestbook-testing.db"' \
> testing
A db configuration for the production has been written to the application config file.
% zf configure db-adapter \
> 'adapter=PDO_SQLITE&dbname=APPLICATION_PATH "/../data/db/guestbook-dev.db"' \
> development
A db configuration for the production has been written to the application config file.
現在編輯你的application/configs/application.ini file文件,在相應的部分你將會看到下面的行被加進去。
; application/configs/application.ini
[production]
; ...
resources.db.adapter = "PDO_SQLITE"
resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook.db"
[testing : production]
; ...
resources.db.adapter = "PDO_SQLITE"
resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook-testing.db"
[development : production]
; ...
resources.db.adapter = "PDO_SQLITE"
resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook-dev.db"
最後的配置文件看起來像這樣:
; application/configs/application.ini
[production]
PHPSettings.display_startup_errors = 0
PHPSettings.display_errors = 0
bootstrap.path = APPLICATION_PATH "/Bootstrap.PHP"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.vIEw[] =
resources.db.adapter = "PDO_SQLITE"
resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook.db"
[staging : production]
[testing : production]
PHPSettings.display_startup_errors = 1
PHPSettings.display_errors = 1
resources.db.adapter = "PDO_SQLITE"
resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook-testing.db"
[development : production]
PHPSettings.display_startup_errors = 1
PHPSettings.display_errors = 1
resources.db.adapter = "PDO_SQLITE"
resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook-dev.db"
注意數據庫將被存儲在“data/db/”目錄下。創建這些目錄並且使它們是可寫的。在類unix系統中,你可以像下面這麼做:
% mkdir -p data/db; chmod -R a+rwX data
在wendows系統中,你需要在資源管理器中創建目錄並設置對任何人的寫權限。
這裡我們有一個到數據庫的連接,它連接到位於application/data/目錄下的sqlite數據庫。我們來設計一個簡單的數據表用於存儲我們的留言板條目。
-- scripts/schema.sqlite.sql
--
-- You will need load your database schema with this SQL.
CREATE TABLE guestbook (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
email VARCHAR(32) NOT NULL DEFAULT '[email protected]',
comment TEXT NULL,
created DATETIME NOT NULL
);
CREATE INDEX "id" ON "guestbook" ("id");
為使我們的工作有創意的(so that we can have some working data out of the box),我們來創建一些數據行使我們的應用程序更加有趣
-- scripts/data.sqlite.sql
--
-- You can begin populating the database with the following SQL statements.
INSERT INTO guestbook (email, comment, created) VALUES
('[email protected]',
'Hello! Hope you enjoy this sample zf application!',
DATETIME('NOW'));
INSERT INTO guestbook (email, comment, created) VALUES
('[email protected]',
'Baz baz baz, baz baz Baz baz baz - baz baz baz.',
DATETIME('NOW'));