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)
Despite the history of the term, the easiest way to think of a SugarCRM bean is as an ActiveRecord model class, and a model class that brings along extra system functionality for free. We’re not going to go too in depth to that extra functionality today, but if you go digging deeper on your own keep this in mind.
While SugarCRM doesn’t provide collection models, beans do contain a number of methods for fetching array s of model objects. First, a note about a method we’ve already seen. The retrieve_by_string_fields method will search your SugarCRM database and return the first item it finds.
The SugarBean class supports all the model operations to and from the database. Any module that interacts with the database utilizes the SugarBean class, which contains methods to create, read/retrieve, update, and delete records in the database (CRUD), as well as fetch related records.
Fetching a parent record is similar to fetching child records in that you will still need to load the relationship using the link: An example of this is to load a contact and fetch its parent account: This article covers how to extend core SugarBean objects to implement custom code.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With