11.15.2006

Eureka

Well...
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.
  1. core.php - set DEBUG to 2 ( let's you see the model at work running queries)
  2. the debug($var) function - it's like print_r only nicer
  3. pr($var) - works just like debug, it's a prettier print_r function
Sometimes during the scaffolding I like to use die(pr($var)) as well.

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.

11.13.2006

Things I know about CAKE (cakePHP that is), and thought I would share.

While working on a recent project using Cake, I had several Database tables that were to be running live. Any change made to the database had to be “approved” by a higher level administrator. So you see, even updating the changes in the active Database had to be stored in another place.

I decided to track the changes by creating a text field in the MasterChanges table. The text field is nothing more than a simple csv list of the columns from the Database table that an agent/sub user has made modifications to. There is another column that tells me which table to apply these changes to as well.

Once the MasterChangesController is told to “approve” an existing entry, the controller then dynamically loads the appropriate Models to update the respective tables. While there are about 20 active tables monitoring data, I figured that loading all 20 Models might be a bit memory intensive. Each time the administrator decides to approve certain entries, they may not necessarily need to load 20 Models.

Once the MasterChangesController reaches the stage where updating Database tables are necessary the following code works great.

$this->uses[] = ‘aNewModel’; // array_push() without the function call = COOL

$this->constructClasses(); // this is a cakePHP function in the Controller Class

While this particular piece of code is inside a foreach loop, the overhead required to load the Models like this is still less memory and processor intensive than loading all 20 Models at once. The caveat is, constructClasses() will only load those Models that have not been loaded yet, so when coming across an existing Model – the code simply skips right on past.

I am fairly new to cakePHP and this is my first project, so I am sure I will come across more great tricks to share in the future.

11.11.2006

Get the ball rolling.

My latest endeavor has been delving into the world of cakePHP. So far I have had some pretty good results as far as speeding up development time. The only thing I haven't had luck with so far is layouts, but then again I am a database guy, not a layout guy.

If you are a PHP developer and you aren't familiar with Frameworks, I suggest you take a look at cakePHP. This Framework is based on the highly popular Ruby-on-Rails MVC (Model-View-Controller) architecture. With built in support for database table associations and scaffolding framework cakePHP is a great tool.


I have some good demo apps that I am almost finished with, so with any luck I will get them posted fairly soon.

Well, until then everybody take care.