Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
It would be incredibly useful to have the ability to do EFQueries with random ordering. This would allow the user to, eg. select a random node.
Comments
Comment #1
Dave ReidorderRandom is already a part of SelectQueryInterface, just need to extend it to EFQ, but I'm not sure it can be done in a cross-platform way like DBTNG.
Comment #2
naught101 CreditAttribution: naught101 commentedhttp://eric.lubow.org/2010/databases/mongodb/getting-a-random-record-fro...
Horrible. I was about to start looking at writing a patch for this, but bojanz reminded me that EFQ works with MongoDB as well, and that method looks hellish.
http://groups.google.com/group/mongodb-user/browse_thread/thread/aae871e... looks far more sensible , but I have no idea how expensive this would be.
http://groups.google.com/group/norm-mongodb/browse_thread/thread/625b2ef...
mentions that methods like these can be expensive on large data sets?
The reason I bring this up is due to #1167024: Add support for devel to generate relations, where we want to select random entities to create relations between, but this could equally be useful in things like the random image block (I'm probably the only one in the world who uses that anyway, but it's a good example).
Comment #3
chx CreditAttribution: chx commentedThat method on MongoDB is http://xkcd.com/221/ this.
Comment #4
rv0 CreditAttribution: rv0 commentedBumped into this when writing a random header image block. Using EFQ because I have to reduce the set depending on field values.
Would be useful indeed.
Comment #5
chx CreditAttribution: chx commentedThis link from #2 http://eric.lubow.org/2010/databases/mongodb/getting-a-random-record-fro... does look doable so if you are interested please go ahead with a patch.
Comment #6
chx CreditAttribution: chx commentedActually this needs to be done because if we would store a random with every entity then even MySQL could gain a much much faster order by random simply by ordering on that. Win.
Comment #7
chx CreditAttribution: chx commentedActually if UUIDs get in then ordering on that , I would like to believe, is sufficiently random. However, as #1346204: [meta] Drupal 8 Entity API improvements does not even list an UUID for entities issue I am leaving this open.
Comment #8
agoradesign CreditAttribution: agoradesign commentedCould we please revitalize this issue? Just needed this functionality by myself and was surprised, how inconvenient it is to solve to get the random order - you'd either have to use a normal SelectQuery instead entity query, or - if you don't have too many concerned entities in your DB - query all without range restriction, do a random sort on the node ids, slice the array and then load the nodes/entities...
Comment #10
TheDucksLover CreditAttribution: TheDucksLover commentedYeah it would be great to have a solution out of the box.. As agoradesign said, atm I'm also getting all the entity IDs to finally use an array_rand while loading them, but well :/
Comment #12
tomasnagy CreditAttribution: tomasnagy as a volunteer commentedYou can achieve this quite simple by adding a tag to your query.
First hook to create the tag.
Then just use add the tag to the required query.
Hope this helps
Comment #13
Chris Gillis CreditAttribution: Chris Gillis commented@tomasnagy, does this add the random order after any existing order by statements? E.g. I want to order by foo, but for rows where foo is equal I want a random sort....
Comment #14
tomasnagy CreditAttribution: tomasnagy as a volunteer commented@Chris this basically adds 'ORDER BY RAND()' at the end of the query from what I've found in core.
Comment #20
jonathan1055 CreditAttribution: jonathan1055 as a volunteer commentedI know we can achieve this with
hook_query_TAG_alter()
but it would be nice to have a solution built in.as @daveReid said, way back,
But a good many years and versions have passed since then, so are there any new ideas now?
Comment #22
lindsay.wils CreditAttribution: lindsay.wils commentedI swear I used to have this working with the tag_alter, but no longer seems to be. Can anyone confirm if they have this still working in D 9.2.6?
Thanks
Comment #27
agoradesign CreditAttribution: agoradesign commentedHey Lindsay,
my answer comes a little bit late. for me, it still works. I've just used that again for a current project on 9.5.3