Skip to content

Commit

Permalink
Update QueryBuilder.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuno Chaves authored Sep 27, 2018
1 parent 77565f1 commit fcd5189
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,14 @@ public function find()
$this->response = null;
$this->multipleResults = false;

$this->limit = 1;
$this->findAll();
$fields = !empty($this->fields) ? $this->fields : '*';
$where = $this->where != "" ? "where {$this->where}" : "";
$order = $this->order != "" ? "order by {$this->order}" : "";

$query = "SELECT top 1 {$fields} FROM {$this->collection} {$this->join} {$where} {$order}";

$col = $this->connection->selectCollection($this->collection);
$this->response = $col->query($query);

return $this;
}
Expand Down Expand Up @@ -186,8 +192,6 @@ public function save($document)
public function delete()
{
$this->response = null;
$this->multipleResults = false;

$doc = $this->find()->toObject();

if ($doc) {
Expand All @@ -205,7 +209,6 @@ public function delete()
public function deleteAll()
{
$this->response = null;
$this->multipleResults = true;
$col = $this->connection->selectCollection($this->collection);

$response = [];
Expand Down Expand Up @@ -235,7 +238,8 @@ public function toObject()
$res = json_decode($this->response);
$docs = $res->Documents ?? [];
if (!is_array($docs) || empty($docs)) return [];
return count($docs) > 1 ? $docs : $docs[0];

return $this->multipleResults == true ? $docs : $docs[0];
}
/**
* @return array|mixed
Expand All @@ -244,7 +248,7 @@ public function toArray()
{
$res = json_decode($this->response);
$docs = $res->Documents ?? [];
return $this->multipleResults == true && count($docs) > 0 ? $docs : $docs[0];
return $this->multipleResults == true ? $docs : $docs[0];
}
/**
* @param $fieldName
Expand Down

0 comments on commit fcd5189

Please sign in to comment.