CakePHP and SQLite: Fatal Error: Call to a member function query() on a non-object

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')); } } ?>

-------------Problems Reply------------

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.

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

Category:cakephp Views:3 Time:2011-01-24

Related post

Copyright (C), All Rights Reserved.

processed in 0.131 (s). 11 q(s)