33 foreach ($data as $k => $v)
35 if (!(static::getEntity()->hasField($k) && static::getEntity()->getField($k) instanceof
Entity\
ScalarField))
38 'Field `%s` not found in entity when trying to query %s row.',
39 $k, static::getEntity()->getName()
53 public static function add(array $data)
55 global $USER_FIELD_MANAGER, $APPLICATION;
58 $object = static::convertArrayToObject($data,
true);
60 $result =
new Entity\AddResult;
61 $hlblock = static::getHighloadBlock();
62 $entity = static::getEntity();
66 static::callOnBeforeAddEvent($object, $data, $result);
69 $data = $object->collectValues(Values::CURRENT, FieldTypeMask::SCALAR);
72 if (!$USER_FIELD_MANAGER->checkFields(
'HLBLOCK_'.$hlblock[
'ID'],
null, $data))
74 if(is_object($APPLICATION) && $APPLICATION->getException())
76 $e = $APPLICATION->getException();
78 $APPLICATION->resetException();
82 $result->addError(
new Entity\
EntityError(
"Unknown error while checking userfields"));
87 if (!$result->isSuccess(
true))
93 self::callOnAddEvent($object, $data, []);
96 $connection = Main\Application::getConnection();
98 $tableName = $entity->getDBTableName();
99 $identity = $entity->getAutoIncrement();
101 $id = $connection->add($tableName, [$identity =>
new Main\DB\
SqlExpression(
'DEFAULT')], $identity);
104 $fields = $USER_FIELD_MANAGER->getUserFields(
'HLBLOCK_'.$hlblock[
'ID']);
106 foreach ($fields as $k => $field)
108 $fields[$k][
'VALUE_ID'] = $id;
111 list($data, $multiValues) = static::convertValuesBeforeSave($data, $fields);
114 foreach ($data as $k => $v)
116 $object->set($k, $v);
120 foreach ($data as $fieldName => $value)
122 $field = static::getEntity()->getField($fieldName);
123 $data[$fieldName] = $field->modifyValueBeforeSave($value, $data);
129 $helper = $connection->getSqlHelper();
130 $update = $helper->prepareUpdate($tableName, $data);
132 $sql =
"UPDATE ".$helper->quote($tableName).
" SET ".$update[0].
" WHERE ".$helper->quote($identity).
" = ".((int) $id);
133 $connection->queryExecute($sql, $update[1]);
137 if (!empty($multiValues))
139 foreach ($multiValues as $userfieldName => $values)
143 foreach ($values as $value)
145 $connection->add($utmTableName, array(
'ID' => $id,
'VALUE' => $value));
155 $primary = array($entity->getAutoIncrement() => $id);
156 static::normalizePrimary($primary);
160 static::normalizePrimary($primary, $data);
164 $result->setPrimary($primary);
165 $result->setData($data);
166 $result->setObject($object);
168 foreach ($primary as $primaryName => $primaryValue)
170 $object->sysSetActual($primaryName, $primaryValue);
173 $entity->cleanCache();
175 static::callOnAfterAddEvent($object, $data, $id);
177 catch (\Exception $e)
180 $result->isSuccess();
197 public static function update($primary, array $data)
199 global $USER_FIELD_MANAGER, $APPLICATION;
202 static::normalizePrimary(
203 $primary, isset($data[
"fields"]) && is_array($data[
"fields"]) ? $data[
"fields"] : $data
205 static::validatePrimary($primary);
208 $object = static::convertArrayToObject($data,
false, $primary);
211 $object->fill(FieldTypeMask::SCALAR);
212 $oldData = $object->collectValues(Values::ACTUAL, FieldTypeMask::SCALAR);
214 $hlblock = static::getHighloadBlock();
215 $entity = static::getEntity();
220 static::callOnBeforeUpdateEvent($object, $data, $result);
223 $data = $object->collectValues(Values::CURRENT, FieldTypeMask::SCALAR);
226 if (!$USER_FIELD_MANAGER->checkFieldsWithOldData(
'HLBLOCK_'.$hlblock[
'ID'], $oldData, $data))
228 if(is_object($APPLICATION) && $APPLICATION->getException())
230 $e = $APPLICATION->getException();
232 $APPLICATION->resetException();
236 $result->addError(
new Entity\EntityError(
"Unknown error while checking userfields"));
241 if (!$result->isSuccess(
true))
246 static::callOnUpdateEvent($object, $data, []);
249 $fields = $USER_FIELD_MANAGER->getUserFieldsWithReadyData(
'HLBLOCK_'.$hlblock[
'ID'], $oldData, LANGUAGE_ID,
false,
'ID');
250 list($data, $multiValues) = static::convertValuesBeforeSave($data, $fields);
253 foreach ($data as $k => $v)
255 $object->set($k, $v);
259 foreach ($data as $fieldName => $value)
261 $field = static::getEntity()->getField($fieldName);
262 $data[$fieldName] = $field->modifyValueBeforeSave($value, $data);
265 $connection = Main\Application::getConnection();
270 $helper = $connection->getSqlHelper();
271 $tableName = $entity->getDBTableName();
273 $update = $helper->prepareUpdate($tableName, $data);
276 foreach ($primary as $k => $v)
278 $id[] = $helper->prepareAssignment($tableName, $k, $v);
280 $where = implode(
' AND ', $id);
282 $sql =
"UPDATE ".$helper->quote($tableName).
" SET ".$update[0].
" WHERE ".$where;
283 $connection->queryExecute($sql, $update[1]);
286 $result->setAffectedRowsCount($connection);
287 $result->setData($data);
288 $result->setPrimary($primary);
289 $result->setObject($object);
292 if (!empty($multiValues))
294 foreach ($multiValues as $userfieldName => $values)
299 $connection->query(sprintf(
300 'DELETE FROM %s WHERE %s = %d',
301 $helper->quote($utmTableName), $helper->quote(
'ID'), $primary[
'ID']
305 foreach ($values as $value)
307 $connection->add($utmTableName, array(
'ID' => $primary[
'ID'],
'VALUE' => $value));
312 $entity->cleanCache();
315 static::callOnAfterUpdateEvent($object, $data);
317 catch (\Exception $e)
320 $result->isSuccess();
336 public static function delete($primary)
338 global $USER_FIELD_MANAGER;
341 static::normalizePrimary($primary);
342 static::validatePrimary($primary);
344 $entity = static::getEntity();
345 $result =
new Entity\DeleteResult();
346 $hlblock = static::getHighloadBlock();
349 $oldData = static::getByPrimary($primary)->fetch();
354 static::callOnBeforeDeleteEvent($primary, $entity, $result, $oldData);
357 if (!$result->isSuccess(
true))
363 static::callOnDeleteEvent($primary, $entity, $oldData);
366 $connection = Main\Application::getConnection();
367 $helper = $connection->getSqlHelper();
369 $tableName = $entity->getDBTableName();
372 foreach ($primary as $k => $v)
374 $id[] = $k.
" = '".$helper->forSql($v).
"'";
376 $where = implode(
' AND ', $id);
378 $sql =
"DELETE FROM ".$helper->quote($tableName).
" WHERE ".$where;
379 $connection->queryExecute($sql);
381 $fields = $USER_FIELD_MANAGER->getUserFields(
'HLBLOCK_'.$hlblock[
'ID']);
383 foreach ($oldData as $k => $v)
385 $userfield = $fields[$k];
388 if ($userfield[
'MULTIPLE'] ==
'Y')
392 $connection->query(sprintf(
393 'DELETE FROM %s WHERE %s = %d',
394 $helper->quote($utmTableName), $helper->quote(
'ID'), $primary[
'ID']
399 if ($userfield[
"USER_TYPE"][
"BASE_TYPE"]==
"file")
401 if(is_array($oldData[$k]))
403 foreach($oldData[$k] as $value)
405 \CFile::delete($value);
410 \CFile::delete($oldData[$k]);
415 $entity->cleanCache();
418 static::callOnAfterDeleteEvent($primary, $entity, $oldData);
420 catch (\Exception $e)
423 $result->isSuccess();
434 $event =
new Entity\Event($object->entity, self::EVENT_ON_BEFORE_UPDATE, [
435 "id" => $object->primary,
441 $event->getErrors($result);
442 $event->mergeObjectFields($object);
444 $oldData = $object->collectValues(Values::ACTUAL, FieldTypeMask::SCALAR);
447 $event =
new Entity\Event($object->entity, self::EVENT_ON_BEFORE_UPDATE, [
448 "id" => $object->primary,
449 "primary" => $object->primary,
452 "oldFields" => $oldData
456 $event->getErrors($result);
457 $event->mergeObjectFields($object);
463 $event =
new Event($object->entity, self::EVENT_ON_UPDATE, [
464 "id" => $object->primary,
466 'object' => clone $object
470 $oldData = $object->collectValues(Values::ACTUAL, FieldTypeMask::SCALAR);
473 $event =
new Event($object->entity, self::EVENT_ON_UPDATE, [
474 "id" => $object->primary,
475 "primary" => $object->primary,
477 'object' => clone $object,
478 "oldFields" => $oldData
486 $event =
new Entity\Event($object->entity, self::EVENT_ON_AFTER_UPDATE, [
487 "id"=> $object->primary,
489 'object' => clone $object
493 $oldData = $object->collectValues(Values::ACTUAL, FieldTypeMask::SCALAR);
496 $event =
new Entity\Event($object->entity, self::EVENT_ON_AFTER_UPDATE, [
497 "id" => $object->primary,
498 "primary" => $object->primary,
500 'object' => clone $object,
501 "oldFields" => $oldData
509 $event =
new Entity\Event($entity, self::EVENT_ON_BEFORE_DELETE, array(
"id"=>$primary));
511 $event->getErrors($result);
514 $event =
new Entity\Event($entity, self::EVENT_ON_BEFORE_DELETE, array(
515 "id"=>$primary,
"primary"=>$primary,
"oldFields" => $oldData
518 $event->getErrors($result);
524 $event =
new Entity\Event($entity, self::EVENT_ON_DELETE, array(
"id"=>$primary));
528 $event =
new Entity\Event($entity, self::EVENT_ON_DELETE, array(
529 "id"=>$primary,
"primary"=>$primary,
"oldFields" => $oldData
537 $event =
new Entity\Event($entity, self::EVENT_ON_AFTER_DELETE, array(
"id"=>$primary));
541 $event =
new Entity\Event($entity, self::EVENT_ON_AFTER_DELETE, array(
542 "id"=>$primary,
"primary" => $primary,
"oldFields" => $oldData
549 $multiValues = array();
551 foreach ($data as $k => $v)
558 $userfield = $userfields[$k];
560 if ($userfield[
'MULTIPLE'] ==
'N')
562 $inputValue = array($v);
571 foreach ($inputValue as $singleValue)
573 $tmpValue[] = static::convertSingleValueBeforeSave($singleValue, $userfield);
577 if ($userfield[
'MULTIPLE'] ==
'N')
579 $data[$k] = $tmpValue[0];
584 $tmpValue = array_filter($tmpValue, array(
'static',
'isNotNull'));
586 $data[$k] = $tmpValue;
587 $multiValues[$k] = $tmpValue;
591 return array($data, $multiValues);
602 if (!isset($userfield[
'USER_TYPE']) || !is_array($userfield[
'USER_TYPE']))
604 $userfield[
'USER_TYPE'] = array();
608 isset($userfield[
'USER_TYPE'][
'CLASS_NAME']) &&
609 is_callable(array($userfield[
'USER_TYPE'][
'CLASS_NAME'],
'onbeforesave'))
612 $value = call_user_func_array(
613 array($userfield[
'USER_TYPE'][
'CLASS_NAME'],
'onbeforesave'), array($userfield, $value)
617 if (static::isNotNull($value))
622 isset($userfield[
'USER_TYPE'][
'BASE_TYPE']) &&
624 $userfield[
'USER_TYPE'][
'BASE_TYPE'] ==
'int' ||
625 $userfield[
'USER_TYPE'][
'BASE_TYPE'] ==
'double'
639 return !($value ===
null || $value ===
false || $value ===
'');