diff --git a/src/ChangesCollector.php b/src/ChangesCollector.php index 2d8e423..e615b57 100644 --- a/src/ChangesCollector.php +++ b/src/ChangesCollector.php @@ -25,8 +25,6 @@ public function changes($object) return Converter::convertJsonPatchToMongoUpdate($diff); } - unset($values['_id']); - return ['$set' => $values]; })->call($object); } diff --git a/src/Hydrator.php b/src/Hydrator.php index 71275bf..b57590c 100644 --- a/src/Hydrator.php +++ b/src/Hydrator.php @@ -3,6 +3,7 @@ use function Makasim\Values\build_object; use function Makasim\Values\set_values; +use MongoDB\BSON\ObjectID; class Hydrator { @@ -41,7 +42,9 @@ public function hydrate(array $values, $model = null) $model = $model ?: $this->create($values); if (isset($values['_id'])) { - $values['_id'] = (string) $values['_id']; + set_object_id($model, new ObjectID((string) $values['_id'])); + + unset($values['_id']); } set_values($model, $values); diff --git a/src/Storage.php b/src/Storage.php index e429bf8..80a1789 100644 --- a/src/Storage.php +++ b/src/Storage.php @@ -67,7 +67,7 @@ public function insert($model, array $options = []) throw new \LogicException('Operation is not acknowledged'); } - set_object_id($model, $result->getInsertedId()); + set_object_id($model, new ObjectID((string) $result->getInsertedId())); $this->changesCollector->register($model); return $result; @@ -111,7 +111,7 @@ public function insertMany(array $models, array $options = []) public function update($model, $filter = null, array $options = []) { if (null === $filter) { - $filter = ['_id' => new ObjectID(get_object_id($model))]; + $filter = ['_id' => get_object_id($model)]; } $update = $this->changesCollector->changes($model); @@ -119,13 +119,10 @@ public function update($model, $filter = null, array $options = []) return; } - if (false == $update) { - return; - } - $result = $this->collection->updateOne($filter, $update, $options); - if (false == $result->isAcknowledged()) { - throw new \LogicException('Operation is not acknowledged'); + + if ($result->getUpsertedCount()) { + set_object_id($model, new ObjectID((string) $result->getUpsertedId())); } $this->changesCollector->register($model); @@ -141,14 +138,7 @@ public function update($model, $filter = null, array $options = []) */ public function delete($model, array $options = []) { - $modelId = new ObjectID(get_object_id($model)); - - $result = $this->collection->deleteOne(['_id' => $modelId], $options); - if (false == $result->isAcknowledged()) { - throw new \LogicException('Operation is not acknowledged'); - } - - return $result; + return $this->collection->deleteOne(['_id' => get_object_id($model)], $options); } /** diff --git a/src/functions.php b/src/functions.php index a419a1b..9a8fd87 100644 --- a/src/functions.php +++ b/src/functions.php @@ -4,23 +4,29 @@ /** * @param object $object + * @param bool $orNull * - * @return string + * @return ObjectID|null */ -function get_object_id($object) +function get_object_id($object, $orNull = false) { - return (function () { - return (string) isset($this->values['_id']) ? $this->values['_id'] : null; + return (function () use ($orNull) { + $id = isset($this->_id) ? $this->_id : null; + + + if (false == $id && false == $orNull) { + throw new \LogicException('The object id is not set.'); + } + + return $id; })->call($object); } /** - * @param object $object - * @param ObjectID|string $objectId + * @param object $object + * @param ObjectID $id */ -function set_object_id($object, $objectId) +function set_object_id($object, ObjectID $id) { - return (function () use ($objectId) { - $this->values['_id'] = (string) $objectId; - })->call($object); -} \ No newline at end of file + (function () use ($id) { $this->_id = $id; })->call($object); +}