Views: 3132
Last Modified: 30.03.2022

When not all object fields are completed with data and you need to fill in the missing data, do not use the following approach:

// initially only ID and NAME are available
$author = \Bitrix\Main\Test\Typography\EO_Author::wakeUp(
	['ID' => 17, 'NAME' => 'Name 17']
);

// we need to write LAST_NAME, retrieving it from database
$row = \Bitrix\Main\Test\Typography\AuthorTable::getByPrimary($author->getId(),
	['select' => ['LAST_NAME']]
)->fetch();

// adding value to the object
$author->setLastName($row['LAST_NAME']);

In this case, the value will be deemed as a newly set, but not an actual value (which, theoretically can lead to unforeseen collisions in further object handling).

The correct way to use the name method for object fill:

// initially only ID and NAME are available
$author = \Bitrix\Main\Test\Typography\EO_Author::wakeUp(
	['ID' => 17, 'NAME' => 'Name 17']
);

// add LAST_NAME from the database
$author->fillLastName();

In addition to name methods, a generic method is available as well. It provides a significantly more options then other generic methods:

$author = \Bitrix\Main\Test\Typography\EO_Author::wakeUp(17);

// filling several fields
$author->fill(['NAME', 'LAST_NAME']);

// filling all presently unfilled fields
$author->fill();

// filling fields by mask, for example, all unfilled scalar fields
$author->fill(\Bitrix\Main\ORM\Fields\FieldTypeMask::SCALAR);

// unfilled scalar and user fields
$author->fill(
	\Bitrix\Main\ORM\Fields\FieldTypeMask::SCALAR
	| \Bitrix\Main\ORM\Fields\FieldTypeMask::USERTYPE
);

/*
 * Masks are available as follows:
 *
 * SCALAR - scalar fields (ORM\ScalarField)
 * EXPRESSION - expressions (ORM\ExpressionField)
 * USERTYPE - user fields
 * REFERENCE - relations 1:1 and N:1 (ORM\Fields\Relations\Reference)
 * ONE_TO_MANY - relations 1:N (ORM\Fields\Relations\OneToMany)
 * MANY_TO_MANY - relations N:M (ORM\Fields\Relations\ManyToMany)
 *
 * FLAT - scalar fields and expressions
 * RELATION - all relations
 *
 * ALL - absolutely all available fields
 */

If you need to additionally fill in several objects, it's strongly not recommended to perform this command in a loop: this will result to a significant number of queries to the database. Handling several objects of the same type requires a similar Collection method.





Courses developed by Bitrix24