Dessert #1 - The 7 crucials of CRUD

Posted by Felix Geisendörfer, on Sep 14, 2006 - in PHP & CakePHP » Controllers, Components & Shells

Deprecated post

The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.

Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.

Comments: You can continue to leave comments on this post, but please consult Google or our search first if you want to get an answer ; ).

Update: I changed the functions new_one and create to create and save based on Daniel's suggestion in the comments. However I still would love the ability to use new & create but since new can't be used (it's a php keyword), this seems like a good workaround.

When you've worked with CakePHP you might have heard about CRUD before (Create, Read, Update, Delete) which is a pattern used in the database world which translates well into controller actions. Now so usally you would use 5 functions (create, edit, view, delete, + index). And while this isn't a bad idea, you always end up with a couple if statements where you check whether the user already posted some data to Posts::create() or whether he just requests it via GET in order to start writing a new Post.

For that reason, a nice simplification can be achieved by using 7 functions instead of 5. The example below shows you how those additional actions could look like and what should be done inside of them.

php
  1. class PostsController extends AppController
  2. {
  3.     var $name = "Posts";
  4.    
  5.     function index() // GET
  6.     {
  7.         // Prepare the overview of all posts,
  8.         // Usally you'll do something like: $this->set('posts', $this->Post->findAll());
  9.     }
  10.    
  11.     function create() // GET
  12.     {
  13.         // Here goes everything you need to prepare an empty form for you Post
  14.         // to be filled out
  15.        
  16.         // If you don't want to write the template for editing a post twice (for create        
  17.         // and edit), just put a $this->render('edit'); at the end of this function.
  18.     }
  19.    
  20.     function save() // POST
  21.     {
  22.         // This function interacts with your Model ( $this->Posts ) in order to bring
  23.         // $this->data['Post'] into your database
  24.        
  25.         // After it you can call $this->edit($this->Post->getLastInsertID()) in order to
  26.         // display the edit box again if you want the User to continue editing
  27.     }
  28.    
  29.     function show($id) // GET
  30.     {
  31.         // This function shows a Post with a given $id
  32.     }
  33.    
  34.     function edit($id) // GET
  35.     {
  36.         // This function reads the Post with $id from your database and prepares all
  37.         // data for the View to display an edit form for it.
  38.     }
  39.    
  40.     function update() // POST
  41.     {
  42.         // This function takes care of updating the post contained in $this->data['Post']
  43.         // in the database.
  44.        
  45.         // Like with create, you can call $this->edit($this->data['Post']['id']); at the
  46.         // end of this function to let the user continue editing the Post.
  47.     }
  48.    
  49.     function destroy($id) // GET
  50.     {
  51.         // This function destroys the Post a given $id
  52.     }
  53. }

Alright, I might end up posting two desserts today. Since I've been getting up at 5 am every morning I am actually able to post on here before I go to school. Ok, I didn't write something insanly exiting, but it's a start ; ).

--Felix Geisendörfer aka the_undefined