Published: Thursday, November 1st, 2007

List

With this post I'm introducing a new segment to this blog: Cute CakePHP Trick of the Day.

This is basically going to be a learn as I learn sort of thing. There are always a bunch of little things that I want to do in CakePHP, but just haven't figured out. So every time I stumble onto something, I'll let you guys in on it.

I love the GenerateList() function. This is generally used when you have models with associations with other models. The GenerateList function is often used in these cases to populate a drop down list or a multiple select list. A typical example is a state drop down on an address form.

One problem that I have with this implementation is that it always produces a complete list and there is no empty slot. There is nothing to say that I don't want to associate anything for this entry. On a drop down list, it's impossible not to select an element. On a multiple selection list, even if the user holds CTRL and clicks to unselect the current entry, CakePHP ignores this entry.

Here's the fix. Assuming in your controller you have something like:



Published: Sunday, October 28th, 2007

Got The Cron

Web Cron is a great method of automating tasks on your server if you're not blessed with cron jobs. However, after writing this I stumbled onto something that may fit your needs a bit better; Remote Cron is that service. It's everything Web Cron is and more:

  • It's Totally Free
  • It's in English

What more can you ask for? Yeah, I know, my standards are really low.

Now On To The Backup

Previously, I used a script called backupDB for my MySQL PHP based backup solution. This package included a nice graphical management tool. It allowed you to pick different databases and different tables that you want to back up. It was also great for automated tasks also by calling it like this:

PHP:
  1. backupDB.php?StartBackup=complete&nohtml=1

And it is this system of passing parameters that makes this tool extremely flexible. Using parameters you could choose to backup all the databases on the server or just the one you specify. You could also specify individual tables that you would want to backup:

PHP:
  1. backupDB.php?onlyDB=dbname&StartBackup=complete&nohtml=1
  2.  
  3. backupDB.php?StartBackup=complete&SelectedTables[dbname]=tablename&SelectedTables[otherdb]=othertable&nohtml=1


Published: Saturday, October 13th, 2007

Clock

What To Do When You Can't Cron

One pet peeve that I have with 1and1 hosting was their lack of Cron Jobs. Cron Jobs are basically the Unix method of scheduling stuff, much like the Windows Task Scheduler. If you're on a Unix/Linux host, then they use cron jobs to schedule their internal activity. But not every host is kind enough to give you the same power to do so. Now that I'm on DreamHost, I don't have this problem anymore, but a lot of shared hosts still lock up the cron jobs. And since hosts like 1and1 aren't too reliable with their backups, doing your own backups becomes, more of less, essential. There are, of course, alternatives if your host doesn't provide you with cron jobs, such as phpJobScheduler (used this a lot back in the day) and pseudo-cron.

How The Other Guys Work

The way these work is that, they keep track of tasks and times every time the cron job is called, but you need to include them in one of your highly accessed pages (e.g. Home page). With phpJobScheduler, you can include the file in your PHP code, or you can include it using the img tag. Using the second (2nd) method spits back a transparent GIF of 1 pixel, so nothing will be displayed on your page and you could even use it within plain HTML pages if you needed to. When this is triggered, it checks the database for time of each task, compares that to the current time, them if x > y it runs the task. Simple really. Since they need to be triggered via a user visit, it's never going to be pin point accurate; this is why you need to include it on a page getting high traffic. Some of the scripts even have an option to build in to help with this. It's a sort of buffer time. This says that even if the specific start time of the task hasn't arrived yet, run it anyway if it's within the buffer amount. You would increase or decrease the buffer based on your anticipated traffic.

Popularity: 20%



Page 2 of 2«12