Sandbox :: Cakephp Sql Converter

The SQL to CakePHP query converter is able to parse most SQL strings and generate the corresponding CakePHP code. To get started simply paste a query string into the textarea and click Convert.

The SQL to CakePHP query converer will convert SQL queries to CakePHP code for the 2.x branch of CakePHP. The code may be valid for 1.3 also, but that is not a goal at the moment.


There are some more advanced options available to select from that will affect the generated code.


This specifies where you will be calling the find from. Depending on the location the code may differ slightly. Here are some examples of what the query converter will generate based on the options you choose.

From a model
This will generate the code to be used within the model itsself
From a related model
This will generate the code to be used from within a related model
From a controller
This generates the code to be used from the models controller (the one that matches the models name or has the model in $uses property)
From a component
Cake passes an instance of the controller class to component methods. This assumes making use of that controller instance.

There are some cases when the model is not directly available so it needs to be loaded. From controllers and components that is pretty easy with the loadModel().

Unrelated model in a controller
If the model is not connected to the controller it needs to be loaded first
Unrelated component
Similar to the unrelated controller, here the model is loaded first
From a lib
For all other locations such as lib files the model is loaded with the ClassRegistry.

Select Queries

This is the type of find you would like to create. The options are from Cake find types such as first, all and list. The will generate code similar to the following:

$this->find('all', $options);
$this->find('first', $options);
$this->find('list', $options);

Custom find types

You can extend CakePHP to use custom find types. This is a great way to stick with the fat models, skinny controllers mantra. Filling this box will generate the code required to use custom find methods.

To See how pagination and other queries work with custom find types you should leave the Find Type field as Automatic

Extending the find types you would be able to do something like the following:

class YourModel extends AppModel {
	protected function _findAwesome($state, $query, $results = array()) {

$this->YourModel->find('awesome', $options);

Virtual fields

CakePHP has a nifty feature for creating fields on the fly called virtualFields that are used for selecting aggregate fields and preforming functions in queries. Without using virtual fields CakePHP will normally not beable to map the data correctly and results end up with arrays looking like $data[0][0]['SUM(your_field)'] = 123;

The query converter will for the most part convert aggregate and functions into virtualFields and generate the correct code for using the virtualFields.

Some smarts

The query converter attempts to figure out the best options to use when generating a query. It looks at what sort of query you are doing and the fields being used.


If the LIMIT has been defined as 1 the query converter will generate a ->find('first'); type query.


If there are only two fields being selected in the query a ->find('list'); type query will be generated.

INSERT queries

Coming soon

UPDATE queries

Coming soon

DELETE queries

Coming soon

Wont Fix

Some things CakePHP does not support such as UNION. These will not be implemented any time soon.


If you notice any bugs let me know via twitter or contact me. I will need a copy of the query that is giving problems so I can fix it, best to paste it in something like and send the link to me.

If the query is not valid I wont look at fixing it. All others will get some attention.