Posted on 14/9/06 by Felix Geisendörfer
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.
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.
var $name = "Posts";
function index() // GET
// Prepare the overview of all posts,
// Usally you'll do something like: $this->set('posts', $this->Post->findAll());
function create() // GET
// Here goes everything you need to prepare an empty form for you Post
// to be filled out
// If you don't want to write the template for editing a post twice (for create
// and edit), just put a $this->render('edit'); at the end of this function.
function save() // POST
// This function interacts with your Model ( $this->Posts ) in order to bring
// $this->data['Post'] into your database
// After it you can call $this->edit($this->Post->getLastInsertID()) in order to
// display the edit box again if you want the User to continue editing
function show($id) // GET
// This function shows a Post with a given $id
function edit($id) // GET
// This function reads the Post with $id from your database and prepares all
// data for the View to display an edit form for it.
function update() // POST
// This function takes care of updating the post contained in $this->data['Post']
// in the database.
// Like with create, you can call $this->edit($this->data['Post']['id']); at the
// end of this function to let the user continue editing the Post.
function destroy($id) // GET
// This function destroys the Post a given $id
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
You can skip to the end and add a comment.
This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.