【问题标题】:Database Connection in Zend FrameworkZend 框架中的数据库连接
【发布时间】:2012-02-07 11:22:43
【问题描述】:

我是采埃孚的新手。我已将我的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 文件中获取非对象的属性

【问题讨论】:

    标签: zend-framework zend-db zend-application zend-app-bootstrap


    【解决方案1】:

    使用时默认

    resources.db.*
    

    在您的 application.ini 中,将设置具有这些设置的默认适配器(并且默认情况下在 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();
    

    【讨论】:

    • 现在这不起作用..我正在编辑我的问题,请在底部看到它
    • 您编辑的内容与您关于 Zend_Db 的第一个问题无关。而是 $data->user_id;是什么给你带来麻烦,使用 Zend_Debug::dump($data);查看您要查找的数据是否实际存储在会话中,或者可能只是一个错字。
    【解决方案2】:

    Zend_Registry 是一个对象,所以你应该这样做:

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

    【讨论】:

      猜你喜欢
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多