Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-11-2008, 10:15 AM
Junior Member
 
Join Date: Feb 2008
Posts: 2
Default ORDER BY RANDOM() not working

Is there any way to use "ORDER BY RANDOM()" in Zend_Db? I tried it in Zend_Db_Select, I tried it in plain Zend_Db, I tried it with Zend_Db_Expr and it all ended with the same effect - no effect that is.
Or maybe someone knows any other way to bring the same functionallity, which is selecting a bunch of random rows from a database? Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-11-2008, 11:49 AM
Junior Member
 
Join Date: Feb 2008
Posts: 2
Default Found the answer

OK, I found out how to do it - plain Zend_Db + Zend_Db_Expr is the answer.
PHP Code:
$res $db->fetchAll(
    
'SELECT * FROM table ORDER BY :random',
    array(
'random' => new Zend_Db_Expr('RANDOM()')
); 
Voila!

This won't work:
PHP Code:
$select $db->select();
$select->from('table')
          ->
order(new Zend_Db_Expr('RANDOM()'));

$res $db->fetchAll($select->__toString()); 
Actually $select->__toString() produces a proper sql query, but bad things happen on a $db->query() level, I suppose in a prepare/execute part as the produced error is something like "FUNCTION table.RANDOM does not exist".

I suppose making something like that
PHP Code:
$select $db->select();
$select->from('table')
          ->
order(new Zend_Db_Expr(':random'));

$res $db->fetchAll(
     
$select->__toString(),
    array(
'random' => new Zend_Db_Expr('RANDOM()')
); 
could work too, but I'm not too sure if putting a placeholder into Zend_Db_Select using Zend_Db_Expr would really work + it doesn't actually look as a nice thing to make things work.

Zend_Db looks nice on a surface, but inside there's a lot of such small nitpicks you have to remember about.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 04:08 PM.