Skip to content

Commit

Permalink
Builder updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
mubin-khalid committed Nov 22, 2016
1 parent a50f5a2 commit a906369
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/Mubin/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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];
}
}*/
}

/**
Expand Down

0 comments on commit a906369

Please sign in to comment.