Custom Propel Criteria Tips

Tuesday, 11 December 2007

Here’s a quick Propel tip. While working on a site recently, I had to figure out a way to randomly pull some records from a database using Propel from within a Symfony application. This is a bit of a hack as it will only work if you’re using MySQL, but it’s an elegant way (considering the alternatives) to get some random results. I assume you can use this method with another database. You’d just have to figure out what functions to call instead of the MySQL specific ones like RAND() and DAYOFWEEK().

From within an action, you can do something like this:

$c = new Criteria();
$c->addAscendingOrderByColumn('RAND()');
$c->setLimit(20);
$this->photos = PhotoPeer::doSelect($c);

… which will get you a set of 20 random photo objects.

Something a bit more interesting is using a custom criteria. This basically lets you use any MySQL specific function from within propel without resorting to building a query completely from scratch. Take this example:

$c->add(PodcastPeer::DATE, 'DAYOFWEEK('.PodcastPeer::DATE.')='.$this->filters['day_of_week'], Criteria::CUSTOM);

Basically, I wanted to be able to use a symfony-type filter (just like the ones that are generated automatically by the propel admin generators) to filter podcasts by certain days of the week. So someone could, for example, choose only to listen to those podcasts that were released on Wednesdays. This snippet of code basically allows me to run this sql query:

SELECT * FROM podcast WHERE DAYOFWEEK(podcast.DATE)=1

… where 1 = Sunday, 2 = Monday, etc.


Making a Custom Apache Install Start at Boot on Mac OS X

Thursday, 06 December 2007

A Bit Of Background

I do all of my web-development on a Mac OS X machine (a MacBook Pro, to be exact) using a local install of Apache, PHP and MySQL. I’m using a custom built Apache 2 install and configuration (for various reasons) so the default Web Sharing control panel does not control the starting and stopping of my webserver. In general, my setup works really well and has led to a really integrated workflow that I can use and develop with even while offline. One thing that had always frustrated me about my custom setup though, is that after a restart, apache had to be manually started before I could get back to work. This is one of those little things that can be annoying, but not really annoying enough to do anything about. Well, I finally decided to fix it.

Mac OS X has a system startup program called launchd which allows us to start the apache webserver automatically on each system boot. You can read more about launchd at the official apple page.

Making Our Own launchd File

To create the launchd plist file, I used Lingon which is an open source graphical front-end for creating these types of files (version 2.0.2 as of this writing). You can use this article as a reference for creating your own, or simply download the apache2 launchd plist file, unzip it, and place the file into /Library/LaunchDaemons/. This file assumes that you have apache2 installed in /usr/local/apache2/.

Once Lingon is launched, you’ll be presented with a window that’ll show you an overview of all of your currently installed launchd files. Click the “new” button to create a new file. You’ll want a Daemon file since apache needs to run at startup and as root:

Screenshot of step 1

Once you’ve got a new file ready for editing, fill out the “Name” field. I’ll be using “com.mirthlab.launchd.apache2”:

Screenshot of step 2

Next, fill out the “What” field. This will be the command we want to run at startup and I’ll be using “/usr/local/apache2/bin/apachectl start” but this may differ if you have Apache installed in a different location:

Screenshot of step 3

And finally, fill out the “When” field. For Apache, I checked the box that says “Run it when it is loaded by the system (at startup or login)”:

Screenshot of step 4

Wrapping It All Up

Click “Save” and enter your password when prompted. This is a root level launchd daemon which is why Lingon needs your password.

Restart and you should have Apache ready to go!

NOTE: If you simply downloaded the plist file, you’ll probably have to change this file’s ownership as files in /Library/LaunchDaemons/ must be owned by root.

To change the file’s owner to root, in the terminal, type:

sudo chown root:wheel /Library/LaunchDaemons/com.mirthlab.launchd.apache2.plist

… all on one line.

You can also see this macosxhints.com hint for a tip on using the Sharing Control Panel to start and stop your custom Apache install.


Welcome to the New MirthLab

Wednesday, 05 December 2007

Well, here we go. It’s about time, too. This blog has been a long time coming and I hope to make it a useful resource for all things PHP, Symfony, JavaScript, CSS, Mac, Unix and the like. Most likely it’ll be more of an online reference than anything (documenting things I come across while hacking away on some web-development project) so I hope you find these posts to be a useful resource.