Views: 190
Last Modified: 26.09.2020

For the new API to be more familiar to a developer, the most popular method name was saved: getList. When previously each getList had its own set of parameters and individual uncustomizable behavior, now this method is the same for all entities and is subject to the same rules.

The entity BookTable, sourced as an example, is not an exception. Which parameters are received by the method BookTable::getList?

BookTable::getList(array(
	'select'  => ... // fields name to be retrieved in the result
	'filter'  => ... // filter description for WHERE and HAVING
	'group'   => ... // directly set fields used to group the result
	'order'   => ... // sorting parameters
	'limit'   => ... // number of entries
	'offset'  => ... // offset for limit
	'runtime' => ... // dynamically determined fields
));

getList always returns the object DB\Result, used to retrieve data via the method fetch():

$rows = array();
$result = BookTable::getList(array(
	...
));
while ($row = $result->fetch())
{
	$rows[] = $row;
}

To get all entries, you can use the method fetchAll():

$result = BookTable::getList($parameters);
$rows = $result->fetchAll();

// or just as follows:
$rows = BookTable::getList($parameters)->fetchAll();

Now, let's review all parameters in more detail.

select

Parameter `select` is defined as an array with entity field names:

BookTable::getList(array(
	'select' => array('ISBN', 'TITLE', 'PUBLISH_DATE')
));

// SELECT ISBN, TITLE, PUBLISH_DATE FROM my_book

When due to some reasons you do not like original field names in the result, you can use aliases:

BookTable::getList(array(
	'select' => array('ISBN', 'TITLE', 'PUBLICATION' => 'PUBLISH_DATE')
));

// SELECT ISBN, TITLE, PUBLISH_DATE AS PUBLICATION FROM my_book

In this example, the field name `PUBLISH_DATE` replaced with `PUBLICATION` and specifically this name will be used in the resulting array.

When all fields must be selected, you can use the '*' character:

BookTable::getList(array(
	'select' => array('*')
));

In this case, only the fields ScalarField will be used, and ExpressionField and associations with other entities won't be affected - they always can be indicated directly.

filter

Parameter `filter` inherited the format iblock filter:

// WHERE ID = 1
BookTable::getList(array(
	'filter' => array('=ID' => 1)
));

// WHERE TITLE LIKE 'Patterns%'
BookTable::getList(array(
	'filter' => array('%=TITLE' => 'Patterns%')
));

Filter can be multi-levelled with AND/OR:

// WHERE ID = 1 AND ISBN = '9780321127426'
BookTable::getList(array(
	'filter' => array(
		'=ID' => 1,
		'=ISBN' => '9780321127426'
	)
));

// WHERE (ID=1 AND ISBN='9780321127426') OR (ID=2 AND ISBN='9781449314286')
BookTable::getList(array(
	'filter' => array(
		'LOGIC' => 'OR',
		array(
			// 'LOGIC' => 'AND', // elements are conjoined via AND
			'=ID' => 1,
			'=ISBN' => '9780321127426'
		),
		array(
			'=ID' => 2,
			'=ISBN' => '9781449314286'
		)
	)
));

Full list of comparative operators that can be used in filter:

  • = equal (works with array as well)
  • % substring
  • > more
  • < less
  • @  IN (EXPR), DB\SqlExpression array or object is passed as a value
  • !@  NOT IN (EXPR), array or object DB\SqlExpression is passed as value

  • != not equal to
  • !% not substring
  • >< between, passes array (MIN, MAX) as a value
  • >= more or equal
  • <= less or equal
  • =% LIKE
  • %= LIKE
  • == boolean expression for ExpressionField (for example, for EXISTS() or NOT EXISTS())

  • !>< not between, passes array(MIN, MAX) as a value
  • !=% NOT LIKE
  • !%= NOT LIKE
  • '==ID' => null strict comparison with NULL by ID
  • '!==NAME' => null strict comparison with NULL by NAME
Attention! When comparison operator = is not indicated directly, executes LIKE by default. In this case, uses filter code from Iblock module.

Fields type int have:
- before released object ORM Starting from main module version 18.0.3. : = (equality comparison, array expands into set of OR conditions =)
- after object ORM release: IN().

group

The parameter `group` lists fields to be grouped:

BookTable::getList(array(
	'group' => array('PUBLISH_DATE')
));

In majority of cases, there is no need to directly indicate grouping - system does it automatically. Find more details below in section dynamically determined fields.

order

The parameter `order` allows indicating sorting order:

BookTable::getList(array(
	'order' => array('PUBLISH_DATE' => 'DESC', 'TITLE' => 'ASC')
));

BookTable::getList(array(
	'order' => array('ID') // sorting direction - ASC
));

offset/limit

Parameters `offset` and `limit` help limiting number of selected entries or implementing per page selection:

// 10 recent entries
BookTable::getList(array(
	'order' => array('ID' => 'DESC')
	'limit' => 10
));

// 5th page with entries, 20 per page
BookTable::getList(array(
	'order' => array('ID')
	'limit' => 20,
	'offset' => 80
));

runtime

Calculated fields (ExpressionField), mentioned in the first section of this lesson are needed mostly not in the entity description, but for selecting various calculations with grouping.

The most simple example, calculation of number of entities, can be performed as follows:

BookTable::getList(array(
	'select' => array('CNT'),
	'runtime' => array(
		new Entity\ExpressionField('CNT', 'COUNT(*)')
	)
));
// SELECT COUNT(*) AS CNT FROM my_book

Calculated field in this example not just converts a field value, but implements arbitrary SQL expression with function COUNT.

After registering a field in`runtime`, it can be referred not only in `select` section, but in other sections as well:

BookTable::getList(array(
	'select' => array('PUBLISH_DATE'),
	'filter' => array('>CNT' => 5),
	'runtime' => array(
		new Entity\ExpressionField('CNT', 'COUNT(*)')
	)
));
// select days, when more than 5 books were released
// SELECT PUBLISH_DATE, COUNT(*) AS CNT FROM my_book GROUP BY PUBLISH_DATE HAVING COUNT(*) > 5

Note. This example shows the above mentioned automatic grouping - system has identified, what must be grouped by the field PUBLISH_DATE.

When calculated field is required only in section `select` (as in most cases), the section `runtime` is optional: you can save time by inserting expression directly into `select`.

BookTable::getList(array(
	'select' => array(
		new Entity\ExpressionField('MAX_AGE', 'MAX(%s)', array('AGE_DAYS'))
	)
));
// SELECT MAX(DATEDIFF(NOW(), PUBLISH_DATE)) AS MAX_AGE FROM my_book

Note, another already existing Expression field AGE_DAYS was used inside the new Expression field MAX_AGE. This way, the system allows using inserted expressions, which will be sequentially expanded in the final SQL code.

Fields of any other types can be registered in the section `runtime`, not only Expression fields. The `runtime` mechanism operates by adding a new field to an entity as if it was originally described in the method `getMap`. However, such files are going to be visible only once in a single query: in the next call for getList such field won't be available, it must be registered again.

Selection caching

Caching of a specific section is available from version 16.5.9. No need to describe anything in the entity itself. No caching by default.

The key cache is added to getList parameters:

$res = \Bitrix\Main\GroupTable::getList(array("filter"=>array("=ID"=>1), "cache"=>array("ttl"=>3600)));

The same is implemented in via the Query:

$query = \Bitrix\Main\GroupTable::query();
$query->setSelect(array('*'));
$query->setFilter(array("=ID"=>1));
$query->setCacheTtl(150);
$res = $query->exec();

It's possible that cached selection result will contain the object ArrayResult.

Selections with JOIN are not cached by default. However, you can optionally cache it:

"cache"=>array("ttl"=>3600, "cache_joins"=>true);
//or
$query->cacheJoins(true);

Cache reset is performed in any method add/update/delete. Forced cache reset for the table:

/* Example for user table */
\Bitrix\Main\UserTable::getEntity()->cleanCache();

Administrators can restrict TTL update and caching.


Note: When selection data formatting is required, use fetch_data_modification.




Courses developed by «Bitrix», Inc.