zend-frameworkzend-dbzend-db-table

Using Zend Framework Db Tables without MVC


I am trying to use the Zend Framework without using the MVC structure, specifically the Db_Table classes.

I have created a couple of classes representing my database tables, i.e.

class DBTables_Templates extends Zend_Db_Table_Abstract  
{  
    protected $_name = "templates";  
}  

When I try to instantiate this class (it is included fine), I get the following error:

Fatal error: Uncaught exception 'Zend_Db_Table_Exception' with message 'No adapter found for DBTables_Templates'

Does anyone know how I create and include the database adapter for the Db_Table classes to use?

Any pointers are greatly appreciated! I am using the latest version of ZF.


Solution

  • You need to create a Zend_Db_Adapter, which is the class you use to connect to the database.

    $db = new Zend_Db_Adapter_Pdo_Mysql(array(
        'host'     => '127.0.0.1',
        'username' => 'webuser',
        'password' => 'xxxxxxxx',
        'dbname'   => 'test'
    ));
    

    Or you can use the factory() method to make instantiation more configurable:

    $db = Zend_Db::factory('Pdo_Mysql', array(
        'host'     => '127.0.0.1',
        'username' => 'webuser',
        'password' => 'xxxxxxxx',
        'dbname'   => 'test'
    ));
    

    See http://framework.zend.com/manual/en/zend.db.html#zend.db.adapter.connecting

    Then specify this adapter object to your table class. There are at least three ways to do this:

    See http://framework.zend.com/manual/en/zend.db.table.html#zend.db.table.constructing