Skip to content

Loading…

Zend\Db\Sql Fix for Insert's Merge and Set capabilities with simlar keys #3031

Merged
merged 1 commit into from

2 participants

@ralphschindler
Zend Framework member
@ralphschindler ralphschindler Zend\Db\Sql
* A better fix and unit test for #2579 and #2999
7f254f0
@weierophinney weierophinney added a commit that referenced this pull request
@weierophinney weierophinney Merge branch 'hotfix/3031' into develop
Forward port #3031
3c65424
@weierophinney weierophinney merged commit 7f254f0 into zendframework:develop
@weierophinney weierophinney added a commit that referenced this pull request
@weierophinney weierophinney Merge branch 'hotfix/3031'
Close #3031
Fixes #2579
Fixes #2999
49542a9
@ghost Unknown pushed a commit that referenced this pull request
@weierophinney weierophinney Merge branch 'hotfix/3031'
Close #3031
Fixes #2579
Fixes #2999
522efb0
@ghost Unknown pushed a commit that referenced this pull request
@weierophinney weierophinney Merge branch 'hotfix/3031' into develop
Forward port #3031
88e89b3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 20, 2012
  1. @ralphschindler

    Zend\Db\Sql

    ralphschindler committed
    * A better fix and unit test for #2579 and #2999
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
View
27 library/Zend/Db/Sql/Insert.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;
View
10 tests/ZendTest/Db/Sql/InsertTest.php
@@ -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'));
}
Something went wrong with that request. Please try again.