• [solved] How to use sortby in getCollection? Can anybody help?#

  • lettis Reply #1, 4 months, 1 week ago

    Reply
    We were trying to get a collection of objects of a table of our database (sorted by the column "name"). Unfortunately the following code was leading to an error (Fatal error: Call to a member function newQuery() on a non-object):

    $path = $modx->getOption('core_path') . 'components/folder/';
    $result = $modx->addPackage('folder',$path . 'model/','prefix_');
    
    $c = $xpdo->newQuery('tablename');  // this line leads to the error described above
    $c->sortby('name','ASC');  
    $db_missing = $modx->getCollection('tablename',$c);
    


    Can anybody tell us what we have to do to make this work? We know that we cannot use newQuery on a tablename but we don´t know what we exactly have to change.

    Can anybody help us?

    Letti


  • Wanze Reply #2, 4 months, 1 week ago

    Reply
    I think your $xpdo variable is not defined and should be $modx.
    (The modx class extends xpdo):
    $result = $modx->addPackage('folder',$path . 'model/','prefix_');
    if($result){
      $c = $modx->newQuery('tablename');
      $c->sortby('name','ASC'); 
      $db_missing = $modx->getCollection('tablename',$c);
    }

    Also you could check the return value of addPackage: it should be true, when the package was added successfully.


  • lettis Reply #3, 4 months, 1 week ago

    Reply
    Perfect! That makes it work. Thanks for your help!