/** * Updates the collection's relations. No separate save of the collection is required. * * @param object $newRelations Object with predicates as keys and URIs as values * @param int $userID User making the change */ public function setRelations($newRelations, $userID) { if (!$this->_id) { throw new Exception('collectionID not set'); } // An empty array is allowed by updateFromJSON() if (is_array($newRelations) && empty($newRelations)) { $newRelations = new stdClass(); } Zotero_DB::beginTransaction(); // Get arrays from objects $oldRelations = get_object_vars($this->getRelations()); $newRelations = get_object_vars($newRelations); $toAdd = array_diff($newRelations, $oldRelations); $toRemove = array_diff($oldRelations, $newRelations); if (!$toAdd && !$toRemove) { Zotero_DB::commit(); return false; } $subject = Zotero_URI::getCollectionURI($this); foreach ($toAdd as $predicate => $object) { Zotero_Relations::add($this->libraryID, $subject, $predicate, $object); } foreach ($toRemove as $predicate => $object) { $relations = Zotero_Relations::getByURIs($this->libraryID, $subject, $predicate, $object); foreach ($relations as $relation) { Zotero_Relations::delete($this->libraryID, $relation->key); } } $this->updateVersion($userID); Zotero_DB::commit(); return true; }
/** * Delete any relations that have the URI as either the subject * or the object */ public static function eraseByURI($libraryID, $uri, $ignorePredicates = false) { Zotero_DB::beginTransaction(); $sql = "SELECT relationID FROM relations WHERE libraryID=? AND subject=?"; $params = [$libraryID, $uri]; if ($ignorePredicates) { foreach ($ignorePredicates as $ignorePredicate) { $sql .= " AND predicate != ?"; $params[] = $ignorePredicate; } } $sql .= " UNION SELECT relationID FROM relations WHERE libraryID=? AND object=?"; $params = array_merge($params, [$libraryID, $uri]); if ($ignorePredicates) { foreach ($ignorePredicates as $ignorePredicate) { $sql .= " AND predicate != ?"; $params[] = $ignorePredicate; } } $ids = Zotero_DB::columnQuery($sql, $params, Zotero_Shards::getByLibraryID($libraryID)); if ($ids) { foreach ($ids as $id) { $relation = self::get($libraryID, $id); Zotero_Relations::delete($libraryID, $relation->key); } } Zotero_DB::commit(); }