views:

881

answers:

1

Hi All

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.

Cheers Stuart

+8  A: 

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:

  • Set an application-wide default for all tables:

    Zend_Db_Table_Abstract::setDefaultAdapter($db);
    
  • Specify the adapter to the table constructor:

    $table = new MyTable( array('db'=>$db) );
    
  • Store the adapter in the registry and specify it to the table or set it as default:

    Zend_Registry::set('my_db', $db); 
    $table = new MyTable( array('db'=>'my_db') );
    // alternatively:
    Zend_Db_Table_Abstract::setDefaultAdapter('my_db');
    

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

Bill Karwin
Thanks a lot Bill, that worked a treat! Thanks again
Stuart