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.

No comments: