Potresti incapsulare rapidamente la funzione in una tabella, dato che non si sa mai quale adapter venga usato:
class MyTable extends Zend_Db_Table_Abstract {
public function randomSelect($select = null) {
if ($select === null) $select = $this->select();
if (!$select instanceof Zend_Db_Select) $select = $this->select($select);
$adapter = $this->getAdapter();
if ($adapter instanceof Zend_Db_Adapter_Mysqli) {
$select->order(new Zend_Db_Expr('RAND()'));
} else if ($adapter instanceof Zend_Db_Adapter_Dblib) {
$select->order(new Zend_Db_Expr('NEWID()'));
} else {
throw new Exception('Unknown adapter in MyTable');
}
return $select;
}
}
$someSelect = $table->select();
$table->randomSelect($someSelect); // add it to an existing select
$select = $table->randomSelect(); // or create one from scratch
Inoltre, ho trovato da qualche parte un articolo che ora non riesco più a recuperare, che suggeriva di provare qualcosa come:
$select->order(new Zend_Db_Expr('0*`id`+RAND()));
per aggirare l'ottimizzatore di query di MSSQL e convincerlo a calcolare un nuovo valore per ogni riga.
Nota: Zend Framework 1 è fuori supporto dal 2016; se stai iniziando un nuovo progetto, valuta Laminas (il fork mantenuto) o un framework più recente.