Search code examples
phpodooodoo-9

Odoo/PHP: how can I set "order by" in API call?


Using the ripcord XML-RPC client and making a call like the following, how can I tell it by which field(s) to sort the records?

$models = ripcord::client($cfg['url'] . '/xmlrpc/2/object');
$srch = $models->execute_kw($cfg['db'], $cfg['uid'], $cfg['pw'], 'account.move.line', 'search_read', array(array(array('account_id', '=', 174), array('date', '>=', '2016-01-01'))));

Using Odoo 9 Community Edition.


Solution

  • The signature of search_read is:

    def search_read(self, cr, uid, domain=None, fields=None, offset=0, limit=None, order=None, context=None):
    

    I am not very familiar with PHP, but something like this should work:

    $models = ripcord::client($cfg['url'] . '/xmlrpc/2/object');
    $srch = $models->execute_kw(
        $cfg['db'],
        $cfg['uid'],
        $cfg['pw'],
        'account.move.line',
        'search_read',
        array(
            array(
                array('account_id', '=', 174),
                array('date', '>=', '2016-01-01')
            ),
            NULL,
            NULL,
            NULL,
            'date desc, id',
        )
    );
    

    We pass NULL for fields, offset and limit to use the default value and order is a comma-separated list of fields to sort on. In the above example it will return the move lines sorted by date in descending order and then by id (if some move lines have the same date) in ascending order.