I have been playing around with cake for about two weeks now and thought I would share this discovery. No it's not the next best internet application, but for the n00bs it will be quite helpful.
When creating pages(views) with forms and the controller functions that work with the view there are some interesting caveats. While the current cake documentation tries to explain how this works, I think it can be explained in more of a n00b way to understand.
Let's look at a simple Edit function. One of the things I struggled with early on was code re-use, and some of the extra code that sometimes goes with the theory. Well, the people at cakePHP have made all of our lives just a little easier. I like to try to use the same view and function for those parts of a web application that are redundant.
For example, our Add/Create and Edit function will use the exact same form 99% of the time. When you really think about it, sounds like carpel-tunnel to me. If you really want to take advantage of cake, learning how the current controller /view $this->data variable works will save you a ton of time. I'm not real keen on all the arrays as I am an object kind of guy, but that's not important right now.
Three functions are built right into cake that are great for watching the inner workings.
- core.php - set DEBUG to 2 ( let's you see the model at work running queries)
- the debug($var) function - it's like print_r only nicer
- pr($var) - works just like debug, it's a prettier print_r function
OK, let me get back on track here.
When you call your Add/Create function for the first time, there usually no variables involved. You are creating a new DB entry for whatever it is you are creating, be it Posts-Tags-Users; whatever.
The simplest function looks like this-
function Create(){
}
Then the create function calls the view-
<form>
<?php $html->input('Whatever/field'); ?>
</form>
Here is where the caveat comes into play. Make sure you follow the cake conventions
and CAPITALIZE the first leter of your Model name here. The $html form functions are meant to work in tandem with the Model.
OK, but what is so special about that you ask?
Well, I like to save time. Especially when it comes to redundant forms, so here is the tricky part. You are almost inevitably going to need an Edit function to go with that Add/Create function, so first off - redirect the function like so
function edit(){
$this->redirect('/yourcontroller/create');
}
then modify your create function to load the data you want to edit
function create($id = null){ // default null tells the function to use create mode vs. edit mode
if($id){ // if the $id var is set we are in edit mode
$this->data = $this->Whatever->find("id = $id"); // load the data we need to edit
}
// otherwise we are in create mode - do nothing
}
now when the view for the create function gets loaded $html->input('Whatever/field') will magically have the value="field data" filled in for you. All thanks to the controller variable $this->data.
No comments:
Post a Comment