Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

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

  • liubov
  • liubov's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • (=) 10 mn and it's ready!
  • Posts: 279
  • Thank you received: 36
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 ?

Please Log in or Create an account to join the conversation.

Last edit: by liubov.

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

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()');

Please Log in or Create an account to join the conversation.

Last edit: by Romkabouter.

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

  • liubov
  • liubov's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • (=) 10 mn and it's ready!
  • Posts: 279
  • Thank you received: 36
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 :)

Please Log in or Create an account to join the conversation.

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

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

Please Log in or Create an account to join the conversation.

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

  • liubov
  • liubov's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • (=) 10 mn and it's ready!
  • Posts: 279
  • Thank you received: 36
Yes, that what I did,

with exploded ORM,

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

thxs.

Please Log in or Create an account to join the conversation.

Last edit: by liubov.
  • Page:
  • 1
Time to create page: 0.088 seconds

Les générateurs de code je les connais presque tous pour les avoir essorés en long en large et en travers ... mais celui ci c'est vraiment un outil formidable de simplicité et d'efficacité !
Marc. (liubov - Forum)

Get Started