繁体   English   中英

ZendFrameWork如何与Zend_Db_Adapter_Driver_Pdo连接

[英]ZendFrameWork how to connect with Zend_Db_Adapter_Driver_Pdo

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

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

在我的Bootstrap中无法连接到数据库,因为我不知道如何编写

use Zend\Db\Adapter\Driver\Pdo;

它说找不到类Zend_Db_Adapter_Driver_Pdo

在application.ini中建立连接:

//excerpt from application.ini
;-------------------------------------------------------------------------------
;Database Settings
;-------------------------------------------------------------------------------

resources.db.adapter = "pdo_Mysql"
resources.db.params.username = "user"
resources.db.params.password = "xxxxxx"
resources.db.params.dbname = "database"
resources.db.params.charset = "utf8"

然后可以在您的引导程序中将所有application.ini设置添加到注册表中:

//Bootstrap.php
protected function _initRegistry()
    {
        $config = new Zend_Config($this->getOptions());
        Zend_Registry::set('config', $config);
    }

Natrium是正确的,您需要对Zend Framework 1.x进行更多研究。 您不会马上解决这个问题。 这个框架在范围或应用上都不是简单的。

您可以使用以下代码进行检查

1)在您的application.ini文件中,添加以下内容

db.adapter = pdo_Mysql
db.params.host = host_name
db.params.username = user_name
db.params.password = pasword
db.params.dbname = database_name
db.params.profiler = true

2)接下来,在您的index.php文件中,添加以下内容

// Define path to application directory
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') : 'production'));

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

// auto loader
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);

// config
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', 'production');
$registry = Zend_Registry::getInstance();
$registry->set('config', $config);

// setup database
$db = Zend_Db::factory($config->db);
Zend_Db_Table::setDefaultAdapter($db);
$registry->set('db', $db);

希望对您有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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