Skip to content

Commit

Permalink
fix from reset codeigniter4#2800
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsn committed Apr 5, 2020
1 parent 7eb0763 commit f05ee25
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
26 changes: 16 additions & 10 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -998,8 +998,8 @@ 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;
}

Expand All @@ -1009,7 +1009,7 @@ 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;
}

Expand Down Expand Up @@ -3328,6 +3328,12 @@ protected function resetSelect()
{
$this->db->setAliasedTables([]);
}

// Reset QBFrom part
if (! empty($this->QBFrom))
{
$this->from(array_shift($this->QBFrom), true);
}
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -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;
}
Expand Down
29 changes: 29 additions & 0 deletions tests/system/Database/Builder/FromTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

//--------------------------------------------------------------------
}

0 comments on commit f05ee25

Please sign in to comment.