REST client for SugarCRM, based on PHP.
PHP version >= 5.3 is required.
Session::connect('http://link/to/your/sugarcrm/service/v4_1/rest.php', 'login', 'password');
To search records use where
method with array
or string
parameter.
Contact::where(array('last_name' => 'Tibbs'))->fetch();
Returns Contact
object where name is Tibbs.
Account::where(array('name' => "LIKE 'Q%'"))->fetchAll();
Returns collection of Account
objects, using for search LIKE
clause. Its also possible using IN
clause in the query:
Account::where(array('name' => "IN ('Airline Maintenance Co', 'Air Safety Inc')"))->fetchAll();
Also possible is use a string
as query:
Contact::where("contacts.last_name = 'Tibbs'")->fetch();
Remember to add a correct table alias.
This method can be chained with other where
methods:
Contact::where(array('last_name' => 'Tibbs'))
->where(array('first_name' => 'John'))
->fetch();
SugarClient allows to create a dynamic method with column name in the method definition:
Account::findByName('Airline Maintenance Co')->fetch();
Return account name where name
is equal to passed argument.
You can dynamically search using findBy
prefix in static method and defining multiple columns e.g.:
Account::findByShippingAddressPostalcodeAndName('60135', 'Airline Maintenance Co')->fetch();
Camel-case is required, column joiner is And
. Parameters are fetching in vararg style, order of parameters are matching to column defined in the method name.
This method can be chained with where
methods:
Contact::findByLastName('Tibbs')
->where(array('first_name' => 'John'))
->fetch();
You can select fields to be returned in the result. By default return all fields.
$account = Account::where(array('name' => "LIKE 'Airline%'"))->select('name', 'phone_office')->fetch();
Now available fields are only name
and phone_office
.
You can return count of the records using count
method.
$count = Account::count(array('name' => "LIKE 'Air%'"));
If you not pass where clause to the count
method then will be returned the number of all records in the module.
When you fetch object you can access to their properties using dynamic fields:
$contact = Contact::where(array('last_name' => 'Tibbs'))->fetch();
$contact->first_name;
$contact->last_name;
To create new record use insert
method:
$contact = new Contact();
$contact->first_name = 'John';
$contact->last_name = 'Doe';
$contact->insert();
insert
method returns id new created record.
To update record use update
method:
$contact = Contact::where(array('last_name' => 'Doe'))->fetch();
$contact->first_name = 'Brad';
$contact->last_name = 'Smith';
$contact->update();
To delete record use delete
method:
Contact::where(array('last_name' => 'Doe'))->delete();
Returns true
if correctly deleted or false
on failure.
To set relation between objects use relatedWith
method.
$account = new Account();
$account->name = 'New Company';
$account->insert();
$contact = new Contact();
$contact->first_name = 'John';
$contact->last_name = 'Doe';
$contact->insert();
$account->relatedWith($contact);
You can reload module attributes using method reload
.
You can join for module related modules. This relations are save in module classes which are in namespace \SugarClient\Module
. Relations types:
belongsTo
- return modulehasMany
- return collection of modules
$contact = Contact::where(array('last_name' => 'Tibbs'))->join('account')->fetch();
Returns Contact
module with joined Account
module. Access to related account is:
$accountName = $contact->account->name;
If you want to specify fields which are returned by the join
method you can pass array whit this fields as second parameter.
$contact = Contact::where(array('last_name' => 'Tibbs'))
->join('account', array('id', 'name'))
->fetch();
This code result that in Account
module will be avaiable only fields: id
and name
.
Sometimes you want to get relations through other relation. You can do this using ->
sparator in a join
method.
$contact = Contact::where(array('last_name' => 'Tibbs'))
->join('account->leads')
->fetch();
To upload Document
file create object, and use uploadFile
method.
$document = new Document(array(
'document_name' => 'new document test',
'revision' => '1'
));
$document->insert();
$content = file_get_contents(Path::join('path', 'to', 'your', 'file_name.txt'));
$fileName = 'file_name.txt';
$document->uploadFile($content, $fileName);
To retrive last file for document use getFile
method.
$document->getFile();
This method return File
object where you can save file using saveTo
method.
$file = $document->getFile();
$file->saveTo(Path::join('destination', 'path', $file->getFileName()));