Sharing technology, ideas, insights!
Call: +91 710 466 0336         Email: hello@sanisoft.com

Blog

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

#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

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

#3 Now make sure your models set useTable to 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 the Author

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 scalability, development tools and usability practices with strong developmental skills in PHP, MySQL/PostgreSQL, HTML, Javascript, and Linux/Apache

24 comments

  1. 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…

  2. 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.

  3. Pingback: Sanisoft Blog: Using CakePHP without a database | Development Blog With Code Updates : Developercast.com

  4. @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?

  5. @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

  6. @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).

  7. @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

  8. @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

  9. @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

  10. 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?

  11. 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….

  12. 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

  13. 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!

  14. 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.

Leave a Reply