• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/Relation/EntityRelationTable.php
  • Класс: Bitrix\Crm\Relation\EntityRelationTable
  • Вызов: EntityRelationTable::rebind
static function rebind(ItemIdentifier $oldItem, ItemIdentifier $newItem): Result
{
	$childrenRows = self::query()
		->setSelect(['DST_ENTITY_TYPE_ID', 'DST_ENTITY_ID'])
		->where('SRC_ENTITY_TYPE_ID', $oldItem->getEntityTypeId())
		->where('SRC_ENTITY_ID', $oldItem->getEntityId())
		->fetchCollection()
		->getAll()
	;

	$parentsRows = self::query()
		->setSelect(['SRC_ENTITY_TYPE_ID', 'SRC_ENTITY_ID'])
		->where('DST_ENTITY_TYPE_ID', $oldItem->getEntityTypeId())
		->where('DST_ENTITY_ID', $oldItem->getEntityId())
		->fetchCollection()
		->getAll()
	;

	$connection = Application::getConnection();
	$helper = $connection->getSqlHelper();

	$sourceQuery = 'UPDATE %s SET SRC_ENTITY_TYPE_ID = %d, SRC_ENTITY_ID = %d WHERE SRC_ENTITY_TYPE_ID = %d AND SRC_ENTITY_ID = %d';
	$destinationQuery = 'UPDATE %s SET DST_ENTITY_TYPE_ID = %d, DST_ENTITY_ID = %d WHERE DST_ENTITY_TYPE_ID = %d AND DST_ENTITY_ID = %d';

	foreach ([$sourceQuery, $destinationQuery] as $query)
	{
		$connection->query(sprintf(
			$query,
			$helper->quote(static::getTableName()),
			$helper->convertToDbInteger($newItem->getEntityTypeId()),
			$helper->convertToDbInteger($newItem->getEntityId()),
			$helper->convertToDbInteger($oldItem->getEntityTypeId()),
			$helper->convertToDbInteger($oldItem->getEntityId())
		));
	}

	return (new Result())->setData([
		'affectedItems' => [
			'parents' => array_map(
				fn($parent) => new ItemIdentifier($parent->getSrcEntityTypeId(), $parent->getSrcEntityId()),
				$parentsRows,
			),
			'children' => array_map(
				fn($child) => new ItemIdentifier($child->getDstEntityTypeId(), $child->getDstEntityId()),
				$childrenRows,
			),
		],
	]);
}