doSave example

/** * {@inheritdoc} */
  public function save(EntityInterface $entity) {
    // Track if this entity is new.     $is_new = $entity->isNew();

    // Execute presave logic and invoke the related hooks.     $id = $this->doPreSave($entity);

    // Perform the save and reset the static cache for the changed entity.     $return = $this->doSave($id$entity);

    // Execute post save logic and invoke the related hooks.     $this->doPostSave($entity, !$is_new);

    return $return;
  }

  /** * Performs presave entity processing. * * @param \Drupal\Core\Entity\EntityInterface $entity * The saved entity. * * @return int|string|null * The processed entity identifier, or null for new entities. * * @throws \Drupal\Core\Entity\EntityStorageException * If the entity identifier is invalid. */
// Tags are not cleaned up in this case, however that is done on invalidateTags().             try {
                $this->doDelete($expiredIds);
            } catch (\Exception $e) {
                $ok = false;
                CacheItem::log($this->logger, 'Failed to delete expired items: '.$e->getMessage()['exception' => $e, 'cache-adapter' => get_debug_type($this)]);
            }
        }
        foreach ($byLifetime as $lifetime => $values) {
            try {
                $values = $this->extractTagData($values$addTagData$removeTagData);
                $e = $this->doSave($values$lifetime$addTagData$removeTagData);
            } catch (\Exception $e) {
            }
            if (true === $e || [] === $e) {
                continue;
            }
            if (\is_array($e) || 1 === \count($values)) {
                foreach (\is_array($e) ? $e : array_keys($values) as $id) {
                    $ok = false;
                    $v = $values[$id];
                    $type = get_debug_type($v);
                    $message = sprintf('Failed to save key "{key}" of type %s%s', $type$e instanceof \Exception ? ': '.$e->getMessage() : '.');
                    
if ($expiredIds) {
            try {
                $this->doDelete($expiredIds);
            } catch (\Exception $e) {
                $ok = false;
                CacheItem::log($this->logger, 'Failed to delete expired items: '.$e->getMessage()['exception' => $e, 'cache-adapter' => get_debug_type($this)]);
            }
        }
        foreach ($byLifetime as $lifetime => $values) {
            try {
                $e = $this->doSave($values$lifetime);
            } catch (\Exception $e) {
            }
            if (true === $e || [] === $e) {
                continue;
            }
            if (\is_array($e) || 1 === \count($values)) {
                foreach (\is_array($e) ? $e : array_keys($values) as $id) {
                    $ok = false;
                    $v = $values[$id];
                    $type = get_debug_type($v);
                    $message = sprintf('Failed to save key "{key}" of type %s%s', $type$e instanceof \Exception ? ': '.$e->getMessage() : '.');
                    

        $this->deferred = [];
        if ($cleared = $this->versioningIsEnabled) {
            if ('' === $namespaceVersionToClear = $this->namespaceVersion) {
                foreach ($this->doFetch([static::NS_SEPARATOR.$this->namespace]) as $v) {
                    $namespaceVersionToClear = $v;
                }
            }
            $namespaceToClear = $this->namespace.$namespaceVersionToClear;
            $namespaceVersion = self::formatNamespaceVersion(mt_rand());
            try {
                $e = $this->doSave([static::NS_SEPARATOR.$this->namespace => $namespaceVersion], 0);
            } catch (\Exception $e) {
            }
            if (true !== $e && [] !== $e) {
                $cleared = false;
                $message = 'Failed to save the new namespace'.($e instanceof \Exception ? ': '.$e->getMessage() : '.');
                CacheItem::log($this->logger, $message['exception' => $e instanceof \Exception ? $e : null, 'cache-adapter' => get_debug_type($this)]);
            } else {
                $this->namespaceVersion = $namespaceVersion;
                $this->ids = [];
            }
        } else {
            
return $query->execute();
      }
      // Some other failure that we can not recover from.       throw new PluginException($e->getMessage(), 0, $e);
    }
  }

  /** * {@inheritdoc} */
  public function save(array $link) {
    $affected_menus = $this->doSave($link);
    $this->resetDefinitions();
    $cache_tags = Cache::buildTags('config:system.menu', $affected_menus, '.');
    $this->cacheTagsInvalidator->invalidateTags($cache_tags);
    return $affected_menus;
  }

  /** * Saves a link without clearing caches. * * @param array $link * A definition, according to $definitionFields, for a * \Drupal\Core\Menu\MenuLinkInterface plugin. * * @return array * The menu names affected by the save operation. This will be one menu * name if the link is saved to the sane menu, or two if it is saved to a * new menu. * * @throws \Exception * Thrown if the storage back-end does not exist and could not be created. * @throws \Drupal\Component\Plugin\Exception\PluginException * Thrown if the definition is invalid, for example, if the specified parent * would cause the links children to be moved to greater than the maximum * depth. */
if ($this->namespaceLen) {
            foreach ($keys as $i => $key) {
                $keys[$i] = $this->getId($key);
            }
        }

        return $this->pool->deleteItems($keys);
    }

    public function save(CacheItemInterface $item): bool
    {
        return $this->doSave($item, __FUNCTION__);
    }

    public function saveDeferred(CacheItemInterface $item): bool
    {
        return $this->doSave($item, __FUNCTION__);
    }

    public function commit(): bool
    {
        return $this->pool->commit();
    }

    
Home | Imprint | This part of the site doesn't use cookies.