正在開發一個Zend應用程序,我需要保留數據庫對象的單個實例,並在當前實例斷開連接時重新連接。下面是代碼:如何檢查Zend_Db_Adapter_Pdo_Mysql對象是否連接到數據庫
class Resource_PdoMysql extends Zend_Application_Resource_ResourceAbstract
{
const KEY = 'PDO_MYSQL';
private static function connect()
{
$connParams = array("host" => host,
"port" => 'port',
"username" => 'username',
"password" => 'password',
"dbname" => 'dbname');
$db = new Zend_Db_Adapter_Pdo_Mysql($connParams);
return $db;
}
public static function getConnection()
{
if (!Zend_Registry::isRegistered(self::KEY))
{
$db = self::connect();
Zend_Registry::set(self::KEY, $db);
}
return Zend_Registry::get(self::KEY);
}
public static function reconnect()
{
$db = self::connect();
Zend_Registry::set(self::KEY, $db);
}
public function init()
{
return self::getConnection();
}
}
Am using $db like this
$db = Resource_PdoMysql::getConnection();
// <Here I need to check if the connection is open before proceeding>
$db->insert('table', $data);
嘗試運行一些簡單的東西,比如'select 1'。如果沒有出錯,則連接打開。 –
感謝您的回覆Marc。 –