Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How we setup cakephp without database?

Tags:

cakephp-2.1

In few project we don't need database, so how we setup cakephp on local machine without modification in database config? Right now what I done ...I created database and modified config file. But my database has no table, its just wastage of database....so please suggest better way to do this.

Thank you in advance..

like image 410
Pank Avatar asked Mar 16 '13 12:03

Pank


2 Answers

With CakePHP 2.3.x I simply use an empty string as a datasource in the database configuration and it works fine.

The database configuration (app/Config/database.php) is almost empty, it looks like this:

class DATABASE_CONFIG {
    public $default = array(
        'datasource' => '',
    );
}

You have to tell your AppModel not to use DB tables, otherwise you'll get an error: "Datasource class could not be found". It's not enough to set the $useTables in descendant models only.

class AppModel extends Model {
    public $useTable = false;
}

I dindn't experienced any problems with that yet.

like image 144
David Ferenczy Rogožan Avatar answered Oct 11 '22 10:10

David Ferenczy Rogožan


Cakephp actually try to connect to a database no matter that you don’t use a table so using this

class MyModel extends AppModel {
    public $useTable = false;
}

it will be just a mistake , creating application on cakephp is piece of cake. Here are some steps you have to do in order to start developing without a database.

  1. Create fake dbo source

Create file DboFakeDboSource.php in app/Model/Datasource/Dbo/ and put the following code in it

class DboFakeDboSource extends DboSource {
  function connect() {
    $this->connected = true;
    return $this->connected;
  }
  function disconnect() {
    $this->connected = false;
    return !$this->connected;
  }
}
  1. Set the default connection

The next step is to tell cakephp to use dbo source by default. Go and change default connection in database.php to be like this

var $default = array(
  'driver' => 'FakeDboSource'
);
  1. Fine tuning the model

The third step is to be sure that $useTable = false; is included in every model, so add it in AppModel.php

like image 28
Abdou Tahiri Avatar answered Oct 11 '22 10:10

Abdou Tahiri