/** * Usage: only if you want to update "unnormalized" attributes (symlink object) * @param array $attributes * @param array $filter * @return \Bitrix\Main\Entity\Result * @internal */ public static function updateAttributesByFilter(array $attributes, array $filter) { $entity = static::getEntity(); $result = new Result(); $event = new Event($entity, self::EVENT_ON_BEFORE_UPDATE_ATTR_BY_FILTER, array('fields' => $attributes, 'filter' => $filter)); $event->send(); $event->getErrors($result); $attributes = $event->mergeFields($attributes); //static::checkFields($result, null, $attributes); if (!$result->isSuccess(true)) { return $result; } $event = new Event($entity, self::EVENT_ON_UPDATE_ATTR_BY_FILTER, array('fields' => $attributes, 'filter' => $filter)); $event->send(); $connection = Main\Application::getConnection(); $helper = $connection->getSqlHelper(); $tableName = static::getEntity()->getDBTableName(); $update = $helper->prepareUpdate($tableName, $attributes); $filterAttributes = array(); foreach ($filter as $k => $v) { $filterAttributes[] = $helper->prepareAssignment($tableName, $k, $v); } $where = implode(' AND ', $filterAttributes); $sql = "UPDATE " . $tableName . " SET " . $update[0] . " WHERE " . $where; $connection->queryExecute($sql, $update[1]); $event = new Event($entity, self::EVENT_ON_AFTER_UPDATE_ATTR_BY_FILTER, array('fields' => $attributes, 'filter' => $filter)); $event->send(); return $result; }
/** * Updates row in entity table by primary key * * @param mixed $primary * @param array $data * * @return UpdateResult * * @throws \Exception */ public static function update($primary, array $data) { global $USER_FIELD_MANAGER, $APPLICATION; // check primary static::normalizePrimary($primary, $data); static::validatePrimary($primary); $entity = static::getEntity(); $result = new UpdateResult(); try { //event before update $event = new Event($entity, self::EVENT_ON_BEFORE_UPDATE, array("id" => $primary, "fields" => $data)); $event->send(); $event->getErrors($result); $data = $event->mergeFields($data); //event before update (modern with namespace) $event = new Event($entity, self::EVENT_ON_BEFORE_UPDATE, array("id" => $primary, "primary" => $primary, "fields" => $data), true); $event->send(); $event->getErrors($result); $data = $event->mergeFields($data); // uf values $ufdata = array(); // separate userfields if (static::getEntity()->getUfId()) { // collect uf data $userfields = $USER_FIELD_MANAGER->GetUserFields(static::getEntity()->getUfId()); foreach ($userfields as $userfield) { if (array_key_exists($userfield['FIELD_NAME'], $data)) { // copy value $ufdata[$userfield['FIELD_NAME']] = $data[$userfield['FIELD_NAME']]; // remove original unset($data[$userfield['FIELD_NAME']]); } } } // check data static::checkFields($result, $primary, $data); // check uf data if (!empty($ufdata)) { if (!$USER_FIELD_MANAGER->CheckFields(static::getEntity()->getUfId(), end($primary), $ufdata)) { if (is_object($APPLICATION) && $APPLICATION->getException()) { $e = $APPLICATION->getException(); $result->addError(new EntityError($e->getString())); $APPLICATION->resetException(); } else { $result->addError(new EntityError("Unknown error while checking userfields")); } } } // check if there is still some data if (!count($data + $ufdata)) { $result->addError(new EntityError("There is no data to update.")); } // return if any error if (!$result->isSuccess(true)) { return $result; } //event on update $event = new Event($entity, self::EVENT_ON_UPDATE, array("id" => $primary, "fields" => $data + $ufdata)); $event->send(); //event on update (modern with namespace) $event = new Event($entity, self::EVENT_ON_UPDATE, array("id" => $primary, "primary" => $primary, "fields" => $data + $ufdata), true); $event->send(); // use save modifiers foreach ($data as $fieldName => $value) { $field = static::getEntity()->getField($fieldName); $data[$fieldName] = $field->modifyValueBeforeSave($value, $data); } // save data $connection = $entity->getConnection(); $helper = $connection->getSqlHelper(); $tableName = $entity->getDBTableName(); $dataReplacedColumn = static::replaceFieldName($data); $update = $helper->prepareUpdate($tableName, $dataReplacedColumn); $replacedPrimary = static::replaceFieldName($primary); $id = array(); foreach ($replacedPrimary as $k => $v) { $id[] = $helper->prepareAssignment($tableName, $k, $v); } $where = implode(' AND ', $id); $sql = "UPDATE " . $tableName . " SET " . $update[0] . " WHERE " . $where; $connection->queryExecute($sql, $update[1]); $result->setAffectedRowsCount($connection); $result->setData($data); $result->setPrimary($primary); // save uf data if (!empty($ufdata)) { $USER_FIELD_MANAGER->update(static::getEntity()->getUfId(), end($primary), $ufdata); } //event after update $event = new Event($entity, self::EVENT_ON_AFTER_UPDATE, array("id" => $primary, "fields" => $data)); $event->send(); //event after update (modern with namespace) $event = new Event($entity, self::EVENT_ON_AFTER_UPDATE, array("id" => $primary, "primary" => $primary, "fields" => $data), true); $event->send(); } catch (\Exception $e) { // check result to avoid warning $result->isSuccess(); throw $e; } return $result; }
/** * Updates row in entity table by primary key * * @param mixed $primary * @param array $data * @return UpdateResult */ public static function update($primary, array $data) { // check primary static::normalizePrimary($primary, $data); static::validatePrimary($primary); $entity = static::getEntity(); $result = new UpdateResult(); //event before update $event = new Event($entity, self::EVENT_ON_BEFORE_UPDATE, array("id" => $primary, "fields" => $data)); $event->send(); $event->getErrors($result); $data = $event->mergeFields($data); // check data static::checkFields($result, $primary, $data); if (!$result->isSuccess(true)) { return $result; } //event on update $event = new Event($entity, self::EVENT_ON_UPDATE, array("id" => $primary, "fields" => $data)); $event->send(); // save data $connection = Main\Application::getConnection(); $helper = $connection->getSqlHelper(); $tableName = $entity->getDBTableName(); $update = $helper->prepareUpdate($tableName, $data); $id = array(); foreach ($primary as $k => $v) { $id[] = $helper->prepareAssignment($tableName, $k, $v); } $where = implode(' AND ', $id); $sql = "UPDATE " . $tableName . " SET " . $update[0] . " WHERE " . $where; $connection->queryExecute($sql, $update[1]); $result->setAffectedRowsCount($connection); $result->setData($data); //TODO: save Userfields //event after update $event = new Event($entity, self::EVENT_ON_AFTER_UPDATE, array("id" => $primary, "fields" => $data)); $event->send(); return $result; }
/** * @param mixed $primary * @param array $data * * @return Entity\UpdateResult */ public static function update($primary, array $data) { global $USER_FIELD_MANAGER, $APPLICATION; $result = new Entity\UpdateResult(); static::normalizePrimary($primary, $data); static::validatePrimary($primary); $oldData = static::getByPrimary($primary)->fetch(); $hlblock = static::getHighloadBlock(); $entity = static::getEntity(); //event before update $event = new Entity\Event($entity, self::EVENT_ON_BEFORE_UPDATE, array("id" => $primary, "fields" => $data)); $event->send(); $event->getErrors($result); $data = $event->mergeFields($data); //event before update (modern with namespace) $event = new Entity\Event($entity, self::EVENT_ON_BEFORE_UPDATE, array("id" => $primary, "primary" => $primary, "fields" => $data, "oldFields" => $oldData), true); $event->send(); $event->getErrors($result); $data = $event->mergeFields($data); // check data by uf manager CheckFieldsWithOldData if (!$USER_FIELD_MANAGER->checkFieldsWithOldData('HLBLOCK_' . $hlblock['ID'], $oldData, $data)) { if (is_object($APPLICATION) && $APPLICATION->getException()) { $e = $APPLICATION->getException(); $result->addError(new Entity\EntityError($e->getString())); $APPLICATION->resetException(); } else { $result->addError(new Entity\EntityError("Unknown error while checking userfields")); } } // return if any error if (!$result->isSuccess(true)) { return $result; } //event on update $event = new Entity\Event($entity, self::EVENT_ON_UPDATE, array("id" => $primary, "fields" => $data)); $event->send(); //event on update (modern with namespace) $event = new Entity\Event($entity, self::EVENT_ON_UPDATE, array("id" => $primary, "primary" => $primary, "fields" => $data, "oldFields" => $oldData), true); $event->send(); // format data before save $fields = $USER_FIELD_MANAGER->getUserFieldsWithReadyData('HLBLOCK_' . $hlblock['ID'], $oldData, LANGUAGE_ID, false, 'ID'); list($data, $multiValues) = static::convertValuesBeforeSave($data, $fields); // use save modifiers foreach ($data as $fieldName => $value) { $field = static::getEntity()->getField($fieldName); $data[$fieldName] = $field->modifyValueBeforeSave($value, $data); } // save data $connection = Main\Application::getConnection(); $helper = $connection->getSqlHelper(); $tableName = $entity->getDBTableName(); $update = $helper->prepareUpdate($tableName, $data); $id = array(); foreach ($primary as $k => $v) { $id[] = $helper->prepareAssignment($tableName, $k, $v); } $where = implode(' AND ', $id); $sql = "UPDATE " . $tableName . " SET " . $update[0] . " WHERE " . $where; $connection->queryExecute($sql, $update[1]); $result->setAffectedRowsCount($connection); $result->setData($data); $result->setPrimary($primary); // save multi values if (!empty($multiValues)) { foreach ($multiValues as $userfieldName => $values) { $utmTableName = HighloadBlockTable::getMultipleValueTableName($hlblock, $fields[$userfieldName]); // first, delete old values $connection->query(sprintf('DELETE FROM %s WHERE %s = %d', $helper->quote($utmTableName), $helper->quote('ID'), $primary['ID'])); // insert new values foreach ($values as $value) { $connection->add($utmTableName, array('ID' => $primary['ID'], 'VALUE' => $value)); } } } //event after update $event = new Entity\Event($entity, self::EVENT_ON_AFTER_UPDATE, array("id" => $primary, "fields" => $data)); $event->send(); //event after update (modern with namespace) $event = new Entity\Event($entity, self::EVENT_ON_AFTER_UPDATE, array("id" => $primary, "primary" => $primary, "fields" => $data, "oldFields" => $oldData), true); $event->send(); return $result; }