diff --git a/src/Sberned/RestORM/Builder.php b/src/Sberned/RestORM/Builder.php index c00837c..f058f10 100644 --- a/src/Sberned/RestORM/Builder.php +++ b/src/Sberned/RestORM/Builder.php @@ -144,7 +144,7 @@ public function limit($per_page, $page) public function getLink(): string { return $this->link . "?" . $this->getFields() . $this->getIncludes() . $this->getOrdering() . $this->pagination . - '&' . $this->getWhereis(); + '&' . $this->getWhereis(); } /** diff --git a/src/Sberned/RestORM/Model.php b/src/Sberned/RestORM/Model.php index 5438925..6c6e9c2 100644 --- a/src/Sberned/RestORM/Model.php +++ b/src/Sberned/RestORM/Model.php @@ -9,6 +9,7 @@ use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; use ArrayAccess; +use Illuminate\Support\Collection; use JsonSerializable; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\Arr; @@ -142,7 +143,7 @@ private function insertThis() $newQuery = new Builder($this->className, $this->getUrl(), $this->getLink(), 'POST', true, $this->attributes); $res = $newQuery->send(); - $className = $this->className; + $className = $this->alias_list; $data = $this->convertToObject($res->$className); if(!empty($data)) { @@ -207,7 +208,7 @@ public function get(array $columns = []) $newQuery = new Builder($this->className, $this->getUrl(), $this->getLink(), 'get', true, []); $newQuery->setValues($this->_values); $res = $newQuery->send(); - $className = $this->url; + $className = $this->alias_list; return collect($res->$className); } @@ -225,7 +226,7 @@ public function all(array $columns = []) $newQuery = new Builder($this->className, $this->getUrl(), $this->getLink(), 'get', true, []); $newQuery->setValues($this->_values); $res = $newQuery->send(); - $className = $this->url; + $className = $this->alias_list; return collect($res->$className); } @@ -279,8 +280,12 @@ public function paginate($limit, $page) * @param array $select * @return mixed */ - public function addSelect(array $select) + public function addSelect($select) { + if (!is_array($select)) { + $select = func_get_args(); + } + $this->_values['Select'] = $select; return $this; @@ -294,20 +299,20 @@ public function addSelect(array $select) * @param string $boolean * @return mixed */ - public function addWhere($column, $operator = '=', $value = null, $fulltext_search = false, $boolean = 'and') + public function addWhere($column, $operator = '=', $value = null) { - if ($fulltext_search) { - $prefix = '@'; - } else { - $prefix = ''; + if (count(func_get_args()) == 2) { + $value = $operator; + $operator = '='; } switch ($operator) { - case '==' || '=': - $this->setWhere($column, $value, $prefix); + case '=': + $this->setWhere($column, $value, ''); break; - case '<>' || '!=': - + case '<>': + case '!=': + $this->setWhere($column, $value, '^'); break; case '<': @@ -320,6 +325,9 @@ public function addWhere($column, $operator = '=', $value = null, $fulltext_sear break; case '>=': + break; + case 'like': + $this->setWhere($column, $value . ':*', '@'); break; } @@ -328,6 +336,10 @@ public function addWhere($column, $operator = '=', $value = null, $fulltext_sear public function addWhereIn($column, $values) { + if ($values instanceof Collection) { + $values = $values->all(); + } + $values = array_filter($values, function($value) { return $value > 0; }); @@ -364,6 +376,10 @@ public function limit($per_page = 15, $page = 1) */ public function addWith($array) { + if (!is_array($array)) { + $array = func_get_args(); + } + if(is_array($array)){ foreach ($array as $arr) { $this->_values['With'][] = $arr; @@ -375,6 +391,11 @@ public function addWith($array) return $this; } + public function addSearch($query) + { + return $this->addWhere('search', $query . ':*'); + } + /** * @return string */ @@ -415,8 +436,12 @@ public static function getCallClass() /** * @param array $query */ - public function setSelect(array $query) + public function setSelect($query) { + if (!is_array($query)) { + $query = func_get_args(); + } + $this->_values['Select'] = $query; } @@ -586,6 +611,10 @@ public function __call($name, $arguments) return call_user_func_array([$this, 'findOne'], $arguments); } + if ($name == 'findMany') { + return call_user_func_array([$this, 'findMany'], $arguments); + } + $method = 'add' . ucfirst($name); if (!method_exists($this, $method)) { throw new \Exception('Method ' . $method . ' does not exists');