Zend_Db: Adapter suddenly is null!

2019-08-14 03:45发布

问题:

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).

It seems like the adapter is set to null somewhere during the script execution. How can that happen?

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

回答1:

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');
    

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



回答2:

Well, that's not too much information. If you want to avoid guessing, I'd suggest to make a copy of the Zend Framework and add some tracer code to those methods that can set $_db using php's built-in debug functions, such as:

  • debug_backtrace()
  • debug_print_backtrace()

to find out what's going on. Afer all, it's open-soure!