6

Programmatically speaking, is there a way to fetch an array or collection of SugarCRM bean objects?

That is, let's say I wanted to fetch a number of account rows that included the word Bank in their name. With raw SQL, I'd do something like this

SELECT * 
FROM Accounts 
WHERE name LIKE '%Associates%`;

Is there a way using the SugarCRM ORM to so something similar? If not, how do SugarCRM programmers typically handle this situation? I realize I could hack something together by selecting a list of IDs from the database

$db         = DBManagerFactory::getInstance();        
$result     = $db->query('SELECT id FROM Accounts where name LIKE "%Banking%"');
$accounts   = array();
while($row = $db->fetchRow($result))
{            
    $accounts[] = BeanFactory::getBean('Accounts', $row['id']);
}

but in most ORM's that would be considered inefficient, and bad practice. Is there a better way?

(Perfectly ready for the answer to be "No, there's not way to do that". I'm new to the platform and trying to get my bearings)

Alana Storm
  • 164,128
  • 91
  • 395
  • 599

3 Answers3

7

Rather use

$bean = BeanFactory::getBean('Accounts');
$account_list = $bean->get_full_list("", "accounts.name like '%Associates%'");

As get_list will give you what you have defined for list_max_entries_per_page.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
davidboris
  • 196
  • 3
6

Here is a great resource for different ways to use the standard SugarBean versus SQL: here

For your example:

$bean = BeanFactory::getBean('Accounts');
$account_list = $bean->get_list("", "accounts.name like '%Associates%'");
egg
  • 1,736
  • 10
  • 12
3

This is an old question but for future readers, SugarBean methods get_list() and get_full_list() seem to be deprecated and it is advised to use SugarQuery instead.

$bean = BeanFactory::getBean('Accounts');
$query = new SugarQuery();
$query->from($bean, array('team_security' => false));
$query->where()->contains('name', 'Associates');
$account_list = $query->execute();
Indrek Ots
  • 3,701
  • 1
  • 19
  • 24