CakePHP schema checker

If you have seen the previous tool I built for converting raw SQL to CakePHP code you might have an idea what is coming. There was good feedback from the initial tool so I have built something similar for people migrating legacy apps and newcomers alike.

Instead of conversions, this time it helps find specific errors in your schema. For example if you are using a field like user_id it will expect there to be a users table. It will also warn about fields that are not cased correctly such as UserID or userId vs user_id.

This tool makes use of the Inflector class for working out if fields and tables are named correctly, and then attempts to link up models based on the fields in the schema much like bake would do.

Currently I am sure that this does not cover every possible case, so let me know if you spot anything that should be covered. Some cases will be virtually impossible to cover, for example if you are using a field called user_primary_key there is no way to know it should be user_id. With regards to foreign key usage in CakePHP they should always end with _id. All database tables and fields should be lowercase, with tables being plural.

This tool has been designed to work with a dump from phpmyadmin, without data. Just select export and then select 'structure only' option. Fields and tables should be wrapped in back ticks `.

Once it runs, a table is shown with all the models and relations. Errors are highlighted with the following key.

Here are some examples of a good and bad CakePHP schema. The good variant is a dump from Croogo while the bad variant is from Cacti, a PHP based server monitoring tool that is not built using CakePHP.

Schema checker

Read more...

Cache all CakePHP find queries

I have been adding some cache to Infinitas recently and found it rather difficult to cache all find queries without having to overload and rewrite the entire Model::find() method. While its not the most complicated method in the world, its not DRY rewriting it and some time in the future its likely to bite me in the arse when something changes.

A basic way to cache the queries is as follows:

…

Read more...

How to convert SQL to CakePHP find

There are a lot of newcomers asking the same question on CakePHP's IRC channel, Stack Overflow and various other places, and that question is how to convert a SQL query into the corresponding CakePHP code.

Once you have been using CakePHP for a while the ORM becomes second nature and its pretty easy to see how a query will fit into a Model::find(). I normally break the SQL up into the various …

Read more...

Batch convert NEF (raw) to PNG

Here is a simple script for converting raw NEF files from a Nikon to PNG format. It uses the convert utility which can handle a large variety of file types and has various options for the conversion. This is just a simple script that will generate a PNG that is either 1024 px wide or high.

#!/bin/bash
##################################
#      Convert files to png      #
##################…

Read more...

Nikon raw images on Ubuntu 12.04

Since switching to Ubuntu 12.04 sometime last year I have not been able to see thumbnails of images shot in raw format. Specifically I have a Nikon camera that shoots NEF files which even after installing various tools such as UFRaw still did not work. Eventually I wrote a small bash script for converting NEF to PNG so I could see what the image was without having to open each one in UFRaw first.…

Read more...

CakePHP inflector sandbox

I built this little tool a while ago because of the large amount of people that kept asking about how the inflector in CakePHP works or why their model was not working correctly. As cake works on conventions almost every class name and table name is generated using the inflector.

The inflector is used to convert from urls to controllers, from controller names to model names and so on. In versi…

Read more...