Skip to content

Commit

Permalink
Merge pull request #396 from koenpunt/empty-array-for-IN-condition
Browse files Browse the repository at this point in the history
Fix error when using find with an empty array argument
  • Loading branch information
cvanschalkwijk committed Jun 28, 2014
2 parents 0b76001 + f6c63f7 commit b860920
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/Expressions.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,24 @@ private function substitute(&$values, $substitute, $pos, $parameter_index)

if (is_array($value))
{
$value_count = count($value);

if ($value_count === 0)
if ($substitute)
return 'NULL';
else
return self::ParameterMarker;

if ($substitute)
{
$ret = '';

for ($i=0,$n=count($value); $i<$n; ++$i)
for ($i=0, $n=$value_count; $i<$n; ++$i)
$ret .= ($i > 0 ? ',' : '') . $this->stringify_value($value[$i]);

return $ret;
}
return join(',',array_fill(0,count($value),self::ParameterMarker));
return join(',',array_fill(0,$value_count,self::ParameterMarker));
}

if ($substitute)
Expand Down
9 changes: 9 additions & 0 deletions test/ActiveRecordFindTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ public function test_find_all_with_no_bind_values()
$this->assert_equals(1,$authors[0]->author_id);
}

/**
* @expectedException ActiveRecord\DatabaseException
*/
public function test_find_all_with_empty_array_bind_value_throws_exception()
{
$authors = Author::find('all',array('conditions' => array('author_id IN(?)', array())));
$this->assertCount(0,$authors);
}

public function test_find_hash_using_alias()
{
$venues = Venue::all(array('conditions' => array('marquee' => 'Warner Theatre', 'city' => array('Washington','New York'))));
Expand Down
7 changes: 7 additions & 0 deletions test/ExpressionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ public function test_zero_variable()
$this->assert_equals(array(0),$a->values());
}

public function test_empty_array_variable()
{
$a = new Expressions(null,'id IN(?)',array());
$this->assert_equals('id IN(?)',$a->to_s());
$this->assert_equals(array(array()),$a->values());
}

public function test_ignore_invalid_parameter_marker()
{
$a = new Expressions(null,"question='Do you love backslashes?' and id in(?)",array(1,2));
Expand Down

0 comments on commit b860920

Please sign in to comment.