Zend_Db 没有 Zend 框架

2021-12-29 00:00:00 php zend-framework zend-db

我想在没有 Zend_Framework 的情况下使用 Zend_Db.我想为我现有的不是使用 Zend Framework 制作的网站合并 Zend_Db.是否可以像这样使用 Zend_Db?你能推荐好的教程或例子吗?

I want using Zend_Db without Zend_Framework. I want incorporate Zend_Db for my existing website that was not made using Zend Framework. Is it possible to use Zend_Db like this? Can you recommend good tutorial or example how to do it good?

推荐答案

在某种程度上,这取决于您使用的 Web 框架.但是,总的来说,Zend_Db 文档在这方面.

To some extent, this depends upon the web framework you are using. But, in general, the Zend_Db documentation is pretty clear in this regard.

在您的引导程序中创建一个适配器实例.举个例子:

Create an adapter instance in your bootstrap. As an example:

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

如果您打算使用 Zend_Db_Table,那么您可以将其设为默认适配器:

If you plan to use Zend_Db_Table, then you can make this the default adapter:

Zend_Db_Table::setDefaultAdapter($db);

无论如何,将此适配器保存在您可以访问的地方会很有帮助.例如:

In any case, it's helpful to have this adapter saved someplace where you can access it. For example:

Zend_Registry::set('db', $db);

然后在您的下游代码中,使用此适配器为 select()insert()update()delete() 等:

Then in your downstream code, use this adapter to create queries for select(), insert(), update(), delete(), etc:

$db = Zend_Registry::get('db');
$select = $db->select()
    ->from('posts')
    ->where('cat_id = ?', $catId)
    ->order('date_posted DESC')
    ->limit(5);
$rows = $db->fetchAll($select);

希望这会有所帮助.干杯!

Hope this helps. Cheers!

相关文章