Пример #1
1
 public static function deleteByUser($userId)
 {
     $result = new DeleteResult();
     $entity = static::getEntity();
     $userId = intval($userId);
     if ($userId <= 0) {
         $result->addError(new Entity\FieldError($entity->getField('USER_ID'), 'UserID must be greater than zero'));
         return $result;
     }
     $event = new Event($entity, "OnBeforeDeleteByUser", array("USER_ID" => $userId));
     $event->send();
     if ($event->getErrors($result)) {
         return $result;
     }
     $event = new Event($entity, "OnDeleteByUser", array("USER_ID" => $userId));
     $event->send();
     $tableName = $entity->getDBTableName();
     $connection = Application::getConnection();
     $sql = "DELETE FROM " . $tableName . " WHERE USER_ID = " . $userId;
     $connection->queryExecute($sql);
     $event = new Event($entity, "OnAfterDeleteByUser", array("USER_ID" => $userId));
     $event->send();
     return $result;
 }
Пример #2
0
 /**
  * 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;
 }
Пример #3
0
 /**
  * Deletes row in entity table by primary key
  *
  * @param mixed $primary
  * @return DeleteResult
  */
 public static function delete($primary)
 {
     // check primary
     static::normalizePrimary($primary);
     static::validatePrimary($primary);
     $entity = static::getEntity();
     $result = new DeleteResult();
     //event before delete
     $event = new Event($entity, self::EVENT_ON_BEFORE_DELETE, array("id" => $primary));
     $event->send();
     if ($event->getErrors($result)) {
         return $result;
     }
     //event on delete
     $event = new Event($entity, self::EVENT_ON_DELETE, array("id" => $primary));
     $event->send();
     // delete
     $connection = Main\Application::getConnection();
     $helper = $connection->getSqlHelper();
     $tableName = $entity->getDBTableName();
     $id = array();
     foreach ($primary as $k => $v) {
         $id[] = $k . " = '" . $helper->forSql($v) . "'";
     }
     $where = implode(' AND ', $id);
     $sql = "DELETE FROM " . $tableName . " WHERE " . $where;
     $connection->queryExecute($sql);
     //event after delete
     $event = new Event($entity, self::EVENT_ON_AFTER_DELETE, array("id" => $primary));
     $event->send();
     return $result;
 }
Пример #4
0
 /**
  * Deletes row in entity table by primary key
  *
  * @param mixed $primary
  *
  * @return DeleteResult
  *
  * @throws \Exception
  */
 public static function delete($primary)
 {
     global $USER_FIELD_MANAGER;
     // check primary
     static::normalizePrimary($primary);
     static::validatePrimary($primary);
     $entity = static::getEntity();
     $result = new DeleteResult();
     try {
         //event before delete
         $event = new Event($entity, self::EVENT_ON_BEFORE_DELETE, array("id" => $primary));
         $event->send();
         $event->getErrors($result);
         //event before delete (modern with namespace)
         $event = new Event($entity, self::EVENT_ON_BEFORE_DELETE, array("id" => $primary, "primary" => $primary), true);
         $event->send();
         $event->getErrors($result);
         // return if any error
         if (!$result->isSuccess(true)) {
             return $result;
         }
         //event on delete
         $event = new Event($entity, self::EVENT_ON_DELETE, array("id" => $primary));
         $event->send();
         //event on delete (modern with namespace)
         $event = new Event($entity, self::EVENT_ON_DELETE, array("id" => $primary, "primary" => $primary), true);
         $event->send();
         // delete
         $connection = $entity->getConnection();
         $helper = $connection->getSqlHelper();
         $tableName = $entity->getDBTableName();
         $replacedPrimary = static::replaceFieldName($primary);
         $id = array();
         foreach ($replacedPrimary as $k => $v) {
             $id[] = $helper->prepareAssignment($tableName, $k, $v);
         }
         $where = implode(' AND ', $id);
         $sql = "DELETE FROM " . $tableName . " WHERE " . $where;
         $connection->queryExecute($sql);
         // delete uf data
         if (static::getEntity()->getUfId()) {
             $USER_FIELD_MANAGER->delete(static::getEntity()->getUfId(), end($primary));
         }
         //event after delete
         $event = new Event($entity, self::EVENT_ON_AFTER_DELETE, array("id" => $primary));
         $event->send();
         //event after delete (modern with namespace)
         $event = new Event($entity, self::EVENT_ON_AFTER_DELETE, array("id" => $primary, "primary" => $primary), true);
         $event->send();
     } catch (\Exception $e) {
         // check result to avoid warning
         $result->isSuccess();
         throw $e;
     }
     return $result;
 }
Пример #5
0
 /**
  * Deletes row in entity table by primary key
  *
  * @param mixed $primary
  * @return DeleteResult
  */
 public static function delete($primary)
 {
     global $USER_FIELD_MANAGER;
     // check primary
     static::normalizePrimary($primary);
     static::validatePrimary($primary);
     $entity = static::getEntity();
     $result = new DeleteResult();
     //event before delete
     $event = new Event($entity, self::EVENT_ON_BEFORE_DELETE, array("id" => $primary));
     $event->send();
     $event->getErrors($result);
     //event before delete (modern with namespace)
     $event = new Event($entity, self::EVENT_ON_BEFORE_DELETE, array("id" => $primary, "primary" => $primary), true);
     $event->send();
     $event->getErrors($result);
     // return if any error
     if (!$result->isSuccess(true)) {
         return $result;
     }
     //event on delete
     $event = new Event($entity, self::EVENT_ON_DELETE, array("id" => $primary));
     $event->send();
     //event on delete (modern with namespace)
     $event = new Event($entity, self::EVENT_ON_DELETE, array("id" => $primary, "primary" => $primary), true);
     $event->send();
     // delete
     $connection = Main\Application::getConnection();
     $helper = $connection->getSqlHelper();
     $tableName = $entity->getDBTableName();
     $id = array();
     foreach ($primary as $k => $v) {
         $id[] = $k . " = '" . $helper->forSql($v) . "'";
     }
     $where = implode(' AND ', $id);
     $sql = "DELETE FROM " . $tableName . " WHERE " . $where;
     $connection->queryExecute($sql);
     // delete uf data
     if (static::getEntity()->getUfId()) {
         $USER_FIELD_MANAGER->delete(static::getEntity()->getUfId(), end($primary));
     }
     //event after delete
     $event = new Event($entity, self::EVENT_ON_AFTER_DELETE, array("id" => $primary));
     $event->send();
     //event after delete (modern with namespace)
     $event = new Event($entity, self::EVENT_ON_AFTER_DELETE, array("id" => $primary, "primary" => $primary), true);
     $event->send();
     return $result;
 }
Пример #6
0
 /**
  * @param mixed $primary
  *
  * @return Entity\DeleteResult
  */
 public static function delete($primary)
 {
     global $USER_FIELD_MANAGER;
     // check primary
     static::normalizePrimary($primary);
     static::validatePrimary($primary);
     // get old data
     $oldData = static::getByPrimary($primary)->fetch();
     $hlblock = static::getHighloadBlock();
     $entity = static::getEntity();
     $result = new Entity\DeleteResult();
     //event before delete
     $event = new Entity\Event($entity, self::EVENT_ON_BEFORE_DELETE, array("id" => $primary));
     $event->send();
     $event->getErrors($result);
     //event before delete (modern with namespace)
     $event = new Entity\Event($entity, self::EVENT_ON_BEFORE_DELETE, array("id" => $primary, "primary" => $primary, "oldFields" => $oldData), true);
     $event->send();
     $event->getErrors($result);
     // return if any error
     if (!$result->isSuccess(true)) {
         return $result;
     }
     //event on delete
     $event = new Entity\Event($entity, self::EVENT_ON_DELETE, array("id" => $primary));
     $event->send();
     //event on delete (modern with namespace)
     $event = new Entity\Event($entity, self::EVENT_ON_DELETE, array("id" => $primary, "primary" => $primary, "oldFields" => $oldData), true);
     $event->send();
     // remove row
     $connection = Main\Application::getConnection();
     $helper = $connection->getSqlHelper();
     $tableName = $entity->getDBTableName();
     $id = array();
     foreach ($primary as $k => $v) {
         $id[] = $k . " = '" . $helper->forSql($v) . "'";
     }
     $where = implode(' AND ', $id);
     $sql = "DELETE FROM " . $tableName . " WHERE " . $where;
     $connection->queryExecute($sql);
     $fields = $USER_FIELD_MANAGER->getUserFields('HLBLOCK_' . $hlblock['ID']);
     foreach ($oldData as $k => $v) {
         $userfield = $fields[$k];
         // remove multi values
         if ($userfield['MULTIPLE'] == 'Y') {
             $utmTableName = HighloadBlockTable::getMultipleValueTableName($hlblock, $userfield);
             $connection->query(sprintf('DELETE FROM %s WHERE %s = %d', $helper->quote($utmTableName), $helper->quote('ID'), $primary['ID']));
         }
         // remove files
         if ($userfield["USER_TYPE"]["BASE_TYPE"] == "file") {
             if (is_array($oldData[$k])) {
                 foreach ($oldData[$k] as $value) {
                     \CFile::delete($value);
                 }
             } else {
                 \CFile::delete($oldData[$k]);
             }
         }
     }
     //event after delete
     $event = new Entity\Event($entity, self::EVENT_ON_AFTER_DELETE, array("id" => $primary));
     $event->send();
     //event after delete (modern with namespace)
     $event = new Entity\Event($entity, self::EVENT_ON_AFTER_DELETE, array("id" => $primary, "primary" => $primary, "oldFields" => $oldData), true);
     $event->send();
     return $result;
 }