- Модуль: highloadblock
- Путь к файлу: ~/bitrix/modules/highloadblock/lib/datamanager.php
- Класс: BitrixHighloadblockDataManager
- Вызов: DataManager::add
static function add(array $data)
{
global $USER_FIELD_MANAGER, $APPLICATION;
// prepare entity object for compatibility with new code
$object = static::convertArrayToObject($data, true);
$result = new EntityAddResult;
$hlblock = static::getHighloadBlock();
$entity = static::getEntity();
try
{
static::callOnBeforeAddEvent($object, $data, $result);
// actualize old-style fields array from object
$data = $object->collectValues(Values::CURRENT, FieldTypeMask::SCALAR);
// check data by uf manager
if (!$USER_FIELD_MANAGER->checkFields('HLBLOCK_'.$hlblock['ID'], null, $data))
{
if(is_object($APPLICATION) && $APPLICATION->getException())
{
$e = $APPLICATION->getException();
$result->addError(new EntityEntityError($e->getString()));
$APPLICATION->resetException();
}
else
{
$result->addError(new EntityEntityError("Unknown error while checking userfields"));
}
}
// return if any error
if (!$result->isSuccess(true))
{
return $result;
}
//event on adding
self::callOnAddEvent($object, $data, []);
// insert base row
$connection = MainApplication::getConnection();
$tableName = $entity->getDBTableName();
$identity = $entity->getAutoIncrement();
$id = $connection->add($tableName, [$identity => new MainDBSqlExpression('DEFAULT')], $identity);
// format data before save
$fields = $USER_FIELD_MANAGER->getUserFields('HLBLOCK_'.$hlblock['ID']);
foreach ($fields as $k => $field)
{
$fields[$k]['VALUE_ID'] = $id;
}
list($data, $multiValues) = static::convertValuesBeforeSave($data, $fields);
// data could be changed by uf manager
foreach ($data as $k => $v)
{
$object->set($k, $v);
}
// use save modifiers
foreach ($data as $fieldName => $value)
{
$field = static::getEntity()->getField($fieldName);
$data[$fieldName] = $field->modifyValueBeforeSave($value, $data);
}
if (!empty($data))
{
// save data
$helper = $connection->getSqlHelper();
$update = $helper->prepareUpdate($tableName, $data);
$sql = "UPDATE ".$helper->quote($tableName)." SET ".$update[0]." WHERE ".$helper->quote($identity)." = ".((int) $id);
$connection->queryExecute($sql, $update[1]);
}
// save multi values
if (!empty($multiValues))
{
foreach ($multiValues as $userfieldName => $values)
{
$utmTableName = HighloadBlockTable::getMultipleValueTableName($hlblock, $fields[$userfieldName]);
foreach ($values as $value)
{
$connection->add($utmTableName, array('ID' => $id, 'VALUE' => $value));
}
}
}
// build standard primary
$primary = null;
if (!empty($id))
{
$primary = array($entity->getAutoIncrement() => $id);
static::normalizePrimary($primary);
}
else
{
static::normalizePrimary($primary, $data);
}
// fill result
$result->setPrimary($primary);
$result->setData($data);
$result->setObject($object);
foreach ($primary as $primaryName => $primaryValue)
{
$object->sysSetActual($primaryName, $primaryValue);
}
$entity->cleanCache();
static::callOnAfterAddEvent($object, $data, $id);
}
catch (Exception $e)
{
// check result to avoid warning
$result->isSuccess();
throw $e;
}
return $result;
}