Documentation

Broker

Methods


Sometimes, when rendering complex interfaces, various locations uploads data associated with the same table.

For example, when retrieving names, associated with user CRM items.

For this purpose, abstract class Service\Broker was developed to avoid repeated queries.

Its main objective is an easy access to bound data with caching.


Please remember, this service protects from repeated queries only if such data was already retrieved using this service.
It's not recommended to make queries in the cycle as follows:
    while($item = $list->fetch())
    {
       $data = $broker->getById($item['USER_ID']);
    }

This class is an abstract. Method and format of retrieved data are defined Descendants.

Broker instance must be retrieved via container.

Method Description Available from version
public function getById(int $id)
Returns data by $id. When data was uploaded previously, they will be sourced from cache.
public function getBunchByIds(array $ids): array
Returns data by $ids array, where key - ID, value - its data.
When data was uploaded previously, they will be sourced from cache. Separate query is performed for retrieving missing data in cache.

Descendants


Service\Broker\User

Provides user data.

Each user record is an array with the following structure:

  • ID - identifier;
  • NAME - name;
  • SECOND_NAME - middle name;
  • LAST_NAME - last name;
  • TITLE - title;
  • WORK_POSITION - position;
  • FORMATTED_NAME - full name;
  • SHOW_URL - profile link;
  • PERSONAL_PHOTO - avatar file ID;
  • PHOTO_URL - avatar link.

This class has an additional method:

Method Description Available from version
public function getName(int $id): ?string
Returns full user name by its ID.

Service\Broker\Contact

Provides data about CRM contacts.

Each contact record is an CRM object from Bitrix\Crm\ContactTable.


Service\Broker\Company

Provides data about CRM companies.

Each record about company is CRM object from Bitrix\Crm\CompanyTable.


Service\Broker\TypePreset

Separately, a preset broker exist for SPA settings.

It's not a descendant of class Service\Broker, but operates based on similar principles.


Class will be refactored and its not recommended to use it directly, only via an event.

It has the following public methods:

Method Description Available from version
public function getList(): array
Returns list of presets.
public function getPredefinedPresets(): array
Returns list of predefined presets.
public function collectPresetsByEvent(): array
Throws the event onCollectTypePresets for CRM module. Returns array with presets of corresponding structure as the result of executed event handler by the key presets.
Returns array with presets retrieved from handlers.
public function getCategories(): array
Returns array with sections for presets.

Example

use Bitrix\Crm\Service;

$contactBroker = \Bitrix\Crm\Service\Container::getInstance()->getCompanyBroker();

// here the contact will be queried
$contact = $contactBroker->getById(1);

echo $contact->getName();

// here data will be retrieved from cache
$contact = $contactBroker->getById(1);
© «Bitrix24», 2001-2024