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.
This will generate the code to be used within the model itsself
$this->find('all');
This will generate the code to be used from within a related model
$this->Related->find('all');
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)
$this->ModelName->find('all');
Cake passes an instance of the controller class to component methods. This assumes making use of that controller instance.
$Controller->ModelName->find('all');
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()
.
If the model is not connected to the controller it needs to be loaded first
$this->loadModel('ModelName');
$this->ModelName->find('all');
Similar to the unrelated controller, here the model is loaded first
$Controller->loadModel('ModelName');
$Controller->ModelName->find('all');
For all other locations such as lib files the model is loaded with the ClassRegistry
.
ClassRegistry::init('ModelName')->find('all');
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);
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);
$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.
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.
Coming soon
Coming soon
Coming soon
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 bin.cakephp.org and send the link to me.
If the query is not valid I wont look at fixing it. All others will get some attention.