簡體   English   中英

Zend Framework中的數據庫連接

[英]Database Connection in Zend Framework

我是ZF的新手。 我在這里將我的application.ini編碼為:

[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

現在,當我更改了我的設置方式(包括bootstrap和application.ini如何實現與我相同的設置? 因為在我的應用程序中,一切都是這樣。

現在我收到此錯誤。

注意:未定義索引:DB in

C:\\ xampp \\ htdocs \\ xyz \\ application \\ controllers \\ LoginController.php,第59行
致命錯誤:在第64行的C:\\ xampp \\ htdocs \\ xyz \\ application \\ controllers \\ LoginController.php中的非對象上調用成員函數select()

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

這就是為什么我得到這個錯誤

注意:嘗試獲取我的.phtml文件中非對象的屬性

使用時默認

resources.db.*

在application.ini中,將設置具有這些設置的默認適配器(默認情況下,在Zend_Db_Table中使用)。

由於默認情況下該適配器已經在Zend_db_Table_Abstract中注冊為默認適配器,因此訪問它就像使用以下命令一樣容易:

$db = Zend_Db_Table_Abstract::getDefaultAdapter();

獲得它的另一種方法是從bootstrap-instance獲取它,它類似於以下內容(根據參考手冊 ):

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

Zend_Registry是一個對象,因此您應該這樣做:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM