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.