Code Generation with Bake all in CakePHP

Each MVC module contains a Model, a Controller and multiple views for each table in the application. It can be a big pain to write all the controllers, models and views for your application from scratch and define the relationships. Not only it’s time consuming, banal, it is prone to errors in defining relationships and constraints. To make life easy its time to use Cake’s scaffolding console for code generation, known as Bake. Launching Bake: For Baking, we will have to use PHP executable. Rather than typing PHP path each time, I prefer to include php executable path to the environment variable paths, for comfort rather than necessity. For windows, it’s accessible via -> Right click on “My Computer”->Properties. -> Go to “Advanced“ tab. Click on environment variables. -> Go to system variable path and edit it. -> Add PHP path (Mine is xampp E:/xampp/php) and cake console (E:/xampp/htdocs/cake/console) paths, separated by semicolons. You might need to restart system after adding these paths. Sometimes the bake command doesnt work on windows even after changing environment variables. Make sure you use the correct forward slash for this when giving path for console and php. Sample Path post editing should look like C:Documents and SettingsEnbake>path PATH=C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:Program FilesTortoiseSVNbin;E:/xampp/htdocs/campaign3/cake/console;E:/xampp/php;

Sample PATH variable

Time for pre heat Go to the app folder of your application through console. Type “cake bake”. This will give you following screen.  We have two options here:
  1. If your database.php is updated with connectivity settings, this will pick existing tables and give you option to bake them. Cakephp Bake Console
  2. But if DB information is missing in databse.php, we can configure it using ‘D’ as an option to bake. It will prompt us for all the settings and create necessary models, views & controllers.
Super Bake : bake all If you are sure about your DB and don’t want to go individual on baking separately for MVC, you can avail option of bake all. Instead of models this time it will ask you for each entity and on pressing the number it will create models, controllers and views. Hope baking will be as simple as walking through the cake & will save you precious time in coding skeleton for controllers, models and views. Especially when doing a feasibility study or brief demo to the client on the app. Optional Enhancements: Over 1.2 the recent released 1.3 bake has had a significant jump, and a number of features and enhancements have been added.
  • New tasks (FixtureTask and TestTask) are added to the main bake menu
  • TemplateTask has been added for use in the shells.
  • All the different bake tasks now can use connections other than default for baking. Using the -connection parameter.
  • Multiple validations on models has been added.
  • Self Associated models using parent_id are now detected. For example if your model is named Thread, a ParentThread and ChildThread association will be created.
  • Baked Tests include as many fixtures as they know about, including plugin detection (plugin detection does not work on PHP4).
Happy Baking !!!