CakePHP Bake – Baking Models, Controllers and Views the CakePHP 1.2 Way

Oven Knob

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.
Setting PATH in Windows
You find the Path system variable and add your PHP (D:wampphp;) and cake console (D:wampwwwcakecakeconsole) 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:wampwwwcakebaz) 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]

Comments

  1. fanna digema mrs. says:

    i would like to know how a fondant and chocolate cake are made
    please reply through this e-mail.

  2. “One thing about baking an application from scratch is that it bakes the application with a new, random Security Salt variable. Pretty cool huh?” – I didn’t know this sweet fact! Before reading your blog I was baking in the original cake directory and everything was getting messy… and each time I had to re-define the Path in the system for each of my test projects :(. Now I put the original fresh cake backup in xampp\cake\ and create the new projects in new directory.. so coo! :D
    I’m a novice.. your tips helped me a lot… really a great article to begin with cake bake.

    Thanks
    Adnan

  3. An excellemt tutorial :)

  4. Hi;
    First of all I am new member to your site to everyone I mehaba; descriptive and useful information about your site, there are a lot but mostly superficial information malesef Failed hope that the future will be more in depth information on your site.
    Mary rose

  5. hi there,it’s a nice tutorial.but I have a problem when trying to display scaffolding.can you explain to me the step by step instruction to get scaffolding working in cakephp? I am bit confuse here in using cakephp.and I cannot move on if I am still cannot use scaffolding feature in cakephp.thanks then.
    .-= jacobian´s last blog ..MVC (model views controller) =-.

  6. nice article, thanks

  7. thats interesting thx

  8. thanks good article from you

  9. Thank you boy! Favorite this now!

  10. intelligent and funny.

Trackbacks

  1. [...] CakePHP Bake – Baking with CakePHP 1.2 | Web Development 2.0: Web Design, CakePHP, Javascript (tags: cakephp) [...]

  2. [...] on how to bake in CakePHP For your baking needs refer to the links below Bake Utility Baking MVC Bake a new Project You could also try cake bake [...]

  3. [...] the original post here: CakePHP Bake – Baking with CakePHP 1.2 | Web Development 2.0: Web Design, CakePHP, Javascript Share and [...]

  4. [...] CakePHP Bake – Baking Models, Controllers and Views the CakePHP 1.2 Way [...]

  5. [...] CakePHP Bake – Baking Models, Controllers and Views the CakePHP 1.2 Way [...]

  6. [...] CakePHP Bake – Baking Models, Controllers and Views the CakePHP 1.2 Way [...]