访问数据库zend框架 [英] Access to database zend framework

查看:71
本文介绍了访问数据库zend框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问数据库"mysql",我读到我们可以通过在application.ini文件中写这些行来定义db适配器和db配置.

I want to access to database "mysql", I read that we can define the db adapter and db configurations by writing these lines in application.ini file

resources.db.adapter = MYSQLI
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password =123456
resources.db.params.dbname = visits_db

我想获取 $ db 对象,以便用于执行sql语句

I want to get the $db object in order to use in for executing sql statements

    $db->insert("inspection_visits_tb",
            $insepctionVisitData = array(
        'day' => $visit->getDay(),
        'date' => $visit->getDate(),
        'target' => $visit->getTarget()
    ));

我不想从application.ini文件中获取它

I want to get it from the application.ini file not like this

require_once 'Zend/Db.php';
    $db = Zend_Db::factory("MYSQLI",
                    array(
                        "host" => "localhost",
                        "dbname" => "visits_db",
                        "username" => "root",
                        "password" => "123456")
    );

因为我只想在一个地方定义数据库适配器.我应该怎么做才能得到 $ db 对象?

because I want to define the db adapter in one place only. What should I do to get the $db object??

推荐答案

像以前一样在application.ini中设置db资源.添加isDefaultAdapter选项.

Set up the db resource in application.ini like you were. Add the isDefaultAdapter option.

resources.db.adapter = "MYSQLI"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = "123456"
resources.db.params.dbname = "visits_db"
resources.db.isDefaultAdapter = true

然后获取$db对象...

protected function _initDb()
{
    $this->bootstrap('db');
    $db = $this->getResource('db');
    Zend_Registry::set('db', $db);
}

使用注册表从其他地方

$db = Zend_Db_Table::getDefaultAdapter();

使用Zend_Db_Table

$table  = new Zend_Db_Table('bugs');
$select = $table->select()...

另请参见 ZF快速入门 Zend_Db_Table

See also ZF Quickstart, Zend_Db_Table, Zend_Db_Select for more examples.

这篇关于访问数据库zend框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆