Permalink
Browse files

Merge branch 'hotfix/3031' into develop

Forward port #3031
  • Loading branch information...
2 parents f1d5c4b + 7f254f0 commit 3c65424eeba261910ed4d7e9c1205b05c162dce3 @weierophinney weierophinney committed Nov 20, 2012
Showing with 25 additions and 12 deletions.
  1. +15 −12 library/Zend/Db/Sql/Insert.php
  2. +10 −0 tests/ZendTest/Db/Sql/InsertTest.php
@@ -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;
@@ -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'));
}

0 comments on commit 3c65424

Please sign in to comment.