databases)) $this->databases = Factory::getInstance()->databases; // Load databaseEngine $this->dbEngine = $this->databases->get($connectionName, 'mongo', $parameters); // Determine the collection $this->collection = $this->getCollection($tableName); } /** * @param string $collectionString * @return Collection * @throws DatabaseException */ protected function getCollection(string $collectionString) { // Determine collection $coll = explode('.', $collectionString); if (count($coll) != 2) throw new DatabaseException("Could not load MongoTableModel. Provided tableName is not a valid collection string."); return $this->dbEngine->{$coll[0]}->{$coll[1]}; } /** * @return string */ public function getName(): string { return 'mongo'; } /** * @param array $data * @param array $options * @param string $table * @return int * @throws DatabaseException */ public function create(array $data, array $options = [], string $table = 'default'): int { // If not data is provided, stop now if (empty($data)) throw new DatabaseException("Could not create data. No data provided."); // Select collection if ($table == 'default') $collection = $this->collection; else $collection = $this->getCollection($table); // And execute the request if ($this->arrIsAssoc($data)) $res = $collection->insertOne($data, $options); else $res = $collection->insertMany($data, $options); // And return the count of inserted documents return $res->getInsertedCount(); } /** * @param array $filter * @param array $options * @param string $table * @return array * @throws DatabaseException */ public function read(array $filter = [], array $options = [], string $table = 'default'): array { // Select collection if ($table == 'default') $collection = $this->collection; else $collection = $this->getCollection($table); // Execute the request $results = $collection->find($filter, $options); // Return the result $return = []; foreach ($results->toArray() as $result) $return[] = iterator_to_array($result); return $return; } /** * @param array $data * @param array $filter * @param array $options * @param string $table * @return int * @throws DatabaseException */ public function update(array $data, array $filter, array $options = [], string $table = 'default'): int { // If not data is provided, stop now if (empty($data)) throw new DatabaseException("Could not create data. No data provided."); // Select collection if ($table == 'default') $collection = $this->collection; else $collection = $this->getCollection($table); // And execute the request $data = ['$set' => $data]; $res = $collection->updateMany($filter, $data, $options); // Return the result return $res->getModifiedCount(); } /** * @param array $filter * @param array $options * @param string $table * @return int * @throws DatabaseException */ public function delete(array $filter, array $options = [], string $table = 'default'): int { // Select collection if ($table == 'default') $collection = $this->collection; else $collection = $this->getCollection($table); // Execute the request $res = $collection->deleteMany($filter, $options); // Return the result return $res->getDeletedCount(); } /** * @return bool */ public function transactionStart(): bool { // TODO: Implement transactionStart() method. } /** * @return bool */ public function transactionEnd(): bool { // TODO: Implement transactionEnd() method. } /** * @return bool */ public function transactionCommit(): bool { // TODO: Implement transactionCommit() method. } /** * @return bool */ public function transactionRollback(): bool { // TODO: Implement transactionRollback() method. } /** * Determines whether an array is associative or numeric * * @param array $arr * @return bool */ private function arrIsAssoc(array $arr): bool { if (array() === $arr) return false; return array_keys($arr) !== range(0, count($arr) - 1); } }