Search code examples
modelzend-db-table

How to write a better zend model class and avoid writing again and again adapters in each method


Here is the basic code that I use for my zend framework models.

class Model_FormMapper extends Zend_Db_Table_Abstract
{ 
    protected $_name    = 'tblMapper'; 
    protected $_primary     = 'mapId';

    public function insertColumns($arrData){

  $db               = Zend_Db_Table::getDefaultAdapter();        
      $sql              = $this->insert($arrData);
      $lastId       = $this->_db->lastInsertId();
      return $lastId;
   }
}

One thing that I don't like in my models is initialisation of adpter in each method.
$db = Zend_Db_Table::getDefaultAdapter();

Can any one tell me a better solution for writing zend models.


Solution

  • I got the answer I can set a database connection variable in registry and can use any where eg: $this->_db.

    class Model_FormMapper extends Zend_Db_Table_Abstract
    { 
       protected $_name    = 'tblMapper'; 
       protected $_primary     = 'mapId';
    
    
      public function insertColumns($arrData){
    
         $sql              = $this->insert($arrData);
         $lastId           = $this->_db->lastInsertId();
         return $lastId;
      }
    }