问题描述
我在我的一个项目中使用 Zend_Db.现在我遇到了问题,在代码执行过程中, Zend_Db_Adapter_Abstract 中的变量 $_db 突然为空.(由 var_dump($this); 在我的 DbTable_xx 类中显示).
I'm using Zend_Db in one of my projects.Now I have the problem, that suddenly during the code execution, the variable $_db in Zend_Db_Adapter_Abstract is null. (shown by var_dump($this); in my DbTable_xx class).
似乎适配器在脚本执行期间的某处设置为 null.怎么会这样?
It seems like the adapter is set to null somewhere during the script execution.How can that happen?
不幸的是,该项目太复杂,无法在此处发布一些代码...我收到此错误(在 Zend_Db_Adapter_Abstract 上执行 find($primary) 方法时):
Unfortunately the project is too complex to post some code here... I'm getting this error (while executing the find($primary) method on Zend_Db_Adapter_Abstract):
Fatal error: Call to a member function quoteTableAs()
on a non-object in xxx/library/Zend/Db/Table/Abstract.php on line 1162
推荐答案
您可能忘记为 Zend_Db_Table 类设置 Db 适配器.
You probably forgot to set the Db adapter for your Zend_Db_Table class.
您至少可以通过三种方式做到这一点:
You can do this in at least three ways:
为所有表设置应用程序范围的默认值:
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');
参见http://framework.zend.com/manual/en/zend.db.table.html#zend.db.table.constructing
这篇关于Zend_Db:适配器突然为空!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!