使用 Zend_Db 数据库连接
我正在尝试自学 Zend 框架。我有一些使用自定义框架的丰富经验,但从未使用过 Zend。这就像戴着手套尝试使用刀叉一样。
我已经启动并运行了一个系统。正在 applition.ini 文件中创建数据库连接,没有错误。
我现在的重点是尝试使用此数据库连接来执行基本 SQL。 application.ini 有以下几行:
db.adapter = PDO_MYSQL
db.params.host = localhost
db.params.username = cpanel_dbuser
db.params.password = 123456
db.params.dbname = cpanel_dbname
我正在尝试连接到 /public/index.php 中的数据库
$config = new Zend_Config_Ini(APPLICATION_PATH .
'/configs/application.ini', 'production');
$application->db = Zend_Db::factory($config->db);
Zend_Db_Table::setDefaultAdapter($db);
我得到的错误是:
Fatal error: Uncaught exception 'Zend_Db_Exception' with message 'Adapter name must be specified in a string' in /home/path/library/Zend/Db.php:226
Stack trace:
#0 /home/path/public/index.php(32): Zend_Db::factory(Array)
#1 {main} thrown in /home/path/library/Zend/Db.php on line 226
如果我 print_r Config 对象
Zend_Config Object
(
[_allowModifications:protected] =>
[_index:protected] => 0
[_count:protected] => 2
[_data:protected] => Array
(
[adapter] => PDO_MYSQL
/* ... more stuff ... */
根据我对教程和的理解,我正在使用 PDF,如果我使此连接正常工作,我将能够从控制器的 indexAction 中执行如下奇妙的操作
$data = $this->db->fetchAll(‘SELECT * FROM table’);
foreach ($data as $row) {
echo $row[‘table_fieldname’];
}
,然后开始为我拥有的表编写模型。
在这一点上,有一个非常严重的诱惑,就是想走捷径并以我已经知道的方式来做这件事,但这完全违背了学习在框架内工作的目的。
任何人都可以为我弥合差距(或者向我指出可以解决这个问题的资源)吗?
I'm trying to teach myself the Zend Framework. I have some extensive experience using custom-frameworks but have never used Zend. It's like trying to use a knife and fork with mittens on.
I've got a system up and running. A database connection is being created in the applition.ini file without errors.
The point I'm at is trying to use this connection to the database to execute basic SQL. The application.ini has the lines:
db.adapter = PDO_MYSQL
db.params.host = localhost
db.params.username = cpanel_dbuser
db.params.password = 123456
db.params.dbname = cpanel_dbname
I'm trying to connect to the database in my /public/index.php
$config = new Zend_Config_Ini(APPLICATION_PATH .
'/configs/application.ini', 'production');
$application->db = Zend_Db::factory($config->db);
Zend_Db_Table::setDefaultAdapter($db);
The error I get is:
Fatal error: Uncaught exception 'Zend_Db_Exception' with message 'Adapter name must be specified in a string' in /home/path/library/Zend/Db.php:226
Stack trace:
#0 /home/path/public/index.php(32): Zend_Db::factory(Array)
#1 {main} thrown in /home/path/library/Zend/Db.php on line 226
And if I print_r the Config Object
Zend_Config Object
(
[_allowModifications:protected] =>
[_index:protected] => 0
[_count:protected] => 2
[_data:protected] => Array
(
[adapter] => PDO_MYSQL
/* ... more stuff ... */
From my understanding of the tutorials & PDF's I'm working from, if I get this connection working I'll be able to do such fantastical amazements as the following from within a controller's indexAction
$data = $this->db->fetchAll(‘SELECT * FROM table’);
foreach ($data as $row) {
echo $row[‘table_fieldname’];
}
And then start writing models for the tables I have.
At this point, there's a pretty serious temptation to cut-the-corner and do this the way I already know how, but that completely defeats the purpose of learning to work within the framework.
Can anyone bridge the gap for me (or point me to a resource that can clear this up)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 config 目录内的 application.ini 文件中执行此操作
,无论何时何地您需要 $db 时,只需执行
此操作即可,这是最好的方法。
In application.ini file inside config directory do
and whenever/wherever you need $db just do
this is the best way to do it.
如果我没记错的话,适配器是区分大小写的。您应该使用
Pdo_Mysql
。不过,您收到的异常似乎与此无关。If I don't remember wrong, the adapters are case sensitive. You should use
Pdo_Mysql
. The exception you are receiving doesn't seems to be related to this though.