Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app\components;
- use app\system\Exception;
- use yii\base\Component;
- class MongoHelper extends Component{
- protected $mongo;
- public $class;
- public $username;
- public $password;
- public $replica_set;
- public $database;
- public $host;
- /**
- * @var MongoClient
- */
- public $client;
- private $contactsClient ;
- protected $contactsCollection = 'associatedContacts';
- protected $countersCollection = 'counters';
- protected $linksCollection = 'contactsLinks';
- public function init()
- {
- $host = $this->host;
- $host = is_array($host) ? implode(',', $host) : $host;
- $username = $this->username;
- $password = $this->password;
- $replicaSet = $this->replica_set;
- if ($replicaSet) {
- $replicaSet = "?replicaSet=" . $replicaSet;
- }
- $mongoDbAuth = '';
- if (!empty($username) && !empty($password)) {
- $mongoDbAuth = "{$username}:{$password}@";
- }
- try {
- $this->client = new \MongoDB\Client("mongodb://{$mongoDbAuth}{$host}/{$replicaSet}");
- $this->client = $this->client->{$this->database};
- $this->contactsClient = $this->client->{$this->contactsCollection};
- } catch (\Exception $e) {
- return [
- 'result' => 5004,
- ];
- }
- }
- public function insert($data)
- {
- if(!empty($data)) {
- return $this->contactsClient->insertOne($data);
- } else {
- throw new \Exception('Empty data', 400);
- }
- }
- /**
- * Find many documents
- *
- * @param array $query
- * @param array $fields
- * @return MongoCursor
- */
- public function find(array $query = array(), array $fields = array())
- {
- return $this->contactsClient->find($query, $fields);
- }
- public function findOne(array $query = array(), array $fields = array())
- {
- return $this->contactsClient->findOne($query, $fields);
- }
- /**
- * @param $collectionName
- * @return MongoCollection
- */
- public function getCollection($collectionName = null)
- {
- $collectionName = $collectionName ? $collectionName : $this->collection;
- if (!($this->client instanceof \MongoDB\Client)) {
- $this->connect($this->defaultConfig);
- }
- return $this->client
- ->selectDB($this->mongoDb)
- ->selectCollection($collectionName);
- }
- public function getNextSequenceValue($id)
- {
- $countersCollectionExist = false;
- //ищем есть ли коллекция для сиквенсов countersCollection
- foreach ($this->client->listCollections() as $collectionInfo) {
- if($collectionInfo->getName() == $this->countersCollection) {
- $sequenceCollection = $this->client->{$this->countersCollection};
- $countersCollectionExist = true;
- break;
- }
- }
- if(!$countersCollectionExist) {
- $this->client->{$this->countersCollection}->insertOne(
- [
- '_id' => "internal_id",
- 'sequence' => 0
- ]
- );
- $sequenceCollection = $this->client->{$this->countersCollection};
- }
- $sequenceDoc = $sequenceCollection->findOneAndUpdate(
- ['_id' => $id],
- ['$inc' => ['sequence' => 1]],
- ['returnDocument' => \MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER]
- );
- $nextSequence = $sequenceDoc->bsonSerialize()->sequence;
- return $nextSequence;
- }
- public function updateContacts($groupId1, $groupId2)
- {
- if (!empty($groupId1) AND !empty($groupId2)) {
- $updateResult = $this->contactsClient->updateMany(
- /*[
- '$or' => [
- ['group_id' => $groupId1],
- ['group_id' => $groupId2],
- ]
- ],*/
- ['group_id' => $groupId2],
- [
- '$set' => ['group_id' => $groupId1] // В Contacts для всех записей, где group_id=contact2.group_id ставим group_id = contact1.group_id
- ]
- );
- return $updateResult;
- } else {
- throw new \Exception('Empty data', 400);
- }
- }
- public function addLinks($internalId1, $internalId2)
- {
- if (!empty($internalId1) AND !empty($internalId2)) {
- return $this->contactsClient->insertOne(
- [
- 'id1' => $internalId1,
- 'id2' => $internalId2
- ]
- );
- } else {
- throw new \Exception('Empty data', 400);
- }
- }
- public function __call($name, $arguments)
- {
- return call_user_func_array([$this->contactsClient, $name], $arguments);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement