CakePHP 和 SQLite:致命错误:调用非对象上的成员函数 query()
我正在按照 CakePHP 网站上的教程进行操作,但决定使用 SQLite 数据库。我创建了一个表,并将数据库文件放在其余站点文件所在的位置。
但是,当调用 ~username/public_html/site/speakers/ 时,出现以下错误:
致命错误:在 /home/username/public_html/site/cake 中的非对象上调用成员函数 query() /libs/model/datasources/dbo/dbo_sqlite3.php 第 133 行
扬声器控制器的源代码:
<?php
class SpeakersController extends AppController {
var $name = 'Speakers';
var $helpers = array ('Html','Form');
function index(){
$this->set('speakers', $this->Speaker->find('all'));
}
}
?>
I'm following through the tutorial on the CakePHP website, but decided to use an SQLite database instead. I made a single table and put the database file where the rest of the site files are.
However, when calling ~username/public_html/site/speakers/, I get the following error:
Fatal error: Call to a member function query() on a non-object in /home/username/public_html/site/cake/libs/model/datasources/dbo/dbo_sqlite3.php on line 133
The source code for SpeakersController:
<?php
class SpeakersController extends AppController {
var $name = 'Speakers';
var $helpers = array ('Html','Form');
function index(){
$this->set('speakers', $this->Speaker->find('all'));
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看可用的核心数据源,没有一个名为
sqlite3
。我怀疑 数据库配置正在提示 CakePHP 尝试加载此文件。请检查您是否已像这样定义了数据库连接:好的,这是(现在)我对情况的理解:
对于 SQLite 2.x,PHP 使用由PHP 的 SQLite 数据库扩展。这是核心 CakePHP
sqlite
数据源当前支持的功能。 (注意:查看下面的评论,看来您当前没有启用此 PHP 扩展。)对于 SQLite 3.x,PHP 使用 PHP 的
SQLite3()
类提供。 a href="http://php.net/manual/en/book.sqlite3.php" rel="nofollow">SQLite3 数据库扩展。当前核心 CakePHPsqlite
数据源和社区提供的sqlite3
数据源均不支持此功能。但是,社区提供的
sqlite3
数据源使用 PHP 较新的 PDO 扩展 和 SQLite PDO 驱动程序。因此,尽管不受 CakePHP 支持,但您似乎希望使用
sqlite3
驱动程序以避免使用不受支持的 SQLite 版本。 :)无论如何,您的问题中出现错误的原因已在 的最后评论中进行了解释您链接到的 Trac 中的票证。该票证已成为 迁移到 Lighthouse 并标记为
wont-fix
。不过,数据源已进入 GitHub 上的社区数据源插件,但看起来主要是未经测试和提交历史记录似乎并不表明问题已得到解决。
无论如何,我都会下载这个最新版本的数据源,但您的问题似乎特别存在,因为
new PDO()
调用(现在是 第 167 行)不会返回PDO
对象,因为它失败连接到数据库。希望这能为您提供调试问题的起点。尝试加入
debug($this->config); debug($this->connection);
检查出了什么问题。Looking at the available core datasources, there isn't one called
sqlite3
. I suspect the database config is prompting CakePHP to attempt to load this file. Please check that you have defined your database connection like so:Okay, this is (now) my understanding of the situation:
For SQLite 2.x, PHP uses the
sqlite_xxx()
functions provided by PHP's SQLite database extension. This is what is currently supported by the core CakePHPsqlite
datasource. (Note: looking at your comment below, it seems you don't currently have this PHP extension enabled.)For SQLite 3.x, PHP uses the
SQLite3()
class provided by PHP's SQLite3 database extension. This is unsupported by both the current core CakePHPsqlite
datasource and the community-providedsqlite3
datasource.However, the community-provided
sqlite3
datasource uses thePDO()
class provided by PHP's newer PDO extension and the SQLite PDO driver.So, although unsupported by CakePHP, it looks like you want to use the
sqlite3
driver as to avoid using an unsupported version of SQLite. :)Anyway, the reason the error in your question appears has been explained in the last comment of the ticket in Trac you linked to. The ticket has since been migrated over to Lighthouse and marked as
wont-fix
.The datasource, however, has made it into the community Datasources plugin on GitHub, but looks largely untested and the commit history doesn't appear to suggest the issue has been fixed.
I would download this latest version of the datasource anyway, but your issue seems to exist specifically because the
new PDO()
call (on what is now line 167) doesn't return aPDO
object because it fails to connect to the database.Hopefully this should give you a starting point for debugging the problem. Try sprinkling in a
debug($this->config); debug($this->connection);
to inspect what is going wrong.看来您没有为 php 启用 sqlite。试试这个:
安装sqlite3:
安装php5-sqlite
It looks that you don't have sqlite enabled for php. Try this:
install sqlite3:
install php5-sqlite