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

I jumped and started to work on a demo component... but 2 days later this demo component became the real component. I just showed today the end result to my customer and he turned to me and said... "this is more than I expected"... All of this is because Cook did cut about 70% of my work and provided me more ways to improve the usability of the component. The end result was 17 tables all related between than to generate a full dashboard for the travel agents. Thanks for Cook developers for such great tool. This component would not be possible to be done at short time with all the features in it
Griiettner (Forum)  

Get Started