Zend_Db 的正确使用
我目前正在使用 Zend_Db 类来管理我的数据库连接。 我对此有几个疑问。
- 它是否智能地管理打开的连接? (例如,我已经打开了一个连接,它是否知道要使用它 - 或者我是否必须在打开新连接之前不断检查是否已经有一个打开的连接?)
- 我使用以下代码来获取结果(在 FETCH_OBJ 模式下获取):
$final = $result->fetchAll();
return $final[0]->first_name;
由于某种原因,fetchRow 不起作用 - 所以我经常使用 fetchAll,即使我只有一个结果(例如搜索 WHERE id= number
并且 id 是 PK )
我的问题是 - 当我使用 fetchAll 而不是 fetchRow 时,即使只有结果,我还要牺牲多少时间/内存?
- 我创建了以下类来管理我的连接:
require 'Zend/Db.php';
class dbconnect extends Zend_Db
{
function init ()
{
$params = array (......
return Zend_Db::factory ( 'PDO_MYSQL', $params );
}
}
然后我调用
$handle = dbconnect::init
$handle->select()....
这是最好的方法吗? 有人有更好的主意吗?
谢谢!
ps 很抱歉这里的代码格式很草率..
I'm currently using the Zend_Db class to manage my database connections.
I had a few questions about it.
- Does it manage opening connections smartly? (eg, I have a connection already open, does it know to take use of it - or do I have to constantly check if theres' already an open connection before I open a new one?)
- I use the following code to fetch results (fetching in FETCH_OBJ mode):
$final = $result->fetchAll();
return $final[0]->first_name;
for some reason, fetchRow doesn't work - so I constantly use fetchAll, even when I'll only have one result (like searching WHERE id= number
and id is a PK)
My question is - how much more time/memory am I sacrificing when I use fetchAll and not fetchRow, even when there's only result?
- I've created the following class to manage my connections:
require 'Zend/Db.php';
class dbconnect extends Zend_Db
{
function init ()
{
$params = array (......
return Zend_Db::factory ( 'PDO_MYSQL', $params );
}
}
and then I call
$handle = dbconnect::init
$handle->select()....
is this the best way? does anyone have a better idea?
Thanks!
p.s. I'm sorry the code formatting came out sloppy here..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很多问题!
是的,当您运行第一个查询时,会创建一个连接,后续查询将使用相同的连接。 如果您重复使用同一个 Zend_Db 适配器,情况就是如此。 我通常使用 Zend_Registry 将其提供给我的整个应用程序:
上面的代码通常位于您的应用程序 bootstrap 中。 我不会费心扩展 Zend_Db 类只是为了初始化它的实例。
关于 fetchRow - 我认为关键的区别在于查询运行仅限于 1 行,并且返回的对象是 Zend_Db_Table_Row 而不是 Zend_Db_Table_Rowset (如行数组),并且执行速度不会明显减慢。
fetchRow 应该没问题,所以发布一些不起作用的代码,因为某处可能有错误。
Lots of questions!
Yes, when you run your first query, a connection is created, and subsequent queries use the same connection. This is true if you're reusing the same Zend_Db adapter. I usually make it available to my entire application using Zend_Registry:
The above code usually goes in your application bootstrap. I wouldn't bother to extend the Zend_Db class just to initialise and instance of it.
Regarding fetchRow - I believe the key difference is that the query run is limited to 1 row, and the object returned is a Zend_Db_Table_Row rather than a Zend_Db_Table_Rowset (like an array of rows), and does not perform significantly slower.
fetchRow should be fine so post some code that's not working as there's probably a mistake somewhere.
对 dcaunt 答案的补充:
FetchAll 返回数组或 Zend_Db_Talbe_Rowset - 取决于您是否执行
$zendDbTableModel->fetchAll()
或$dbAdapter->fetchAll()
fetchRow()
也是如此。 如果您无法使 fetchRow 适用于模型,则使用 $model->getAdapter()->fetchRow($sqlString); 会更容易addition to dcaunt's answer:
FetchAll returns array OR Zend_Db_Talbe_Rowset - depending on if you execute
$zendDbTableModel->fetchAll()
or$dbAdapter->fetchAll()
Same goes for
fetchRow()
. If you fail to make fetchRow working for models, it's easier to use$model->getAdapter()->fetchRow($sqlString);