Search code examples
phpzend-frameworkphp-5.3zend-dbzend-db-table

How to use Data Tables and Data Mappers? Are they necessary?


I have had experience with frameworks before, but never really got into models much. I don't know if this is the case for all frameworks or just zend, but currently I am learning Zend Framework.

I have been crawling through sample applications and trying to read huge articles, but I couldn't find a short and clear answer to what confuses me.

What is the relationship between the 3 classes? (model, modelmapper, datatable)

Let's say I have a database table called users and it has 3 fields userID, userName, userPassword What would be an example code?

and is it necessary to build the model part of my application in this way? Would it be a bad practice if I just had functions to retrieve data from the database and return results as arrays?

Please consider that this is a very simple application that has users,their image galleries, and messaging functionality.

Thanks in advance.


Solution

  • For simple application and to get your feet wet in ZF start out by just using the DbTable models that tie your database table to the database adapter. This is not best practice, buit is easy and will get you started.

    using the Zend_Tool cli the command will have this format
    zf create db-table name actual-table-name module force-overwrite,
    which will translate to:

    zf create db-table Users users
    

    db this will create a file named Users.php at /application/models/DbTable/ and it will look like:

    <?php
    
    class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract
    {
        protected $_name = 'users'; //name of table, does not have to match name of class
    }
    

    now to use this in a controller to fetchAll is as simple as:

    <?php
    
    class IndexController extends Zend_Controller_Action {
    
       public function indexAction(){
           $db = new Application_Model_DbTable_Users();//instantiate model
           $result = $db->fetchAll();//perform query, see Zend_Db_Table_Abstract for API
           $this->view->users = $result;//send to view
        } 
    } 
    

    just by making this one little class you will have access to the functionality of your chosen database adapter. You can also build methods in the DbTable model to customize your access needs.

    <?php
    
    class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract
    {
        protected $_name = 'users'; //name of table, does not have to match name of class
    
        public function getUser($id) {
           $select = $this->select();//see Zend_Db_Select for info, provides more secure interface for building queries.
           $select->where('id = ?', $id);//can be chained
    
           $result = $this->fetchRow($select);//returns a row object, if array is needed use ->toArray()
           return $result;
        }
    } 
    

    This method would be used in a similar manner to the fetchAll() method:

    <?php
    
    class IndexController extends Zend_Controller_Action {
    
       public function indexAction(){
           $id = $this->getRequest()->getParam('id');//assumes the id is set somehow and posted
           $db = new Application_Model_DbTable_Users();//instantiate model
           $result = $db->getUser($id);//perform query, see Zend_Db_Table_Abstract for API
           //$this->view->user = $result;//send to view as object
           $this->view->user = $result->toArray();//send to view as an array
        } 
    } 
    

    Hope this get you started, don't forget to read the manual