Browse Source

ZF-2953
Altered Zend_Db_Table_Abstract::insert() to check for the existence of sensible primary key values


git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23621 44c647ce-9c0f-0410-b52a-842ac1e357ba

ralph 15 years ago
parent
commit
c0377a0b06
2 changed files with 58 additions and 3 deletions
  1. 12 2
      library/Zend/Db/Table/Abstract.php
  2. 46 1
      tests/Zend/Db/Table/TestCommon.php

+ 12 - 2
library/Zend/Db/Table/Abstract.php

@@ -1046,14 +1046,24 @@ abstract class Zend_Db_Table_Abstract
          */
         if (is_string($this->_sequence) && !isset($data[$pkIdentity])) {
             $data[$pkIdentity] = $this->_db->nextSequenceId($this->_sequence);
+            $pkSuppliedBySequence = true;
         }
 
         /**
          * If the primary key can be generated automatically, and no value was
          * specified in the user-supplied data, then omit it from the tuple.
+         * 
+         * Note: this checks for sensible values in the supplied primary key
+         * position of the data.  The following values are considered empty:
+         *   null, false, true, '', array()
          */
-        if (array_key_exists($pkIdentity, $data) && $data[$pkIdentity] === null) {
-            unset($data[$pkIdentity]);
+        if (!isset($pkSuppliedBySequence) && array_key_exists($pkIdentity, $data)) {
+            if ($data[$pkIdentity] === null                                        // null
+                || $data[$pkIdentity] === ''                                       // empty string
+                || is_bool($data[$pkIdentity])                                     // boolean
+                || (is_array($data[$pkIdentity]) && empty($data[$pkIdentity]))) {  // empty array
+                unset($data[$pkIdentity]);
+            }
         }
 
         /**

+ 46 - 1
tests/Zend/Db/Table/TestCommon.php

@@ -779,8 +779,9 @@ abstract class Zend_Db_Table_TestCommon extends Zend_Db_Table_TestSetup
 
     /**
      * See ZF-1739 in our issue tracker.
+     * @group ZF-1739
      */
-    public function testTableInsertMemoryUsageZf1739()
+    public function testTableInsertWithHighMemoryUsage()
     {
         $this->markTestSkipped('Very slow test inserts thousands of rows');
 
@@ -809,6 +810,50 @@ abstract class Zend_Db_Table_TestCommon extends Zend_Db_Table_TestSetup
         $mem_delta = $mem2-$mem1;
         $this->assertThat($mem_delta, $this->lessThan(513));
     }
+    
+    /**
+     * @group ZF-2953
+     */
+    public function testTableInsertWithEmptyValueAsPrimaryKey()
+    {
+        $table = $this->_table['bugs'];
+        $row = array (
+            'bug_description' => 'New bug',
+            'bug_status'      => 'NEW',
+            'created_on'      => '2007-04-02',
+            'updated_on'      => '2007-04-02',
+            'reported_by'     => 'micky',
+            'assigned_to'     => 'goofy',
+            'verified_by'     => 'dduck'
+        );
+        
+        // empty string
+        $row['bug_id'] = '';
+        $insertResult = $table->insert($row);
+        $this->assertTrue(is_numeric($insertResult), 'Empty string did not return assigned primary key');
+        
+        // false (bool)
+        $row['bug_id'] = false;
+        $insertResult = $table->insert($row);
+        $this->assertTrue(is_numeric($insertResult), 'Bool false did not return assigned primary key');
+
+        // empty array
+        $row['bug_id'] = array();
+        $insertResult = $table->insert($row);
+        $this->assertTrue(is_numeric($insertResult), 'Empty array did not return assigned primary key');
+        
+        // zero '0'
+        $row['bug_id'] = '0';
+        $table->delete('bug_id > 0'); // clear table
+        $insertResult = $table->insert($row);
+        $this->assertEquals('0', $insertResult, 'Zero string did not return assigned primary key');
+        
+        // zero 0
+        $row['bug_id'] = 0;
+        $table->delete('bug_id > 0'); // clear table
+        $insertResult = $table->insert($row);
+        $this->assertEquals('0', $insertResult, 'Zero int did not return assigned primary key');
+    }
 
     public function testTableUpdate()
     {