views:

582

answers:

2

Hello, Im new to kohana 3 here, and my understanding of it is very limited.

The problem is instead of displaying the first row, what code should I use when I'll try to get a random row from a certain table?

BTW: I'm using the ORM module.

Thanks

+4  A: 

You can issue the query directly, if you are using MySQL:

SELECT * FROM table LIMIT 1 ORDER BY RAND();

Or with Kohona Query Builder:

$this->db->from('table')->select('*')->limit(1)->orderby(null, 'RAND()')->get();
Alix Axel
If it is a much-used query I would even consider making it a proc since it requires no input.
Xeoncross
Oops, forgot to say that I'm using ORM. How do we do that using ORM? :)
ed
@Xeoncross: Yeah, that may be a good idea. @ed: AFAIK you don't, ORMs aren't meant for that kind of stuff.
Alix Axel
+2  A: 

You can use this (if using MySQL):

ORM::factory('some_model')->order_by(DB::expr('RAND()'))->find();
dusan