getCollectionFromDb ignores ORDER BY?

Game development with Board Game Arena Studio
Post Reply
Tenchy
Posts: 2
Joined: 05 April 2023, 15:00

getCollectionFromDb ignores ORDER BY?

Post by Tenchy »

Hi,

I'm trying to get the data from server and give it back to the js in the getAllDatas function, but when the data gets to the js it's always sorted using the id not the value that was given to the SQL request

This is my code:

Code: Select all

$sql = "SELECT planet_id id, is_hidden hidden, position pos FROM planet ORDER BY pos ASC";
$result['planets'] = self::getCollectionFromDb( $sql );
in JS I have this code:

Code: Select all

for (var i in this.gamedatas.planets)
{
     var p = this.gamedatas.planets[i];
     this.showPlanetOnTable(p.id, p.hidden, p.pos, this.gamedatas.planets_extra[p.id].css_name);
}

showPlanetOnTable : function(planet_id, is_hidden, pos, css_name)
{
     console.log('id: '+planet_id+' hidden: '+is_hidden+' pos: '+pos+' css_name: '+css_name);
     dojo.place(this.format_block('jstpl_planet', 
     {
         planet_css : css_name,
         planet_id : planet_id
      }), 'planets');
},
Why isn't my data sorted (now it gets sorted by the id)
Thanks!
User avatar
tchobello
Posts: 694
Joined: 18 March 2012, 13:19

Re: getCollectionFromDb ignores ORDER BY?

Post by tchobello »

have you tried to place pos as first field in your getCollection request ?

If you don't need an associative array, try with getObjectListFromDB instead...
User avatar
Blacktango
Posts: 539
Joined: 18 April 2015, 12:15

Re: getCollectionFromDb ignores ORDER BY?

Post by Blacktango »

Yeah, the getCollectionFromDb() function probably give you an associative array with IDs as keys.
So when you loop over it, you lose the sort by pos.
User avatar
quietmint
Posts: 299
Joined: 31 July 2017, 00:28

Re: getCollectionFromDb ignores ORDER BY?

Post by quietmint »

It's a JavaScript issue. Object property order is NOT maintained. Don't use a JS object/associative array (use a JS array/plain array instead) if the order matters.

https://stackoverflow.com/questions/552 ... erty-order
Tenchy
Posts: 2
Joined: 05 April 2023, 15:00

Re: getCollectionFromDb ignores ORDER BY?

Post by Tenchy »

Thanks,

using the getObjectListFromDB() worked as expected
Post Reply

Return to “Developers”