ZendFrameWork如何与Zend_Db_Adapter_Driver_Pdo连接

时间:2013-03-29 10:04:07

标签: php zend-framework

$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

2 个答案:

答案 0 :(得分:0)

在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 :(得分:0)

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

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

希望这对你有帮助。