diff --git a/system/Database/BaseBuilder.php b/system/Database/BaseBuilder.php index a49a061b28d2..2b722fe7e966 100644 --- a/system/Database/BaseBuilder.php +++ b/system/Database/BaseBuilder.php @@ -981,12 +981,12 @@ public function orHavingNotIn(string $key = null, $values = null, bool $escape = * @used-by whereNotIn() * @used-by orWhereNotIn() * - * @param string $key The field to search - * @param array|Closure $values The values searched on, or anonymous function with subquery - * @param boolean $not If the statement would be IN or NOT IN - * @param string $type - * @param boolean $escape - * @param string $clause (Internal use only) + * @param string $key The field to search + * @param array|Closure $values The values searched on, or anonymous function with subquery + * @param boolean $not If the statement would be IN or NOT IN + * @param string $type + * @param boolean $escape + * @param string $clause (Internal use only) * @throws InvalidArgumentException * * @return BaseBuilder @@ -998,9 +998,9 @@ protected function _whereIn(string $key = null, $values = null, bool $not = fals if (CI_DEBUG) { throw new InvalidArgumentException(sprintf('%s() expects $key to be a non-empty string', debug_backtrace(0, 2)[1]['function'])); - } - - return this; + } + + return $this; } if ($values === null || (! is_array($values) && ! ($values instanceof Closure))) @@ -1009,8 +1009,8 @@ protected function _whereIn(string $key = null, $values = null, bool $not = fals { throw new InvalidArgumentException(sprintf('%s() expects $values to be of type array or closure', debug_backtrace(0, 2)[1]['function'])); } - - return this; + + return $this; } is_bool($escape) || $escape = $this->db->protectIdentifiers; @@ -3328,6 +3328,12 @@ protected function resetSelect() { $this->db->setAliasedTables([]); } + + // Reset QBFrom part + if (! empty($this->QBFrom)) + { + $this->from(array_shift($this->QBFrom), true); + } } //-------------------------------------------------------------------- @@ -3426,7 +3432,7 @@ protected function setBind(string $key, $value = null, bool $escape = true): str return $key; } - if (!array_key_exists($key, $this->bindsKeyCount)) + if (! array_key_exists($key, $this->bindsKeyCount)) { $this->bindsKeyCount[$key] = 0; } diff --git a/tests/system/Database/Builder/FromTest.php b/tests/system/Database/Builder/FromTest.php index fb5570ef5e8b..e2c3866278d7 100644 --- a/tests/system/Database/Builder/FromTest.php +++ b/tests/system/Database/Builder/FromTest.php @@ -69,4 +69,33 @@ public function testFromWithMultipleTablesAsString() } //-------------------------------------------------------------------- + + public function testFromReset() + { + $builder = new BaseBuilder('user', $this->db); + + $builder->from(['jobs', 'roles']); + + $expectedSQL = 'SELECT * FROM "user", "jobs", "roles"'; + + $this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); + + $expectedSQL = 'SELECT * FROM "user"'; + + $this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); + + $expectedSQL = 'SELECT *'; + + $builder->from(null, true); + + $this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); + + $expectedSQL = 'SELECT * FROM "jobs"'; + + $builder->from('jobs'); + + $this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); + } + + //-------------------------------------------------------------------- }