Retrieving a random record in Symfony using Propel

Monday, April 21st, 2008

Just a quicky, as I had to spend 10 mins looking this up hopefully it’ll save someone else time later.

I need to retrieve a random user from the users table, but didn’t know how to express a random record… this method will return a single random user, or can take an argument to return more than 1
UserPeer

	public static function getRandomUsers($num=1){
		$c = new Criteria();
		$c->addAscendingOrderByColumn('rand()');
		$c->setLimit($num);
		$users = UserPeer::doSelect($c);
		return $users;	
	}

Simple. (and probably documented better somewhere else)

Written by baj | Posted on April 21st, 2008 in symfony |