2
votes

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.

1
You did not share your code with us. especially the model which tries to access the database. I bet you forgot public $useTable => false there.mark
@mark thank you, but it seems you did not read my question carefully.Mirko Pagliai
maybe you didnt set it properly. Thats why it is vital to post the code. Also note, that this could have been fixed in a later cake version. Please try your code with the current 2.3 master branch and confirm that this "issue" still exists there.mark
@mark, there'is no code to see. My application now works correctly without models and database, but the file Config/database.php must be present, even if empty. If there is not, I get 2 warnings. If I create a model, completely empty and only with the property $useTable set to false, I get the error that the database is not configured. I'm using Cake 2.3.1, sorry for the wrong tag.Mirko Pagliai
@mark, you can try this also with an empty app. You know the PagesController in your application you just downloaded? Download Cake, set tmp/ as writable and open the homepage. Look down. You'll see that he still wants the database file.Mirko Pagliai

1 Answers

4
votes

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.