Sugar Query for Sugar 7

By
319 views

 

As per the scenario SugarCRM developer needs to query the database. For Sugar 7, new SugarQuery API can be used instead of direct SQL. Sugar Query is a SQL query builder for retrieving data directly from the database and is used within Sugar CRM.  SugarQuery has a very simple interface for building queries.

The basic methods to create a sugar query:

The basic methods you will need to create a query are:

  • select($fields) – accepts an array of fields that you would like to select
  • from($bean) – validates the query against a SugarBean at generation
  • where() – creates an AND where object
  • orWhere() – creates an OR where Object
  • join($link) – accepts a link from the from bean to create the correct join(s)
  • orderBy($field, $direction) – accepts a field and a direction to sort upon
  • groupBy($field) – accepts a field to group by
  • having($condition) – accepts a condition (see below)

compileSql() function can be called on the object for viewing the SQL that is generated and exexute() function executes the query.  An example for retrieving all Accounts with industry Media:

$query = new SugarQuery();

$query->select(array(‘id’,’name’));

$query->from(BeanFactory::getBean(‘Accounts’));

$query->where()->equals(‘industry’,’Media’);

$results = $query->execute();

Need Help? Get Free Consultation


Please leave this field empty.

Get in touch



Please leave this field empty.

CONTACT INFORMATION