Просмотр исходного кода

ZF-9836
- Fixed Zend_Db_Table_Row so that objects are Traversable/Iterable


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

ralph 15 лет назад
Родитель
Сommit
fdc828adb6
2 измененных файлов с 26 добавлено и 1 удалено
  1. 6 1
      library/Zend/Db/Table/Row/Abstract.php
  2. 20 0
      tests/Zend/Db/Table/Row/TestCommon.php

+ 6 - 1
library/Zend/Db/Table/Row/Abstract.php

@@ -32,7 +32,7 @@ require_once 'Zend/Db.php';
  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  */
-abstract class Zend_Db_Table_Row_Abstract implements ArrayAccess
+abstract class Zend_Db_Table_Row_Abstract implements ArrayAccess, IteratorAggregate
 {
 
     /**
@@ -642,6 +642,11 @@ abstract class Zend_Db_Table_Row_Abstract implements ArrayAccess
         return $result;
     }
 
+    public function getIterator()
+    {
+        return new ArrayIterator((array) $this->_data);
+    }
+    
     /**
      * Returns the column/value data as an array.
      *

+ 20 - 0
tests/Zend/Db/Table/Row/TestCommon.php

@@ -875,7 +875,27 @@ abstract class Zend_Db_Table_Row_TestCommon extends Zend_Db_Table_TestSetup
         }
     }
 
+    /**
+     * @group ZF-9836
+     */
+    public function testTableRowIsIterable()
+    {
+        $table = $this->_table['bugs'];
 
+        $rowset = $table->find(1);
+        $row = $rowset->current();
+        $this->assertTrue($row instanceof Traversable);
+        $this->assertTrue($row instanceof IteratorAggregate);
+        $this->assertType('ArrayIterator', $row->getIterator());
+        
+        $count=0;
+        foreach ($row as $columnValue) {
+            $count++;
+        }
+        
+        $this->assertEquals(8, $count, 'The row was iterated, there should be 8 columns iterated');
+    }
+    
 
     /**
      * Utility methods below