1

我是ZF的新手。我已經編寫我application.ini那就是:Zend Framework中的數據庫連接

[development] 
includePaths.library = APPLICATION_PATH "/../library" 
bootstrap.path = APPLICATION_PATH "/Bootstrap.php" 
bootstrap.class = "Bootstrap" 
appnamespace = "Application" 
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers" 
resources.frontController.params.displayExceptions = 0 
resources.view.encoding = "utf-8" 
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts" 


phpSettings.display_startup_errors = 1 
phpSettings.display_errors = 1 
phpSettings.display_startup_errors = 1 
phpSettings.display_errors = 1 
resources.frontController.params.displayExceptions = 1 
resources.DB.adapter   = "pdo_mysql" 
resources.DB.params.host  = "localhost" 
resources.DB.params.username = "root" 
resources.DB.params.password = "" 
resources.DB.params.dbname  = "xyz" 

,這裏是我的index.php

   defined('APPLICATION_PATH') 
       || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application')); 

      // Define application environment 
      defined('APPLICATION_ENV') 
       || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development')); 

      // Ensure library/ is on include_path 
      set_include_path(implode(PATH_SEPARATOR, array(
       realpath(APPLICATION_PATH . '/../library'), 
       get_include_path(), 
     ))); 

      /** Zend_Application */ 
      require_once 'Zend/Application.php'; 

      // Create application, bootstrap, and run 
      $application = new Zend_Application(
       APPLICATION_ENV, 
       APPLICATION_PATH . '/configs/application.ini' 
     ); 
      $application->bootstrap()->run(); 

這裏是我的bootstrap.php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap{ 
protected function _initDoctype(){ 
    $this->bootstrap('view'); 
    $view = $this->getResource('view'); 
    $view->doctype(Zend_View_Helper_Doctype::HTML5); 
    } 
} 

現在首先我做了我的連接,如此

$params = array('host'   => 'localhost', 
      'username' => 'root', 
      'password' => '', 
      'dbname'  => 'xyz' 
     ); 

    $DB  = new Zend_Db_Adapter_Pdo_Mysql($params); 
     $DB->setFetchMode(Zend_Db::FETCH_OBJ); 
     Zend_Registry::set('DB',$DB); 

後來在我的疑問我用這個來檢索記錄:

$registry = Zend_Registry::getInstance(); 
$DB = $registry['DB']; and than my query 

現在,我已經改變了我的設置意味着包括引導和application.ini如何實現像我一樣一樣的嗎?因爲在我的應用程序中,一切都是這樣的。

現在我收到了這個錯誤。

Notice: Undefined index: DB in

C:\xampp\htdocs\xyz\application\controllers\LoginController.php on line 59
Fatal error: Call to a member function select() on a non-object in C:\xampp\htdocs\xyz\application\controllers\LoginController.php on line 64

線#59是

$registry = Zend_Registry::getInstance(); 
$DB = $registry['DB']; 

線#64

$select = $DB->select() 
    ->from(array('p' => 'phone_service')) 
    ->join(array('u' => 'user_preferences'), 'u.phone_service_id = p.phone_service_id') 
    ->where('u.user_preferences_name = ?', 'is_user_package_active') 
    ->where('p.user_id = ?', $user_id); 

編輯

if($result->isValid()){ 
$data = $authAdapter->getResultRowObject(null,'password'); 
$auth->getStorage()->write($data); 
$this->_redirect('/login/controlpannel');} 

現在我的查詢無法找到$user_id這裏

$data = Zend_Auth::getInstance()->getStorage()->read(); 
$user_id = $data->user_id; 
    $DB = Zend_Db_Table_Abstract::getDefaultAdapter(); 

$select = $DB->select() 
    ->from(array('p' => 'phone_service')) 
    ->join(array('u' => 'user_preferences'), 'u.phone_service_id = p.phone_service_id') 
    ->where('u.user_preferences_name = ?', 'is_user_package_active') 
    ->where('p.user_id = ?', $user_id); 

這是使用

resources.db.* 
在你的application.ini

時使用這些設置的默認適配器會成立(並且默認爲什麼我收到此錯誤

Notice: Trying to get property of non-object in in my .phtml file

回答

0

默認在Zend_Db_Table中使用)。

由於適配器默認情況下,已註冊爲默認適配器Zend_db_Table_Abstract,訪問它是那麼容易,因爲使用:

$db = Zend_Db_Table_Abstract::getDefaultAdapter(); 

另一種方式得到它是從自舉實例,該實例去把它拿來像這樣(根據Reference Manual):

$front = Zend_Controller_Front::getInstance(); 
$bootstrapResource = $front->getParam('boostrap')->getPluginResource('db'); 
$db = $boostrapResource->getDbAdapter(); 
+0

現在這不是我working..i編輯我的問題PLZ看到它在底部 – 2012-02-07 12:04:59

+1

什麼您已經編輯是不是與您約Zend_Db的第一個問題。相反,$ data-> user_id;是什麼導致你麻煩,使用Zend_Debug :: dump($ data);查看您要查找的數據是否實際存儲在會話中,或者只是一個錯字。 – dbrumann 2012-02-09 07:55:37

0

的合適的詞彙是一個對象,所以你應該做的:

$registry = Zend_Registry::getInstance(); 
     $DB = $registry->DB;