public function clearRevisions() { $documentId = $this->document->getId(); $this->getRevisionsCollection()->deleteDocuments(function (Expression $expression) use($documentId) { /* @var $expression \Sokil\Mongo\Expression */ return $expression->where('__documentId__', $documentId); }); return $this; }
public function removeRelation($relationName, Document $document = null) { if (!$this->isRelationExists($relationName)) { throw new \Exception('Relation ' . $relationName . ' not configured'); } $relation = $this->relations[$relationName]; list($relationType, $relatedCollectionName, $field) = $relation; $relatedCollection = $this->document->getCollection()->getDatabase()->getCollection($relatedCollectionName); if ($document && !$relatedCollection->hasDocument($document)) { throw new \Sokil\Mongo\Exception('Document must belongs to related collection'); } switch ($relationType) { case Document::RELATION_BELONGS: $this->document->unsetField($field)->save(); break; case Document::RELATION_HAS_ONE: $document = $this->getRelated($relationName); if (!$document) { // relation not exists return $this; } $document->unsetField($field)->save(); break; case Document::RELATION_HAS_MANY: if (!$document) { throw new \Sokil\Mongo\Exception('Related document must be defined'); } $document->unsetField($field)->save(); break; case Document::RELATION_MANY_MANY: if (!$document) { throw new \Sokil\Mongo\Exception('Related document must be defined'); } $isRelationListStoredInternally = isset($relation[3]) && $relation[3]; if ($isRelationListStoredInternally) { $this->document->pull($field, $document->getId())->save(); } else { $document->pull($field, $this->document->getId())->save(); } break; default: throw new \Sokil\Mongo\Exception('Unsupported relation type "' . $relationType . '" when resolve relation "' . $relationName . '"'); } return $this; }
/** * Store document to pool * * @param array $document * @return \Sokil\Mongo\Collection */ public function addDocumentToDocumentPool(Document $document) { $documentId = (string) $document->getId(); if (!isset($this->documentPool[$documentId])) { $this->documentPool[$documentId] = $document; } else { // merging because document after // load and before getting in second place may be changed // and this changes must be preserved: // // 1. Document loads and modifies in current session // 2. Document loads modified in another session // 3. Document loads once again in current session. Changes from stage 2 merges as unmodified $this->documentPool[$documentId]->mergeUnmodified($document->toArray()); } return $this; }