Orderby column RAND() error |
|||
|---|---|---|---|
| Date: | 07/31/2007 | Severity: | Minor |
| Status: | Resolved | Reporter: | phildawson |
| Version: | 1.5.4 | ||
| Keywords: | Libraries, Database Class | ||
Description
The user guide for the orderby method in the Active Record Class explains that the options for the directions are asc or desc or RAND().
If RAND() is used for the second parameter it will produce an error, as it will create an invalid query ORDER BY column RAND()
The solution is to update the guide to use the following code for random ordering.
$this->db->orderby(’RAND()’);
The orderby method in DB_active_rec.php line 408 should then have ‘RAND()’ removed from the array.
function orderby($orderby, $direction = ‘’)
{
if (trim($direction) != ‘’)
{
$direction = (in_array(strtoupper(trim($direction)), array(’ASC’, ‘DESC’, ‘RAND()’), TRUE)) ? ‘ ‘.$direction : ‘ ASC’;
}
$this->ar_orderby[] = $orderby.$direction;
return $this;
}
Code Sample
$this->db->orderby('column', RAND());
Expected Result
Table ordered by random.
Actual Result
MySQL Error
Comment on Bug Report
| Posted by: Derek Allard on 10 August 2007 6:24am | |
|
|
Thanks for taking the time to report this. It seems that every database has a different way to accomplish random order. For example:
MySQL uses ORDER BY RAND()
For now, we’ve removed mention of this from the user guide, but if you wanted to add the note on rand() for MySQL databases with the user contributed notes feature, that would be appreciated. |
| Posted by: barbazul on 27 October 2007 12:26pm | |
|
|
Why not setting a common word for random across all drivers ie ci_random_ugly_name so when users call orderby(’ci_random_ugly_name’); each driver would parse it its own way and you can still have a unified form of documenting it. I have all copyrights on ‘ci_random_ugly_name’ so try to come up with a diferent name for it (: |
| Posted by: Derek Allard on 27 October 2007 12:36pm | |
|
|
Yup, that is the long term solution. Someone just needs to code it in, so for now I simply removed it. If you code it up, I’ll push to use your name |
| Posted by: barbazul on 27 October 2007 2:59pm | |
|
|
OK, I prepared a first attempt at fixing this based on information gathered from the internet. Testing in the different platforms is still required since I only have a mysql server installed.
File system/database/DB_active_record.php
File system/database/drivers/mssql/mssql_driver.php
File system/database/drivers/mysql/mysql_driver.php
File system/database/drivers/mysqli/mysqli_driver.php
File system/database/drivers/oci8/oci8_driver.php
File system/database/drivers/odbc/odbc_driver.php
File system/database/drivers/postgre/postgre_driver.php
File system/database/drivers/sqlite/sqlite_driver.php
|
| Posted by: barbazul on 1 November 2007 10:12pm | |
|
|
I’ve opened a forum thread to keep this bug alive as I personally don’t believe this bug to be really resolved http://codeigniter.com/forums/viewthread/64145/ |
| Posted by: Derek Allard on 18 December 2007 11:14am | |
|
|
A note, ‘random’ has been added into order_by(), and replaces the old ‘RAND()’ keyword. |
