소스 검색

ZF-8486
Fix for Zend_Db_Table_Rowset throwing exception after iteration and calling getRow() without seek


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

ralph 15 년 전
부모
커밋
27cbc8af7c
2개의 변경된 파일77개의 추가작업 그리고 18개의 파일을 삭제
  1. 29 18
      library/Zend/Db/Table/Rowset/Abstract.php
  2. 48 0
      tests/Zend/Db/Table/Rowset/TestCommon.php

+ 29 - 18
library/Zend/Db/Table/Rowset/Abstract.php

@@ -245,20 +245,8 @@ abstract class Zend_Db_Table_Rowset_Abstract implements SeekableIterator, Counta
             return null;
         }
 
-        // do we already have a row object for this position?
-        if (empty($this->_rows[$this->_pointer])) {
-            $this->_rows[$this->_pointer] = new $this->_rowClass(
-                array(
-                    'table'    => $this->_table,
-                    'data'     => $this->_data[$this->_pointer],
-                    'stored'   => $this->_stored,
-                    'readOnly' => $this->_readOnly
-                )
-            );
-        }
-
         // return the row object
-        return $this->_rows[$this->_pointer];
+        return $this->_loadAndReturnRow($this->_pointer);
     }
 
     /**
@@ -390,17 +378,17 @@ abstract class Zend_Db_Table_Rowset_Abstract implements SeekableIterator, Counta
      */
     public function getRow($position, $seek = false)
     {
-        $key = $this->key();
         try {
-            $this->seek($position);
-            $row = $this->current();
+            $row = $this->_loadAndReturnRow($position);
         } catch (Zend_Db_Table_Rowset_Exception $e) {
             require_once 'Zend/Db/Table/Rowset/Exception.php';
             throw new Zend_Db_Table_Rowset_Exception('No row could be found at position ' . (int) $position, 0, $e);
         }
-        if ($seek == false) {
-            $this->seek($key);
+        
+        if ($seek == true) {
+            $this->seek($position);
         }
+        
         return $row;
     }
 
@@ -420,5 +408,28 @@ abstract class Zend_Db_Table_Rowset_Abstract implements SeekableIterator, Counta
         }
         return $this->_data;
     }
+    
+    protected function _loadAndReturnRow($position)
+    {
+        if (!isset($this->_data[$position])) {
+            require_once 'Zend/Db/Table/Rowset/Exception.php';
+            throw new Zend_Db_Table_Rowset_Exception("Data for provided position does not exist");
+        }
+        
+        // do we already have a row object for this position?
+        if (empty($this->_rows[$position])) {
+            $this->_rows[$position] = new $this->_rowClass(
+                array(
+                    'table'    => $this->_table,
+                    'data'     => $this->_data[$position],
+                    'stored'   => $this->_stored,
+                    'readOnly' => $this->_readOnly
+                )
+            );
+        }
+
+        // return the row object
+        return $this->_rows[$position];
+    }
 
 }

+ 48 - 0
tests/Zend/Db/Table/Rowset/TestCommon.php

@@ -291,4 +291,52 @@ abstract class Zend_Db_Table_Rowset_TestCommon extends Zend_Db_Table_TestSetup
         $this->assertEquals(0, $rowset->key());
     }
 
+
+    
+    /**
+     * @group ZF-8486
+     */
+    public function testTableRowsetIteratesAndWillReturnLastRowAfter()
+    {
+        $table = $this->_table['bugs'];
+        $rowset = $table->fetchAll('bug_id IN (1,2,3,4)', 'bug_id ASC');
+        foreach ($rowset as $row) {
+            $lastRow = $row;
+        }
+        
+        $numRows = $rowset->count();
+        $this->assertEquals(4, $numRows);
+        
+        $rowset->seek(3);
+        $seekLastRow = $rowset->current();
+        
+        $this->assertSame($lastRow, $seekLastRow);
+    }
+    
+    /**
+     * @group ZF-8486
+     */
+    public function testTableRowsetThrowsExceptionOnInvalidSeek()
+    {
+        $table = $this->_table['bugs'];
+        $rowset = $table->fetchAll('bug_id IN (1,2,3,4)', 'bug_id ASC');
+        $rowset->seek(3);
+        
+        $this->setExpectedException('Zend_Db_Table_Rowset_Exception', 'Illegal index 4');
+        $rowset->seek(4);
+    }
+    
+    /**
+     * @group ZF-8486
+     */
+    public function testTableRowsetThrowsExceptionOnInvalidGetRow()
+    {
+        $table = $this->_table['bugs'];
+        $rowset = $table->fetchAll('bug_id IN (1,2,3,4)', 'bug_id ASC');
+        $rowset->getRow(3);
+        
+        $this->setExpectedException('Zend_Db_Table_Rowset_Exception', 'No row could be found at position 4');
+        $rowset->getRow(4);
+    }
+    
 }