Turn Off Inflections (Singular Plural) in CakePHP
Friday, January 28th, 2011I am going to be very honest, guys. When I started learning CakePHP it was a great amazement to me that they decided to impose plural for names of data model entities. By most popular data modeling methodologies names of entities (tables) in a database should be singular. Data for users should be in a table called user, date for products should be in table product etc., etc.
OK, second surprise was that some components in the CakePHP framework related to a database table are suggested (to a new user of CakePHP will appear imposed) to be in singular, like the component closest to the data schema – the model (the .php model file) and the model object/class defined in it. When we get to views we go to plural again.
I am sure there was some reasoning behind that and some desire for allowing when these names of components are used in regular English for them to sort of sound cool.
The CakePHP framework core uses dedicated logic to handle translation between singular and plural and for cases where the language (including everyday English) does not allow simple plural formation by adding an ‘s’, the framework has provided a configuration file to set special translations – inflections.php.
Here is my criticisms with the plural / singular thing in CakePHP.
- popular and accepted data modeling methodologies suggest entities to be named in singular;
- naming some components in plural and some in singular creates a lot of confusion especially for beginners (beginners not necessarily in software development but in CakePHP); isn’t one of the ideas behind frameworks and RAD a shorter learning curve?
- the need to configure special inflections is against the convention vs. configuration principle; CakePHP out of the box comes with inflections configured for mouse/mice, status/statuses, deer, sheep, etc.; a convention would have been to let all components be named in singular (or plural for that matter, but consistently) and allow developers use configuration (plurals in some cases) only of they want to;
- fragility – most CakePHP developers learn about the absolute necessity to configure inflections after they stumble on this underwater rock in a real bug;
My ultimate solution:
- turn off plural/singular madness completely off by editing this line in inflections.php
$uninflectedPlural = array();
to
$uninflectedPlural = array(‘.*’);
Yep, treat all words (used in data model entities (tables), models, views, controllers) as Un-inflected Plurals / Singular. And actually use singular forms of such words / nouns.
In addition to bringing some sanity to your coding experience with CakePHP you will remove some overhead from making the framework figure out the irregular plurals / singular on every call.
Let us know what you think.