The Blogs Page: The index/blogs action handler
Friday, December 4, 2009
The blogs page is handled by the blogs action of the index controller. This fetches information about the blogs from a database table; it then passes this information to the view script.
The action handler is implemented by the following code:
public function blogsAction() { try { $dbBlog = new Default_Model_Db_Blog(); $this->view->blogs = $dbBlog->fetchBlogs(); } catch (Exception $e) { $this->view->errorMessage = $e->getMessage(); } }
There are five classes used to handle access to the database:
- Default_Model_Db_Blog
- Default_Model_Db_Abstract
- Default_Model_Db_Mapper_Blog
- Default_Model_Db_Mapper_Abstract
- Default_Model_Db_Table_Blog
Let us consider Default_Model_Db_Table_Blog first. This class extends Zend_Db_Table_Abstract. The implementation of the class is very simple:
class Default_Model_Db_Table_Blog extends Zend_Db_Table_Abstract { protected $_name = 'blog'; }
Setting the member variable $_name to the name of a table in the database binds the class to that table, and allows it to be used to perform operations against the table. Note that the database connection itself has been set up during the bootstrapping process, as described in the post Bootstrapping the Application: the Standard Database Resource Plugin.
The data in the rows of the blog table is handled by the Default_Model_Db_Blog class; this is linked to the blog table class by a mapper class called Default_Model_Db_Mapper_Blog. Each class contains the code specific to the blog table; they are both derived from an abstract base class which contains the generic code for these type of classes. This allows the easy addition of classes handling other database tables.
In the next couple of posts, we shall look at the implementation of first the data classes, then the mapper classes.

Practical Web 2.0 Applications with PHP (Expert's Voice) by Quentin Zervaas
Beginning Databases with PostreSQL: From Expert to Professional 2nd Edition: From Novice to Professional by Neil Matthew, Richard Stones