Welcome, Guest
Username: Password: Remember me

TOPIC: Randomize a Select Query

Randomize a Select Query 19 Aug 2017 12:06 #15273

  • liubov
  • liubov's Avatar
  • Offline
  • Elite Member
  • (=) 10 mn and it's ready!
  • Posts: 278
  • Thank you received: 35
  • Karma: 22
Hello Cookers,

I am facing a little problem with a query. I want to display a random selection of products (9 out of 20).
Before ORM, this code works like a charm:
    // LIMIT
    $this->setState('list.limit', 9);

    // ORDER
    $this->addOrder( 'rand()' ); 
Query Result : ORDER BY rand(), a.dname asc;

Now, I am trying to update my code with ORM but the RAND() do not work (no warning, no error) but ORDER BY is default.
My code :
            // LIMIT
            $this->setState('list.limit', 9);
                
            // ORDERING
            $orderCol = $this->getState('list.ordering', 'dname');
            $orderDir = $this->getState('list.direction', 'RAND()');

            $this->orm->order(array($orderCol => $orderDir));
                
            break;
Query Result: ORDER BY a.dname ASC;

Any idea to hold that, chief ?
Last Edit: 19 Aug 2017 16:44 by liubov.
The administrator has disabled public write access.

Randomize a Select Query 20 Aug 2017 20:58 #15274

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
You have now a rand function in the direction.
The direction can only be ASC or DESC (ascending of descending)

Try changing
$orderCol = $this->getState('list.ordering', 'dname');
to
$orderCol = $this->getState('list.ordering', 'RAND()');
Last Edit: 20 Aug 2017 20:59 by Romkabouter.
The administrator has disabled public write access.

Randomize a Select Query 21 Aug 2017 09:28 #15275

  • liubov
  • liubov's Avatar
  • Offline
  • Elite Member
  • (=) 10 mn and it's ready!
  • Posts: 278
  • Thank you received: 35
  • Karma: 22
ho yes, I made a mistake in my post.
I tried that for testing but result is not better. (no random display)
$this->orm->order(array( 'dname' =>  'RAND()'));

thank for your help :)
The administrator has disabled public write access.

Randomize a Select Query 24 Aug 2017 08:46 #15276

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
Sorry, did not read correctly.

The way you have your code does not work in orm that way.
But you can still use addOrder since it is a function on the model.

I think your addOrder can not be updated
The administrator has disabled public write access.

Randomize a Select Query 24 Aug 2017 15:15 #15277

  • liubov
  • liubov's Avatar
  • Offline
  • Elite Member
  • (=) 10 mn and it's ready!
  • Posts: 278
  • Thank you received: 35
  • Karma: 22
Yes, that what I did,

with exploded ORM,

and $this->addorder('rand()') method .

thxs.
Last Edit: 24 Aug 2017 15:15 by liubov.
The administrator has disabled public write access.
Time to create page: 0.114 seconds

Get Started