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.065 seconds

  I found "cook" two days ago. Played around with it for a day and then within a day got rid of two legacy applications in IBM Lotus Domino Notes and replaced them with "cook". It was really easy. A piece of cake. Actually it was even easier than cooking. Cooking involves more work than developing with "cook". What can I say about "cook"? Great application great price for what it delivers great forum with a lot of information and fast responses active and friendly community fast forward moving development cycle So what should I say "Monsieur Le Cook"? Keep the good work going it will lead to success. Having become a paying member in the words of Edith Piaf may I say ..... non rien de rien ... je ne regrette rien. Vive La France.
FK (JED)
         

Get Started