CakePHP 和 SQLite:致命错误:调用非对象上的成员函数 query()

发布于 2024-10-13 06:12:29 字数 533 浏览 1 评论 0原文

我正在按照 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗弃M 2024-10-20 06:12:29

查看可用的核心数据源,没有一个名为sqlite3。我怀疑 数据库配置正在提示 CakePHP 尝试加载此文件。请检查您是否已像这样定义了数据库连接:

var $default = array(
    'driver' => 'sqlite', // not 'sqlite3'
    'database' => '../database_name.sqlite', // db in /app directory
);

好的,这是(现在)我对情况的理解:

  • 对于 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 数据库扩展。当前核心 CakePHP sqlite 数据源和社区提供的 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:

var $default = array(
    'driver' => 'sqlite', // not 'sqlite3'
    'database' => '../database_name.sqlite', // db in /app directory
);

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 CakePHP sqlite 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 CakePHP sqlite datasource and the community-provided sqlite3 datasource.

  • However, the community-provided sqlite3 datasource uses the PDO() 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 a PDO 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.

七度光 2024-10-20 06:12:29

看来您没有为 php 启用 sqlite。试试这个:
安装sqlite3:

sudo aptitude install sqlite3

安装php5-sqlite

sudo aptitude install php5-sqlite

It looks that you don't have sqlite enabled for php. Try this:
install sqlite3:

sudo aptitude install sqlite3

install php5-sqlite

sudo aptitude install php5-sqlite
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文