00001 <?php 00002 /** 00003 * Magento 00004 * 00005 * NOTICE OF LICENSE 00006 * 00007 * This source file is subject to the Open Software License (OSL 3.0) 00008 * that is bundled with this package in the file LICENSE.txt. 00009 * It is also available through the world-wide-web at this URL: 00010 * http://opensource.org/licenses/osl-3.0.php 00011 * If you did not receive a copy of the license and are unable to 00012 * obtain it through the world-wide-web, please send an email 00013 * to license@magentocommerce.com so we can send you a copy immediately. 00014 * 00015 * DISCLAIMER 00016 * 00017 * Do not edit or add to this file if you wish to upgrade Magento to newer 00018 * versions in the future. If you wish to customize Magento for your 00019 * needs please refer to http://www.magentocommerce.com for more information. 00020 * 00021 * @category Mage 00022 * @package Mage_Core 00023 * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com) 00024 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 00025 */ 00026 00027 /** 00028 * Active record implementation 00029 * 00030 * @author Magento Core Team <core@magentocommerce.com> 00031 */ 00032 class Mage_Core_Model_Resource_Iterator extends Varien_Object 00033 { 00034 /** 00035 * Walk over records fetched from query one by one using callback function 00036 * 00037 * @param Zend_Db_Statement_Interface|Zend_Db_Select|string $query 00038 * @param array|string $callback 00039 * @param array $args 00040 * @return Mage_Core_Model_Resource_Activerecord 00041 */ 00042 public function walk($query, array $callbacks, array $args=array()) 00043 { 00044 $stmt = $this->_getStatement($query); 00045 00046 $args['idx'] = 0; 00047 while ($row = $stmt->fetch()) { 00048 $args['row'] = $row; 00049 foreach ($callbacks as $callback) { 00050 $result = call_user_func($callback, $args); 00051 if (!empty($result)) { 00052 $args = array_merge($args, $result); 00053 } 00054 } 00055 $args['idx']++; 00056 } 00057 00058 return $this; 00059 } 00060 00061 /** 00062 * Fetch Zend statement instance 00063 * 00064 * @param Zend_Db_Statement_Interface|Zend_Db_Select|string $query 00065 * @param Zend_Db_Adapter_Abstract $conn 00066 * @return Zend_Db_Statement_Interface 00067 */ 00068 protected function _getStatement($query, $conn=null) 00069 { 00070 if ($query instanceof Zend_Db_Statement_Interface) { 00071 return $query; 00072 } 00073 00074 if ($query instanceof Zend_Db_Select) { 00075 return $query->query(); 00076 } 00077 00078 $hlp = Mage::helper('core'); 00079 00080 if (is_string($query)) { 00081 if (!$conn instanceof Zend_Db_Adapter_Abstract) { 00082 Mage::throwException($hlp->__('Invalid connection')); 00083 } 00084 return $conn->query($query); 00085 } 00086 00087 Mage::throwException($hlp->__('Invalid query')); 00088 } 00089 }