Page 1 of 1

getCollectionFromDb ignores ORDER BY?

Posted: 22 April 2023, 17:53
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!

Re: getCollectionFromDb ignores ORDER BY?

Posted: 22 April 2023, 19:54
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...

Re: getCollectionFromDb ignores ORDER BY?

Posted: 23 April 2023, 00:07
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.

Re: getCollectionFromDb ignores ORDER BY?

Posted: 23 April 2023, 02:47
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

Re: getCollectionFromDb ignores ORDER BY?

Posted: 24 April 2023, 09:54
by Tenchy
Thanks,

using the getObjectListFromDB() worked as expected