Using CakePHP without a database

If you solve a problem – blog about it, chances are that somebody, somewhere, is already facing the same problem.

We recently programmed a CakePHP app which relies solely on remote server talking Saleforce API for all data operations. The first challenge that we faced was how to prevent CakePHP from insisting on a database connection. The solution was simple enough and I never gave it much thought till the same question was brought up by more than one person on the CakePHP list. The solution involves creating a minimal dbo source file.

Without much fanfare – here are the steps to trick CakePHP into believing that there is a database even when there is none!

#1 Create a file called dbo_my_dbo_source.php in the directory app/models/datasources/dbo/
Put the following code in this newly created file

  1. class DboMyDboSource extends DboSource
  2. {
  3.     function connect()
  4.     {
  5.         $this->connected = true;
  6.         return $this->connected;
  7.     }
  8.  
  9.     function disconnect()
  10.     {
  11.         $this->connected = false;
  12.         return !$this->connected;
  13.     }
  14.  
  15.  
  16. }

#2 Next we need to tell our app to use this dbo source by default – for that edit database.php and as driver put ‘my_dbo_source’ this is set to ‘mysql’ by default

  1. 'driver' => 'my_dbo_source'

Your installation will now report that “Cake is able to connect to the database.”

#3 Now make sure your models set useTable to false

  1. $useTable = false;

That is it! you are free to take this forward as you wish. In our case we abstracted a lot of the functionality of the API into the dbo source and the app model. This made the models and controllers almost indistinguishable from the regular CakePHP ones – so at a future date if we want to shift to another datasource it will a *piece of cake*

Thanks to Amit Badkas who wrote the code for this.

About Tarique Sani

Dr. Tarique Sani is a pediatrician and forensic expert by education. He is a PHP programmer of 'wrote the book' caliber and has to his credit several very popular open source as well as commercial PHP projects. He leads a team of dynamic programmers at SANIsoft who have in-depth understanding of Web development tools and usability practices with strong developmental skills in PHP, MySQL/PostgreSQL, HTML, DHTML, Javascript, and Linux/Apache

,

24 Responses to Using CakePHP without a database

  1. Neil Crookes August 22, 2008 at 5:07 pm #

    Could you have created a sales force dbo source?

  2. Neil Crookes August 22, 2008 at 5:08 pm #

    i.e. the dbo source does all the calls to the Sales Force API.

  3. Daniel Hofstetter August 22, 2008 at 5:09 pm #

    Is there some special reason you do steps #1 and #2? Seems to be a bit of overkill to create a DboSource just to avoid the error message of the installation page ;-)

  4. Tarique Sani August 22, 2008 at 5:25 pm #

    @Daniel – like I wrote you have to extend the functionality as per your wishes :)

  5. Tarique Sani August 22, 2008 at 5:30 pm #

    @Neil – that is exactly what we did, the dbo and app_model handles most of the common calls to the salesforce API.

    I guess people are not reading the last para :)

  6. Daniel Hofstetter August 22, 2008 at 5:51 pm #

    Wouldn’t it be cleaner to use a simple PHP class (which doesn’t extend anything) as a model and implement the API there? DboSource looks to me like the base class for drivers for SQL-based databases, but not for accessing remote APIs…

  7. Tarique Sani August 22, 2008 at 5:58 pm #

    @Daniel – matter of personal choice

  8. Tarique Sani August 22, 2008 at 6:01 pm #

    to elaborate a bit more – it is matter of how you look at the data source. Thanks to frameworks the SQL part is not the consideration any more and on the other hand there are APIs which allow you to execute SQL *like* queries.

  9. Neil Crookes August 23, 2008 at 12:26 am #

    @Tarique, my apologies, don’t know how I missed that, must not have had a coffee this morning.

    @Daniel, do you not need to do step 1 to avoid the error message if your app has no other db whatsoever?

    Perhaps a combination of the 2 options presented would be the most flexible solution, i.e. a separate vendor class containing the API functionality that can be used outside a cake app, and a custom SalesForceDboSource class that acts as a wrapper around the vendor class?

  10. Tarique Sani August 23, 2008 at 8:22 am #

    @Neil – if you put $useTable = false then DB connection is not checked for by the regular models and you don’t need to define a dbo source.

    Yes, you are thinking in the right direction in case of Salesforce API the SDK was put in Vendors and the dbo was a wrapper for some of the functionality

  11. Dardo Sordi August 26, 2008 at 6:43 pm #

    @Tarique:

    Daniel is right DboSource is the base class for SQL drivers, I think it is better to extend DataSource (the base class for DboSource).

  12. Tarique Sani August 26, 2008 at 6:48 pm #

    @Dardo Sordi – now that you mention it again, yes Daniel was write if he meant DataSource should be extended and not DboSource

  13. Amit Badkas August 26, 2008 at 6:57 pm #

    @Dardo Sordi – a lot of functionality which is in DboSource is needed by any API based data source as well like connecting to begin with – so it makes more sense to extend DboSource even though semantically DboSource is coded more towards SQL databases – hence it was my conscious decision to use DboSource as the base class and not the more abstract DataSource

  14. Tarique Sani August 26, 2008 at 7:31 pm #

    Well if Amit stands by what he has written – I will agree with him – he knows more CakePHP internals than me

  15. Dardo Sordi August 27, 2008 at 1:16 am #

    @Amit Badkas: well probably in your case it was the right choice, what I said was based in the examples I saw in the comunity: bakery’s LDAP DataSource, Felix’s Google Analitycs and Amazon datasources, and Youtube datasource (in GitHUB)… but we all know YMMV

  16. Amit Badkas August 27, 2008 at 10:51 am #

    @Dardo Sordi: as per MVC and cake conventions, data resources should be wrapped in models – I particularly disapprove of creating a datasource object directly in controller and using it – so $amazon->find() IMHO should ideally be $this->Amazon->find() and also this will be more flexible and extensible

  17. Daniel Farrell September 11, 2008 at 5:28 pm #

    While reading this discussion I had the following idea. What if we make a WebSource(or RestSource or whatever) that extends DataSource and provides the parts of DboSource that would be useful? Then we could have a web/ directory next to dbo/ that holds the datasources for each api that extend WebSource. Does that interest anyone?

  18. Anonymous Coward March 10, 2009 at 8:35 pm #

    I’ve recently built a salesforce integration as well, and what i’ve run into is the API call limit – something like 1000 calls per license.
    Have you run into this issue? How have you solved it?
    I’m thinking of building a caching layer into the app to avoid any downtime due to API unavailability….

    • Tarique Sani March 12, 2009 at 9:31 am #

      Caching just for the times when API is unavailable is tricky – we have a DB based caching for the complete app…. depends on your requirement.

  19. Maddin April 16, 2009 at 9:20 pm #

    Hi,

    I´m quite new to CakePHP. I got this tutorial running, but how can I set and get data to the DboFile? There are no functions for doing that, am I right?
    When I try the Alternative, like Daniel mentioned, then Data gets lost. Maybe the object gets reinitialised …
    Maybe somebody has a hint, or something :) , thanks

  20. Duff [for you Duff for me :] October 16, 2009 at 2:11 am #

    Just wanted to say quick ‘Thanks’ :) Im setting up quick ‘site under construction’ with contact form and your no-db solution was just what i was looking for !

  21. Patty November 17, 2011 at 2:34 am #

    When I use your script im getting this error:

    Fatal error: Call to undefined method DboMyDboSource::_execute() in /directoryToFile/cake/libs/model/datasources/dbo_source.php on line 253

    Im basically trying to do what you are, im connecting to a 3rd party API for all data operations and want to use the model for validation and what not. Most of the site seems to work fine, it’s just when I pass an ‘id’ field in via the URL (/business/venue/edit/123456) or use it as the name of a form field I get this error. If I change the name of the form field and do not pass the ‘id’ via the URL I do not get the error BUT I shouldnt have to change the names right? It should just work.

    Any help would be most appreciated!

  22. Ross November 29, 2011 at 1:28 am #

    Looks promising; does this work for 2.0 as the naming schema is different and there’s no DBO folder unless I’m mistaken; only Datasources.

Trackbacks/Pingbacks

  1. Sanisoft Blog: Using CakePHP without a database | Development Blog With Code Updates : Developercast.com - August 22, 2008

    [...] the Sanisoft blog today Tarique Sani has posted about a method for using the CakePHP framework without a database to back it up. We recently programmed [...]

Leave a Reply