CakePHP without a database

Posted on June 9th, 2009

You can follow any responses to this entry through the RSS 2.0 feed.

CakePHP

I have started using the CakePHP MVC framework a few months ago, and it’s not too bad.
However, if you want to create a website that does not require a database connection, there’s no example or default setting.
Somehow, if you Google around you’ll find an old “MyDbo” example that isn’t working properly (at least for me) in the latest CakePHP (1.2.x)

The solution is simple, and most of us probably do this right away, but I think having an example somewhere on the net can help many people. Well, at least I hope so, so here we go:

First create the directory for the new databasource (which is, your database connector)

# mkdir app/models/datasources/dbo

Then add a file named

dbo_dummy_source.php

and fill it with all required functions for a DataSource class:

< ?php
class DboDummySource extends DataSource
{
var $description = "This is a dummy data source";
function connect()
{
$this->connected = true;
return $this->connected;
}
function disconnect()
{
$this->;connected = false;
return !$this->connected;
}
function value($string)
{
return "\0".$string."\0";
}
}
?>

Finally, just use this as datasource in CakePHP, in the file

app/config/database.php
class DATABASE_CONFIG {
 
var $default = array( 'driver' => 'dummy_source');
}

Don’t forget to also use

var $useTables = false;

in every model.

3 Responses to “CakePHP without a database”

  1. Visionary Scope says:

    This worked wonders, thanks!

  2. sokar says:

    In cake 1.3(tested on RC4) we only need isConnected()

  3. CakePHP без базы данных | Заметки разработчика says:

    [...] источнике решения автор пишет, что проверял данный код в версии кейка [...]

Leave a Reply