diff --git a/src/Mubin/Mongodb/Query/Builder.php b/src/Mubin/Mongodb/Query/Builder.php index c19ff9f..f2061a6 100644 --- a/src/Mubin/Mongodb/Query/Builder.php +++ b/src/Mubin/Mongodb/Query/Builder.php @@ -256,7 +256,7 @@ public function getFresh($columns = []) ]; // Execute aggregation - $results = iterator_to_array($this->collection->aggregate($pipeline, $options)); + $results = $this->iterate($this->collection->aggregate($pipeline, $options)); // Return results return $results; @@ -317,10 +317,32 @@ public function getFresh($columns = []) $cursor = $this->collection->find($wheres, $options); // Return results as an array with numeric keys - return iterator_to_array($cursor, false); + return $this->iterate($cursor); } } + /** + * Iterate over the Cursor and return plain array. + * @param $cursor + * @return array + */ + public function iterate($cursor) + { + $final = []; + $item_num = 0; + foreach ($cursor as $item) { + foreach ($item as $key => $value) { + if($key == '_id') { + $final[$item_num][$key]['$id'] = $value->__toString(); + } else { + $final[$item_num][$key] = $value; + } + } + $item_num += 1; + } + return $final; + } + /** * Generate the unique cache key for the current query. * @@ -485,20 +507,23 @@ public function insert(array $values) * * @param array $values * @param string $sequence - * @return int + * @return array */ public function insertGetId(array $values, $sequence = null) { $result = $this->collection->insertOne($values); - if (1 == (int) $result->isAcknowledged()) { + if($result){ + return [ '$id' => $result->getInsertedId()->__toString()]; + } + /*if (1 == (int) $result->isAcknowledged()) { if (is_null($sequence)) { $sequence = '_id'; } // Return id return $sequence == '_id' ? $result->getInsertedId() : $values[$sequence]; - } + }*/ } /**