Zend Framework Quick Start. Create a Model and Database Table

Translations of this material:

into Russian: 1.4 Zend Framework — Быстрый старт. Создание модели и таблицы базы данных. Translation complete.
Submitted for translation by antdmi 07.02.2011 Published 7 years, 1 month ago.

Text

Before we get started, let's consider something: where will these classes live, and how will we find them? The default project we created instantiates an autoloader. We can attach other autoloaders to it so that it knows where to find different classes. Typically, we want our various MVC classes grouped under the same tree -- in this case, application/ -- and most often using a common prefix.

Zend_Controller_Front has a notion of "modules", which are individual mini-applications. Modules mimic the directory structure that the zf tool sets up under application/, and all classes inside them are assumed to begin with a common prefix, the module name. application/ is itself a module -- the "default" or "application" module. As such, we'll want to setup autoloading for resources within this directory.

Zend_Application_Module_Autoloader provides the functionality needed to map the various resources under a module to the appropriate directories, and provides a standard naming mechanism as well. An instance of the class is created by default during initialization of the bootstrap object; your application bootstrap will by default use the module prefix "Application". As such, our models, forms, and table classes will all begin with the class prefix "Application_".

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:

    01. // application/models/Guestbook.php

    02.

    03. class Application_Model_Guestbook

    04. {

    05.     protected $_comment;

    06.     protected $_created;

    07.     protected $_email;

    08.     protected $_id;

    09.

    10.     public function __set($name, $value);

    11.     public function __get($name);

    12.

    13.     public function setComment($text);

    14.     public function getComment();

    15.

    16.     public function setEmail($email);

    17.     public function getEmail();

    18.

    19.     public function setCreated($ts);

    20.     public function getCreated();

    21.

    22.     public function setId($id);

    23.     public function getId();

    24. }

    25.

    26. class Application_Model_GuestbookMapper

    27. {

    28.     public function save(Application_Model_Guestbook $guestbook);

    29.     public function find($id);

    30.     public function fetchAll();

    31. }

__get() and __set() will provide a convenience mechanism for us to access the individual entry properties, and proxy to the other getters and setters. They also will help ensure that only properties we whitelist will be available in the object.

find() and fetchAll() provide the ability to fetch a single entry or all entries, while save() takes care of saving an entry to the data store.

Now from here, we can start thinking about setting up our database.

First we need to initialize our Db resource. As with the Layout and View resource, we can provide configuration for the Db resource. We can do this with the zf configure db-adapter command:

    01. % zf configure db-adapter \

    02. > 'adapter=PDO_SQLITE&dbname=APPLICATION_PATH "/../data/db/guestbook.db"' \

    03. > production

    04. A db configuration for the production has been written to the application config file.

    05.

    06. % zf configure db-adapter \

    07. > 'adapter=PDO_SQLITE&dbname=APPLICATION_PATH "/../data/db/guestbook-testing.db"' \

    08. > testing

    09. A db configuration for the production has been written to the application config file.

    10.

    11. % zf configure db-adapter \

    12. > 'adapter=PDO_SQLITE&dbname=APPLICATION_PATH "/../data/db/guestbook-dev.db"' \

    13. > development

    14. A db configuration for the production has been written to the application config file.

Now edit your application/configs/application.ini file, where you'll see the following lines were added in the appropriate sections.

    01. ; application/configs/application.ini

    02.

    03. [production]

    04. ; ...

    05. resources.db.adapter = "PDO_SQLITE"

    06. resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook.db"

    07.

    08. [testing : production]

    09. ; ...

    10. resources.db.adapter = "PDO_SQLITE"

    11. resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook-testing.db"

    12.

    13. [development : production]

    14. ; ...

    15. resources.db.adapter = "PDO_SQLITE"

    16. resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook-dev.db"

Your final configuration file should look like the following:

    01. ; application/configs/application.ini

    02.

    03. [production]

    04. phpSettings.display_startup_errors = 0

    05. phpSettings.display_errors = 0

    06. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"

    07. bootstrap.class = "Bootstrap"

    08. appnamespace = "Application"

    09. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"

    10. resources.frontController.params.displayExceptions = 0

    11. resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"

    12. resources.view[] =

    13. resources.db.adapter = "PDO_SQLITE"

    14. resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook.db"

    15.

    16. [staging : production]

    17.

    18. [testing : production]

    19. phpSettings.display_startup_errors = 1

    20. phpSettings.display_errors = 1

    21. resources.db.adapter = "PDO_SQLITE"

    22. resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook-testing.db"

    23.

    24. [development : production]

    25. phpSettings.display_startup_errors = 1

    26. phpSettings.display_errors = 1

    27. resources.db.adapter = "PDO_SQLITE"

    28. resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook-dev.db"

Note that the database(s) will be stored in data/db/. Create those directories, and make them world-writeable. On unix-like systems, you can do that as follows:

Pages: ← previous Ctrl next
1 2 3 4 5