CakePHP model component for quick model actions

CakePHP's scaffolding controller actions provide a very nice template for basic model actions such as add, edit, delete. However my problem with them is that they are too verbose for something used so often. Therefore I set out to abstract them into their own component that can achieve the same code that an entire scaffold action does with just a few lines.

The results of my efforts is the model component, available in full at Please go there to see and download the source code. I will just outline its features here. Please note that the component uses a global function called dot, which is simply a dot notation array access function I wrote. Please see my PHP array dot notation access article for the source code to that.

The component contains two main methods for use. They are add and edit. Both methods will automatically check the parent controller's data variable, which is automatically filled if you used CakePHP's form helper for all form inputs. Add and edit are basically the same method with add calling the create method on a given model prior to saving and edit checking that an id is provided before saving.

Add/edit accepts the following parameters: $modelName is the name of the model that is to be saved, ie. User. $params is an array with the following optional items:
  • success: Sets the flash message to display on success
  • successUrl: Redirects to this URL after the model data have been successfully saved
  • fail: Sets the flash message to display on failure
  • failUrl: Redirects to this URL if the results of the save is false
A sample call from a controller looks like the following:

$this->Model->edit('User', array('success' => 'Your account details have been saved', 'successUrl' => '/messages/thanks', 'fail' => 'Sorry, your account details could not be saved please correct the following errors and try again'));
That code will redirect the user to /messages/thanks and show the success message while leaving the user at the current page to correct any errors that were detected by the User model.

The component also auto-loads any model that it does not find in the controller so one does not need to worry about specifying the model in the $uses array if the model will not be used often. However I find that if you are performing creation and update actions then you will need to load the model for the viewing page anyway.

It is possible to extend the component to support delete as well although I do find that just calling the default del method is simple enough, which is why I did not include it in the component.


Juan Basso said...

It's a nice idea. Very simple and less code for multiple scaffoldings. But you can replace dot function by Set::format, making a cake style.

Paul Chiu said...

Thanks for tip on Set::format Juan. Although it seems like Set::extract does more of what I need dot for. I'll try to integrate it into a future project and see if I can make the Model component more standardized.

Post a Comment