Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for manual document upserting #106

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## master (2015-08-12)
* Persistance now fire's document's events (beforeSave/beforeInsert/beforeUpdate)

## 1.13.5 (2015-08-07)
* Fix getting HAS_ONE and BELONGS relation when related object not found;

Expand Down
11 changes: 9 additions & 2 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,10 @@ public function append($selector, $value)
return $this;
}

public function setUpsert() {
$this->options['upsert'] = 1;
}

/**
* Push argument as single element to field value
*
Expand Down Expand Up @@ -1198,7 +1202,7 @@ public function save($validate = true)
if($this->triggerEvent('beforeSave')->isCancelled()) {
return $this;
}
if ($this->isStored()) {
if ($this->isStored() || $this->getOption('upsert')) {
if($this->triggerEvent('beforeUpdate')->isCancelled()) {
return $this;
}
Expand All @@ -1209,13 +1213,16 @@ public function save($validate = true)
$query['__version__'] = $this->get('__version__');
$this->getOperator()->increment('__version__');
}

$data = ! $this->isStored() ? $this->toArray() : $this->getOperator()->toArray();

// update
$status = $this->collection
->getMongoCollection()
->update(
$query,
$this->getOperator()->toArray()
$data,
['upsert'=>$this->getOption('upsert') ]
);

// check update status
Expand Down
Loading