getBean parameters not behaving the way I would expect.

I was excited to find in Link2.php that getBeans can take parameters to filter the beans that are retrieved.

I want to get the latest related record in code.

I was hoping to get it by ordering by date_modified and using a limit of 1

$filter = array(
    'order_by'=>'date_modified desc',
    'limit' => '1'
);
$bean = $recordBean->$link->getBeans($filter)

Similarly to what I would expect from a query like

select * from accounts
order by date_modified desc
limit  1

But it's not giving me the result I expected. It seems to be getting one seemingly random record and ignoring the sort.

I can get records newer than a given date by adding a where clause so the parameters are indeed being considered.

    $filter = array(
      'where' => array(
      'lhs_field' => 'date_modified',
      'operator' => '>=',
      'rhs_value' => $date)
    );

Any thoughts on ways to get the latest without having to get them all, sort them and then get one?

I'm trying to limit how much data I pull, there could be thousands of related records.

thanks,

Francesca

Parents
  • Although the docblock above Link2::getBeans says `order_by` the actual key you need it `orderby`, it's been showing the incorrect value in the docblock for a while. Not that you need to hear as you have the bean as this approach is less lines, but an alternative is also using SugarQuery should you need to fallback to write your own queries in a database abstract method. But anyway you should fine using `orderby` works as you desire.

Reply
  • Although the docblock above Link2::getBeans says `order_by` the actual key you need it `orderby`, it's been showing the incorrect value in the docblock for a while. Not that you need to hear as you have the bean as this approach is less lines, but an alternative is also using SugarQuery should you need to fallback to write your own queries in a database abstract method. But anyway you should fine using `orderby` works as you desire.

Children