CakePHP tricks: Baking everything with scaffold

One of the best parts of CakePHP, our current rapid development framework of choice, is scaffold. This is a built in way to quickly add a front end onto any database (or set of models) to better visualise whether or not an application is going to work in the real world. This allows you to rapidly prototype ideas and make more informed decisions on the way forward.

We prototype several ideas a month so for us, more speed means more time to assess and refine an idea. In order to provide us with as much time as possible we also use CakePHP’s excellent “Bake” console application to quickly generate models, controllers and views in conjunction with scaffold.

However, for all CakePHP’s good points, one thing that lets it down is the online documentation. For example the page in the online manual for the bake console application states the following:

Parameters on the ControllerTask have changed as well. cake bake controller scaffold is now cake bake controller public.

As well as not adequately explaining the change (or the format of the parameters for that matter) it also misses out one of the most important commands for rapid prototyping cake bake controller all scaffold. Making a rapid prototype of a web application in early versions of cake was a simple three step process (once you had set up your database connection and security settings):

  • Create the database using the cake naming conventions
  • Run cake bake model all
  • Run cake bake controller all scaffold

Then you could change your database to your hearts content and keep re-baking all of the models as you tweaked the application to come up with something that fits the requirements.

It seems that at some point the method to bake all of the controllers with scaffold in this way has either been removed and not documented properly, or accidentally dropped. Either way, one of the most useful rapid prototyping commands for CakePHP has slipped by the wayside. Not great, so let’s look at how we can build it back into the latest stable release of CakePHP, which is currently 2.0.6.

The file we will need to edit is in

lib/Cake/Console/Command/Task/ControllerTask.php

First locate the “all” function and change it as below – the change is highlighted by a comment:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * Bake All the controllers at once.  Will only bake controllers for models that exist.
 *
 * @return void
 */
	public function all() {
		$this->interactive = false;
		$this->listAll($this->connection, false);
		ClassRegistry::config('Model', array('ds' => $this->connection));
		$unitTestExists = $this->_checkUnitTest();
		foreach ($this->__tables as $table) {
			$model = $this->_modelName($table);
			$controller = $this->_controllerName($model);
			App::uses($model, 'Model');
			if (class_exists($model)) {
				if ($this->params['scaffold']) { //RNDMR.com: If statement to work out whether we requested scaffold or not
					$actions = 'scaffold';
				}
				if ($this->bake($controller, $actions) && $unitTestExists) {
					$this->bakeTest($controller);
				}
			}
		}
	}

Easy! Now we need to tell bake to expect a new type of parameter, called scaffold, in it’s new way of receiving parameters, otherwise it will just display the help text when we try to use it. Locate the “getOptionParser” function and modify it as below (once again the change is marked with a comment):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
 * get the option parser.
 *
 * @return void
 */
	public function getOptionParser() {
		$parser = parent::getOptionParser();
		return $parser->description(
				__d('cake_console', 'Bake a controller for a model. Using options you can bake public, admin or both.')
			)->addArgument('name', array(
				'help' => __d('cake_console', 'Name of the controller to bake. Can use Plugin.name to bake controllers into plugins.')
			))->addOption('public', array(
				'help' => __d('cake_console', 'Bake a controller with basic crud actions (index, view, add, edit, delete).'),
				'boolean' => true
			))->addOption('scaffold', array( //RNDMR.com: New scaffold option added
				'help' => __d('cake_console', 'Bake a controller using scaffold.'),
				'boolean' => true
			))->addOption('admin', array(
				'help' => __d('cake_console', 'Bake a controller with crud actions for one of the Routing.prefixes.'),
				'boolean' => true
			))->addOption('plugin', array(
				'short' => 'p',
				'help' => __d('cake_console', 'Plugin to bake the controller into.')
			))->addOption('connection', array(
				'short' => 'c',
				'help' => __d('cake_console', 'The connection the controller\'s model is on.')
			))->addSubcommand('all', array(
				'help' => __d('cake_console', 'Bake all controllers with CRUD methods.')
			))->epilog(__d('cake_console', 'Omitting all arguments and options will enter into an interactive mode.'));
	}

And that’s it! You should now be able to run the following command to generate all of your controllers with scaffold in one easy step.

cake bake controller all --scaffold

Happy scaffolding!

This entry was tagged , , , , , . Bookmark the permalink.


One Response to CakePHP tricks: Baking everything with scaffold

Click here to leave a reply