5

I am trying to build a simple app with CakePHP 2.1.1 using SQLite3 as the database. To save time I tried to use the bake tool to create a model for the following table:

CREATE TABLE animals (
  id integer primary key autoincrement,
  name text
);

but the bake tool returns the following error: Your database does not have any tables.

I figured Cake had a problem connecting to the database, so I went ahead and created the appropriate model, controller, and views myself. I inserted a single record into the animals table. And it worked.

I came up with nothing after searching the web. Either nobody tried to use the bake tool on an SQLite3 database, or I am having bad luck.

Does anyone have any ideas?

UPDATE

Here's the output of cake bake:

johan@ubuntu:~/php/app$ Console/cake bake model

Welcome to CakePHP v2.1.1 Console
---------------------------------------------------------------
App : app
Path: /home/johan/php/app/
---------------------------------------------------------------
---------------------------------------------------------------
Bake Model
Path: /home/johan/php/app/Model/
---------------------------------------------------------------
Your database does not have any tables.

and the config file:

<?php
class DATABASE_CONFIG {
    public $default = array(
        'datasource' => 'Database/Sqlite',
        'persistent' => false,
        'host' => 'localhost',
        'database' => 'cake',
    );
}

The database file is located at ~/php/app/webroot/cake

tereško
  • 58,060
  • 25
  • 98
  • 150
Johan A.
  • 53
  • 4
  • Did you check your log files for any errors? What happens when you type `cake bake model`, does it give you any errors? Are you 100% sure your database is accessible by cake? – dr Hannibal Lecter Apr 29 '12 at 18:57
  • I checked the log files from CakePHP and Apache and did not see anything out of the ordinary. `cake bake model` does not error out either. It proceeds to tell me that the database does not have any tables. I am 100% sure that the database is accessible from within my CakePHP application because I was able to insert records. – Johan A. Apr 29 '12 at 19:13
  • Are you running `cake bake` in your app folder or somewhere else? What does your Config/database.php look like? Where did you put your database? – dr Hannibal Lecter Apr 29 '12 at 19:27
  • I will update my question with answers to yours. I'm curious, though, have you ever been able to bake models from an SQLite3 database? – Johan A. Apr 29 '12 at 19:50
  • I have, and it works. Your setup is strange. First, having your database in webroot is quite a security risk, you might want to reconsider that. Secondly, try setting the database param in your config to a full path. If that doesn't work, we'll have to investigate further :) – dr Hannibal Lecter Apr 29 '12 at 20:01
  • Your second tip worked! Post an answer for your upvote :) Back in CakePHP 1.3 the location of the SQLite database was relative to the webroot directory and that's why I put the database in the webroot directory in the first place. I'm aware of the security issues and it was only for development purposes. In reality I was just being lazy and hoped to just copy the config file straight to production and not have a separate config file. – Johan A. Apr 29 '12 at 20:27

1 Answers1

7

Try putting a full path into your database config, this is what I did in my app:

<?php
define('DEFAULT_DB', TMP.'db'.DS.'main.db3');

class DATABASE_CONFIG {
    public $default = array(
        'datasource' => 'Database/Sqlite',
        'persistent' => false,
        'host' => '',
        'database' => DEFAULT_DB,
        'encoding' => 'utf8',
    );
}
dr Hannibal Lecter
  • 6,665
  • 4
  • 33
  • 42
  • I appreciate your help. I accepted your answer and will return to upvote it once I have enough reputation. – Johan A. Apr 29 '12 at 20:35
  • Currently prototyping on Windows, CakePHP 3.8, path ended up looking like this: 'database' => 'C:\Users\Me\Downloads\phptest\app\webroot\test.db', ... without this, I got the same results as OP. – TechFanDan Nov 16 '19 at 18:07