Skip to content

Commit

Permalink
Merge branch 'hotfix/3031'
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Nov 20, 2012
2 parents a326c5c + 7f254f0 commit 49542a9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
27 changes: 15 additions & 12 deletions library/Zend/Db/Sql/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,27 @@ public function values(array $values, $flag = self::VALUES_SET)
throw new \InvalidArgumentException('values() expects an array of values');
}

// determine if this is assoc or a set of values
$keys = array_keys($values);
$firstKey = current($keys);

if ($flag == self::VALUES_SET) {
$this->columns = array();
$this->values = array();
}

if (is_string($firstKey)) {
if ($flag == self::VALUES_MERGE) {
$this->columns(array_merge($this->columns, $keys));
} else {
$this->columns($keys);
foreach ($keys as $key) {
if (($index = array_search($key, $this->columns)) !== false) {
$this->values[$index] = $values[$key];
} else {
$this->columns[] = $key;
$this->values[] = $values[$key];
}
}
$values = array_values($values);
} elseif (is_int($firstKey)) {
$values = array_values($values);
}

if ($flag == self::VALUES_MERGE) {
$this->values = array_merge($this->values, $values);
} else {
$this->values = $values;
// determine if count of columns should match count of values
$this->values = array_merge($this->values, array_values($values));
}

return $this;
Expand Down
10 changes: 10 additions & 0 deletions tests/ZendTest/Db/Sql/InsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ public function testValues()
$this->insert->values(array('foo' => 'bar'));
$this->assertEquals(array('foo'), $this->readAttribute($this->insert, 'columns'));
$this->assertEquals(array('bar'), $this->readAttribute($this->insert, 'values'));

// test will merge cols and values of previously set stuff
$this->insert->values(array('foo' => 'bax'), Insert::VALUES_MERGE);
$this->insert->values(array('boom' => 'bam'), Insert::VALUES_MERGE);
$this->assertEquals(array('foo', 'boom'), $this->readAttribute($this->insert, 'columns'));
$this->assertEquals(array('bax', 'bam'), $this->readAttribute($this->insert, 'values'));

$this->insert->values(array('foo' => 'bax'));
$this->assertEquals(array('foo'), $this->readAttribute($this->insert, 'columns'));
$this->assertEquals(array('bax'), $this->readAttribute($this->insert, 'values'));
}


Expand Down

0 comments on commit 49542a9

Please sign in to comment.