jump to navigation

Zend Framework :Connecting to a Database with the Table Data Gateway Patern April 20, 2011

Posted by Tournas Dimitrios in Zend Framework v1.10.

My previous article demonstrated how to connect to a database using a Reusable Database Handler . Let’s take a different route to make a Database connection , we will use a “simple” pattern Table Data Gateway . The concept , from a very general view , is simple . We create a separate Class for each table of the Database that we want to access . Each of these Classes will be a separate module inside the models directory and the credentials for connecting to the database will be “placed” in the general configuration file of the application ( APPLICATION-ROOT/application/configs/application.ini ) . We could of course manually create the directory structure – Class and Database credentials , but Zend provides us the “zf tool ” to automate the process . Let’s pretend that the name of the Database is “test” and the name of the table we want to access is “users” .

  • Creating a module “Users” :
    zf  create  db-table  users   users
    The zf create db-table  takes in two parameters:

    • ClassName – the name of the class
    • database_table – the name of the table

    This will create in the Models directory a directory DbTable  and inside this dir a Class “Users” , this class  extends Zend_Db_Table_Abstract and provides us all the functions to manipulate the table (CRUD) .

    class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract
        protected $_name = 'users';

    A good practice is to name the Class and the protected variable to the table that we want to access . As we will create one Class for each table we want to have access , that makes identifying our code more easy .

  • Configuring the application.ini file with the Database credentials : zf configure dbadapter “adapter=pdo_mysql&username=root&password=”&dbname=test&hostname=localhost” productionOur application’s configuration file will be updated with the following Database credentials :
    resources.db.adapter = "pdo_mysql"
    resources.db.params.username = "root"
    resources.db.params.password = ''
    resources.db.params.dbname = "test"
    resources.db.params.hostname = "localhost"


  • The next step is to configure the index controller action
    $users  = new Application_Model_DbTable_Users();
    $data  = $users->fetchAll($users->select());
    $this->view->data = $data->toArray();
  • The last step is to configure the view
    echo "

A practical example with the TDG   here >>>>>



1. suminho - April 24, 2011

Very nice. 🙂 I’ve just started to learn about db-table and I was not realizing why should we use db-table and not zf create model for example. Well, thanks to you I have a more clear distinction and role of each of those commands. Thanks a lot. 🙂

2. suminho - April 25, 2011

Btw, don’t you feel the need to add a Data Mapper between your data source and the model ?


tournasdimitrios1 - April 25, 2011

Thanks for your comments .
The aim of this article was to just show a newcomer how the Table Data Gateway pattern is bind inside the MVC framework . I didn’t pay attention to proper design patterns that should be followed to perform the separation .
The only database design pattern available from the Zend Framework that works ” out of the box ” is the Table Data Gateway , which allows for a direct communication to a table . But it requires a direct ( 1:1 ) relation with the fields of the table . Unfortunately that’s not going to work in real world applications , because the data spans multiple tables in a database . So we have to write the data mapper manually . But that is a subject for a future article 🙂

3. suminho - April 25, 2011

@tournasdimitrius: I see… That makes so much sense. I was using, until today, a DAO/VO that suffers from the same problem. I had a table per dao/vo. It was a nice organization approach but problems started to rise when I had to work with some joins, and cross table relations broadly speaking. What I did was to pick one (convention over configuration) of those tables, and create my methods there, or create a method called “table1table2join” ugly ugly. 🙂 But, as you point, even this will not work with TDG “out of the box” that Zend provides. So a mapper should be created. All mappers we create will extend Zend_Db, if we are building something with 200 tables, that Zend_Db extended (or called) 200 times may be a very heavy choice. So perhaps creating an abstract mapper method that all mappers could extend, that may be a nice approach hm? Oh my… 🙂 Well, I will way for your next article, because this one WAS indeed at least at some point, a break point to me. Thanks again! 🙂 (ps- if I’m saying something silly, or if I miss understood something, please let me know, better to not knowing is to know bad). Cheers!

tournasdimitrios1 - April 26, 2011

Well I’m still learning the Zend Framework , so take my response to your post , just as a opinion .

While Zend_Table_Abstract performs the same functionality as Zend_Db_Adapter_Abstract ( CRUD) , it is used completely differently ( it works on a higher layer of abstraction ) . For example both Classes implement the “FetchAll() ” method , look at the signature of this method .

/* Zend_Db_Table
public function fetchAll($where = null, $order = null, $count = null, $offset = null)

public function fetchAll($sql, $bind = array(), $fetchMode = null)

While Zend_Db_Adapter expect from us to specify a well – defined sql statement , Zend_Db_Table Class just expect from us to specify the parts we are interested in , and it will handle the rest for us . Now the interesting part where Zend_Db_Table wins the golden metal , is when it handles table-relationship . For handling this relationship ( one to many , many to many ) it makes use of some built-in functionality .

On my desktop , I have some notes related to this subject . But it’s still difficult to me to create an article that will be useful , as a starting point , for a newcomer to the Zend Framework . I will tray in future articles to finish this topic .

suminho - May 1, 2011

@tournasdimitrios1: I’m trying to create a simple CRUD application using Data Mapper and Gateway patterns. While doing this, I’m writting things out. Let’s see what it leads. I will let you know when I finnished it. So that perhaps that could help us under the learning process of this Framework.

Cheers, and thanks for your time.

tournasdimitrios1 - May 1, 2011

Well it sounds a brilliant idea

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s