Skip to content

Commit

Permalink
Merge branch 'master' into 3.4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyklay authored Sep 16, 2019
2 parents 31713fa + d17dd99 commit 3d773a5
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
71 changes: 71 additions & 0 deletions Library/Phalcon/Mvc/MongoCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,77 @@ public function create()
*/
return $this->_postSave(self::$_disableEvents, $success, false);
}

/**
* {@inheritdoc}
*
* @param array $data
*/
public function createIfNotExist(array $criteria)
{
if (empty($criteria)) {
throw new Exception("Criteria parameter must be array with one or more attributes of the model");
}

/**
* Choose a collection according to the collection name
*/
$collection = $this->prepareCU();

/**
* Assume non-existence to fire beforeCreate events - no update does occur anyway
*/
$exists = false;

/**
* Reset current operation
*/
$this->_operationMade = self::OP_NONE;

/**
* The messages added to the validator are reset here
*/
$this->_errorMessages = [];

/**
* Execute the preSave hook
*/
if ($this->_preSave($this->_dependencyInjector, self::$_disableEvents, $exists) === false) {
return false;
}

$keys = array_flip($criteria);
$data = $this->toArray();

if (array_diff_key($keys, $data)) {
throw new \Exception("Criteria parameter must be array with one or more attributes of the model");
}

$query = array_intersect_key($data, $keys);

$success = false;

$status = $collection->findOneAndUpdate($query,
['$setOnInsert' => $data],
['new' => true, 'upsert' => true]);

if ($status == null) {
$doc = $collection->findOne($query);

if (is_object($doc)) {
$success = true;
$this->_operationMade = self::OP_CREATE;
$this->_id = $doc['_id'];
}
} else {
$this->appendMessage(new Message("Document already exists"));
}

/**
* Call the postSave hooks
*/
return $this->_postSave(self::$_disableEvents, $success, $exists);
}

/**
* {@inheritdoc}
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ See [CONTRIBUTING.md](CONTRIBUTING.md).

See [INDEX.md](INDEX.md).

## Sponsors

Become a sponsor and get your logo on our README on Github with a link to your site. [[Become a sponsor](https://opencollective.com/phalcon#sponsor)]

<a href="https://opencollective.com/phalcon/#contributors">
<img src="https://opencollective.com/phalcon/tiers/sponsors.svg?avatarHeight=48&width=800">
</a>

## Backers

Support us with a monthly donation and help us continue our activities. [[Become a backer](https://opencollective.com/phalcon#backer)]

<a href="https://opencollective.com/phalcon/#contributors">
<img src="https://opencollective.com/phalcon/tiers/backers.svg?avatarHeight=48&width=800&height=200">
</a>

## License

Incubator is open-sourced software licensed under the [New BSD License](https://github.com/phalcon/incubator/blob/master/LICENSE.txt).<br>
Expand Down

0 comments on commit 3d773a5

Please sign in to comment.