diff --git a/src/Query/Sql/Select.php b/src/Query/Sql/Select.php index 2335d62..78d5845 100644 --- a/src/Query/Sql/Select.php +++ b/src/Query/Sql/Select.php @@ -93,7 +93,7 @@ public function fields($fields) // when a string is given if (is_string($fields)) { - $fields = $this->stringArgumentToArray($fields); + $fields = $this->stringArgumentToArray($fields); } // it also could be an object elseif (is_object($fields)) @@ -107,13 +107,13 @@ public function fields($fields) // add the fields foreach($fields as $key => $field) { - // when we have a string as key we have an alias definition - if (is_string($key)) - { + // when we have a string as key we have an alias definition + if (is_string($key)) + { $this->addField($key, $field); - } else { + } else { $this->addField($field); - } + } } return $this; @@ -124,13 +124,13 @@ public function fields($fields) * * ->addField('title') * - * @param string $field - * @param string $alias + * @param string $field + * @param string $alias * @return self */ public function addField($field, $alias = null) { - $this->fields[] = array($field, $alias); return $this; + $this->fields[] = array($field, $alias); return $this; } /** @@ -222,23 +222,23 @@ public function addFieldRound($field, $decimals = 0, $alias = null) * * ->orderBy('created_at') * ->orderBy('modified_at', 'desc') - * + * * // multiple order statements * ->orderBy(['firstname', 'lastname'], 'desc') * * // muliple order statements with diffrent directions * ->orderBy(['firstname' => 'asc', 'lastname' => 'desc']) * - * @param array|string $cols - * @param string $order + * @param array|string $cols + * @param string $order * @return self */ public function orderBy($columns, $direction = 'asc') { - if (is_string($columns)) - { - $columns = $this->stringArgumentToArray($columns); - } + if (is_string($columns)) + { + $columns = $this->stringArgumentToArray($columns); + } foreach ($columns as $key => $column) { @@ -259,15 +259,15 @@ public function orderBy($columns, $direction = 'asc') * ->groupBy('id') * ->gorupBy(['id', 'category']) * - * @param array|string $keys + * @param array|string $keys * @return self */ public function groupBy($groupKeys) { - if (is_string($groupKeys)) - { - $groupKeys = $this->stringArgumentToArray($groupKeys); - } + if (is_string($groupKeys)) + { + $groupKeys = $this->stringArgumentToArray($groupKeys); + } foreach ($groupKeys as $groupKey) { @@ -282,21 +282,21 @@ public function groupBy($groupKeys) * * ->join('avatars', 'users.id', '=', 'avatars.user_id') * - * @param array|string $table - * @param string $localKey - * @param string $operator - * @param string $referenceKey - * @param string $type + * @param array|string $table + * @param string $localKey + * @param string $operator + * @param string $referenceKey + * @param string $type * * @return self */ public function join($table, $localKey, $operator = null, $referenceKey = null, $type = 'left') { - // validate the join type - if (!in_array($type, array('inner', 'left', 'right', 'outer'))) - { - throw new Exception('Invalid join type "'.$type.'" given. Available type: inner, left, right, outer'); - } + // validate the join type + if (!in_array($type, array('inner', 'left', 'right', 'outer'))) + { + throw new Exception('Invalid join type "'.$type.'" given. Available type: inner, left, right, outer'); + } // to make nested joins possible you can pass an closure // wich will create a new query where you can add your nested wheres @@ -312,67 +312,67 @@ public function join($table, $localKey, $operator = null, $referenceKey = null, $this->joins[] = array($type, $table, $subquery); return $this; } - $this->joins[] = array($type, $table, $localKey, $operator, $referenceKey); return $this; + $this->joins[] = array($type, $table, $localKey, $operator, $referenceKey); return $this; } /** * Left join same as join with special type * - * @param array|string $table - * @param string $localKey - * @param string $operator - * @param string $referenceKey + * @param array|string $table + * @param string $localKey + * @param string $operator + * @param string $referenceKey * * @return self */ public function leftJoin($table, $localKey, $operator = null, $referenceKey = null) { - return $this->join($table, $localKey, $operator, $referenceKey, 'left'); + return $this->join($table, $localKey, $operator, $referenceKey, 'left'); } /** * Left join same as join with special type * - * @param array|string $table - * @param string $localKey - * @param string $operator - * @param string $referenceKey + * @param array|string $table + * @param string $localKey + * @param string $operator + * @param string $referenceKey * * @return self */ public function rightJoin($table, $localKey, $operator = null, $referenceKey = null) { - return $this->join($table, $localKey, $operator, $referenceKey, 'right'); + return $this->join($table, $localKey, $operator, $referenceKey, 'right'); } /** * Left join same as join with special type * - * @param array|string $table - * @param string $localKey - * @param string $operator - * @param string $referenceKey + * @param array|string $table + * @param string $localKey + * @param string $operator + * @param string $referenceKey * * @return self */ public function innerJoin($table, $localKey, $operator = null, $referenceKey = null) { - return $this->join($table, $localKey, $operator, $referenceKey, 'inner'); + return $this->join($table, $localKey, $operator, $referenceKey, 'inner'); } /** * Left join same as join with special type * - * @param array|string $table - * @param string $localKey - * @param string $operator - * @param string $referenceKey + * @param array|string $table + * @param string $localKey + * @param string $operator + * @param string $referenceKey * * @return self */ public function outerJoin($table, $localKey, $operator = null, $referenceKey = null) { - return $this->join($table, $localKey, $operator, $referenceKey, 'outer'); + return $this->join($table, $localKey, $operator, $referenceKey, 'outer'); } /** @@ -433,14 +433,14 @@ public function get() $results = $this->executeResultFetcher(); // we always exprect an array here! - if (!is_array($results)) + if (!is_array($results) || empty($results)) { $results = array(); } // In case we should forward a key means using a value // from every result as array key. - if ($this->forwardKey !== false && is_string($this->forwardKey)) + if ((!empty($results)) && $this->forwardKey !== false && is_string($this->forwardKey)) { $rawResults = $results; $results = array(); @@ -459,7 +459,7 @@ public function get() } // Group the resuls by a items value - if ($this->groupResults !== false && is_string($this->groupResults)) + if ((!empty($results)) && $this->groupResults !== false && is_string($this->groupResults)) { $rawResults = $results; $results = array(); @@ -516,7 +516,7 @@ public function one() /** * Find something, means select one record by key * - * @param int $id + * @param int $id * @param string $key * @return mixed */ diff --git a/src/Query/Sql/SelectBase.php b/src/Query/Sql/SelectBase.php index fdc94db..5b1b02e 100644 --- a/src/Query/Sql/SelectBase.php +++ b/src/Query/Sql/SelectBase.php @@ -51,6 +51,36 @@ protected function stringArgumentToArray($argument) return array($argument); } + /** + * Will reset the current selects where conditions + * + * @return self + */ + public function resetWheres() + { + $this->wheres = array(); return $this; + } + + /** + * Will reset the current selects limit + * + * @return self + */ + public function resetLimit() + { + $this->limit = null; return $this; + } + + /** + * Will reset the current selects offset + * + * @return self + */ + public function resetOffset() + { + $this->offset = null; return $this; + } + /** * Create a where statement * diff --git a/tests/Query/Sql/BaseSql.php b/tests/Query/Sql/BaseSql.php index 8d7ab93..afe39b3 100644 --- a/tests/Query/Sql/BaseSql.php +++ b/tests/Query/Sql/BaseSql.php @@ -71,6 +71,14 @@ public function testWhere() )); } + /** + * BaseSql::whereReset + */ + public function testWhereReset() + { + $this->assertAttributes($this->createQuery()->where('id', 42)->resetWheres(), array()); + } + /** * BaseSql::orWhere */ @@ -211,6 +219,14 @@ public function testLimit() $this->assertAttributes($this->createQuery()->limit(10)->limit(null)); } + /** + * BaseSql::limitReset + */ + public function testLimitReset() + { + $this->assertAttributes($this->createQuery()->limit(10)->resetLimit(), array()); + } + /** * BaseSql::offset */ @@ -223,6 +239,14 @@ public function testOffset() $this->assertAttributes($this->createQuery()->limit(2, 5)->offset(3), array('limit' => 5, 'offset' => 3)); } + /** + * BaseSql::offsetReset + */ + public function testOffsetReset() + { + $this->assertAttributes($this->createQuery()->offset(10)->resetOffset(), array()); + } + /** * BaseSql::page */