简体   繁体   中英

Database Connection in Zend Framework

I am new to ZF. I have coded my application.ini here it is:

[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"

here is my 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();

and here is my 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);
   }
}

now firstly i did my connection like this

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

Later in my queries I used this to retrieve records:

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

Now as I have changed my setting mean including bootstrap and application.ini how to achieve the same as I did? Because in my app everything is like that..

Now I am getting this error.

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

Line #59 is

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

Line #64 is

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

Edited

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

Now my query can't find $user_id here

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

That's why I am getting this error

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

By default when using

resources.db.*

in your application.ini a default adapter with these settings will be set up (and by default be used in Zend_Db_Table).

As the adapter is already registered as default adapter in Zend_db_Table_Abstract by default, accessing it is as easy as using:

$db = Zend_Db_Table_Abstract::getDefaultAdapter();

Another way to get it is to fetch it from the bootstrap-instance, which goes something like this (according to Reference Manual ):

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

The Zend_Registry is a object so you should be doing:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM