开发者

Using Zend Framework Db Tables without MVC

开发者 https://www.devze.com 2022-12-08 00:34 出处:网络
I am trying to use the Zend Framework without using the MVC structure, specifically the Db_Table classes.

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 = "te开发者_JS百科mplates";  
}  

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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消