CakePHP Bake – Baking Models, Controllers and Views the CakePHP 1.2 Way
Patty Cake, Patty Cake, Baker’s Man
One of the things that sold me on CakePHP is the bake routine. This is basically code generation for the lazy types like myself. It was good in CakePHP version 1.1, now it’s just awesome. The only way they can make it better is to implement a web version, but that’s another story.
What Baking Does
In CakePHP we refer to the automatic code generation as baking (get it now?). An entire application can be baked from nothing more than a few tables in a database. CakePHP uses some skeleton templates, which you can of course customize to your needs, to generate your Models, Views and Controllers. The controllers and views come with the standard CRUD (create, read, update, and delete) functions and can also contain admin functions.
Currently, we run the Bake routine from the command line. I’m hoping sooner or later this can change, but with a lot of hosts allowing shell access to your account, this isn’t a priority with the developers.
Setting Up In Windows
Although this is not necessary, one thing I like to do is add both bake and the PHP executable to my path. To do this, we go to the Windows System Properties dialog, then the Advanced tab, and click on Environment Variables.
You find the Path system variable and add your PHP (D:\wamp\php;) and cake console (D:\wamp\www\cake\cake\console) paths, separated by semicolons.
Linux users, you haven’t Been forgotten.
Preheat the Oven
When I bake, I like to bake from scratch, so to speak. Let’s assume we’re creating a fresh new application called baz. You go to CakePHP main folder and type the following on the command line:
cake bake baz
and follow the prompts.
This will generate a new application folder called baz ready for use. After this, you would go to the application folder and set up your configurations; core.php, database.php, etc. One thing about baking an application from scratch is that it bakes the application with a new, random Security Salt variable. Pretty cool huh?
You could even bake your database configuration for the database.php file, but let’s keep not get carried away here.
Baking Models
This is the most important part of baking, the model. Because CakePHP draws all its power from from conventions, it is extremely important that your database follow the CakePHP conventions. This means table names, field names, foreign key names, plurals, etc. Head to the folder for the application: (D:\wamp\www\cake\baz) and type in:
cake bake
You could also skip the first prompt and bake the model directly:
cake bake model [model name]
Follow the prompts, bake a model, etc. At every step, you’ll be prompted for various aspects of your model. It asks you what type of validation you want for each field for the model. If your database is set up correctly, the bake routine can even detect your database associations and build them accordingly. For example, while baking the Users model, if you have a table, users that contains a field group_id, the routine will ask if the model “hasOne Group”. Say no, and it will ask if the model “hasMany Groups”. It doesn’t get easier than this.
Baking Controllers
You can go through all the prompts, but in my opinion this takes too much time for me. Here are some straight to the point commands to get you on your way:
cake bake controller Users
This bakes an empty controller using scaffolding. If you don’t know, Scaffolding is sort of like baking, except with no functions and no views. This is usually used to test of database schema, model association, model validation, etc.
cake bake controller Users scaffold
This does the same thing as the command above, except it actually creates the functions: index, add, view, edit, and delete.
cake bake controller Users scaffold admin
Again, this does the same thing as the top, but with admin_index, admin_add, admin_view, admin_edit and admin_delete functions. Starting to get the picture?
Baking Views
This couldn’t be simpler:
cake bake view Users
Creates views for all the functions created in the Users controller.
Now you could of course just use the basic bake routine and follow the prompts for every aspect of building, but I’ve found that this wastes too much time; especially with the controllers and views. You only need to pay attention for the model.
Bake applications has become priceless for me when trying to do a proof of concept of a new application for a client.
Source: CakePHP Baking [CakeBaker]