Jason
Jason

Reputation: 1977

cf9 orm entityload in random order

How would I do an entityload with its resultset being in random order?

So if I do entityload("modelName"), how Would set it's sort order so that it is randomly different each time I call it?

Many Thanks

Upvotes: 1

Views: 585

Answers (2)

Jason
Jason

Reputation: 1977

Another approach I used is to create a sortOrder column in my table (and model) and a function to randomly populate it. probably not as efficient as jCaito's option, but had some benefits.

public function getRandom(){
    randomizer();
    return entityload("provider", {}, "sortOrder");
}

private void function randomizer(){
    source = entityload("provider");
    for(i=1;i<=ArrayLen(source);i++){        
        source[i].setSortOrder(randRange(1,999));        
    }
}

Upvotes: 0

jcaito
jcaito

Reputation: 51

Well, the short answer is it is really difficult with entityLoad(). However, with HQL, it's actually not too bad.

Most DB languages will have a random function. So, using the built in art table and an entity, you can use ormExecuteQuery to generate your results:

<cfscript>
hql = " SELECT DISTINCT artName
        FROM art
        ORDER BY RANDOM() ";

results = ormExecuteQuery(hql);

for( art in results ) {
    writeOutput(art & "<br/>");
}
</cfscript>

Because it won't return any null records, you will get a random result set every time.

Hope this helps ^__^

Upvotes: 2

Related Questions