Zend Framework中的数据库连接
我是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
默认在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();
的合适的词汇是一个对象,所以你应该做的:
$registry = Zend_Registry::getInstance();
$DB = $registry->DB;
现在这不是我working..i编辑我的问题PLZ看到它在底部 – 2012-02-07 12:04:59
什么您已经编辑是不是与您约Zend_Db的第一个问题。相反,$ data-> user_id;是什么导致你麻烦,使用Zend_Debug :: dump($ data);查看您要查找的数据是否实际存储在会话中,或者只是一个错字。 – dbrumann 2012-02-09 07:55:37