Mirko Pagliai
Mirko Pagliai

Reputation: 1241

CakePHP Model with $useTable = false still tries to connect to database

I'm trying to develop an application that does not use a database, but that still has some models.

First problem: Cake look however for the configuration of the database (APP/Lib/Cake/Model/ConnectionManager.php, line 69), it tries to include the file Config/database.php.

If the file doesn't exist, I get 2 warnings. If it exists (even if it is completely empty) all ok. I want to clarify that this takes place even in the absence of models.

Second problem: if I try to use a model, I get the error:

Missing Datasource Configuration
Error: The datasource configuration default was not found in database.php.

This happens whether the Config/database.php file exists, whether the file is not present.

Finally, I specify that models (including the AppModel) have the property $useTable set to false, but it seems that it makes no difference.

But I have noticed that everything works correctly when in an application that uses a database there are only a few models that do not use it (but the app as a whole uses it).

I found this which proposes a solution, which then is the solution that is found on the web.

But below, in the comments, I read that Lorenzo says:

This does not make any sense at all, CakePHP 2.0 will not try to perform any datbase connection unless you request it to do so

It seems to me that it has not, maybe CakePHP does not perform any query but still want a database is configured. Is that so? Or I missed something?

Thank you.

Upvotes: 2

Views: 3402

Answers (1)

thaJeztah
thaJeztah

Reputation: 29007

Possible causes

CakePHP doesn't try to connect to the database until it is actually required, however;

  • The app/Config/database.php file should always be present, even if you're not using a database. However, you don't have to 'configure' the various connections if you're not using them
  • If your models use behaviors, it's possible that database-calls are made by the behavior, causing CakePHP to attempt to connect to the database
  • Various methods inside the Model class will make connections to the database (e.g. requesting the 'schema' or 'columnType'.
  • Non-existing Model-methods will be mapped to a magic __call() method; This method will first check if a Behavior is able to handle the call, and otherwise will make a call to the database. E.g. using $this->Mymodel->doSomething() will cause CakePHP to attempt to open a database connection and execute doSomething in the database.
  • If a Model doesn't exist, CakePHP will create one, basically, it creates an instance of Model, using schema information from the database, causing a connection to be opened.

Example

class PostsController extends AppController {
    // This controller does not use a Model
    public $uses = false;

    public function index() {
        // CakePHP will construct a 'Post' model here
        $this->Post->find('something');
    }

}

Datasources, even if no database is used

Having said that. In most cases, it's best to have your models use a datasource; A datasource does not have to be a database, but may also be, for example, a Twitter Feed or a static Array().

Having a datasource simplifies most tasks (e.g. you can just perform find(....) to get the data from your source, whether that is a database or not.

Upvotes: 4

Related Questions