From fcd5189e51ba19ce7f5abc0e80b5035889c34101 Mon Sep 17 00:00:00 2001 From: Nuno Chaves Date: Thu, 27 Sep 2018 09:47:47 +0100 Subject: [PATCH] Update QueryBuilder.php --- src/QueryBuilder.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index a2ef452..5ef6eb0 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -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; } @@ -186,8 +192,6 @@ public function save($document) public function delete() { $this->response = null; - $this->multipleResults = false; - $doc = $this->find()->toObject(); if ($doc) { @@ -205,7 +209,6 @@ public function delete() public function deleteAll() { $this->response = null; - $this->multipleResults = true; $col = $this->connection->selectCollection($this->collection); $response = []; @@ -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 @@ -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