public function getRows($where = null, $order = null, $limit = null, $start = null) { if (!is_object($where)) { if (is_string($where)) { $where = array($where); } $select = $this->select($where, $order, $limit, $start); } else { $select = $where; } if ($select->getPart(Kwf_Model_Select::WHERE_EQUALS) || $select->getPart(Kwf_Model_Select::WHERE_NULL)) { $cacheSetting = $this->_getCacheSetting($select); if ($cacheSetting) { $cacheId = $this->_getCacheId($cacheSetting); if (!isset($this->_cacheData[$cacheId])) { if (!($this->_cacheData[$cacheId] = $this->_getCache()->load($cacheId))) { $this->_cacheData[$cacheId] = $this->_getCacheData($cacheSetting); $this->_getCache()->save($this->_cacheData[$cacheId], $cacheId); } } $whereEquals = $select->getPart(Kwf_Model_Select::WHERE_EQUALS); $whereNull = $select->getPart(Kwf_Model_Select::WHERE_NULL); $values = array(); foreach ($cacheSetting['index'] as $value) { if ($whereEquals) { if (isset($whereEquals[$value])) { $values[] = $this->_escapeSearchKeyElement($whereEquals[$value]); } } if ($whereNull) { foreach (array_values($whereNull) as $whereKey => $whereValue) { if ($whereValue == $value) { $values[] = $this->_escapeSearchKeyElement(null); } } } } $valuesbefore = $values; $v = implode($values, '_'); if (isset($this->_cacheData[$cacheId][$v])) { $data = $this->_cacheData[$cacheId][$v]; } else { $data = array(); } return new $this->_rowsetClass(array('model' => $this, 'rowClass' => $this->_rowClass, 'cacheData' => $data)); } } return parent::getRows($where, $order, $limit, $start); }
public function testGetRows() { $fnf = $this->getMock('Kwf_Model_FnF', array('getRows')); $fnf->expects($this->once())->method('getRows')->with($this->equalTo(null), $this->equalTo(null), $this->equalTo(null), $this->equalTo(null)); $proxy = new Kwf_Model_Proxy(array('proxyModel' => $fnf)); $rowset = $proxy->getRows(); $select = new Kwf_Model_Select(); $select->whereId(2); $fnf = $this->getMock('Kwf_Model_FnF', array('getRows')); $fnf->expects($this->once())->method('getRows')->with($this->equalTo($select), $this->equalTo(null), $this->equalTo(null), $this->equalTo(null)); $proxy = new Kwf_Model_Proxy(array('proxyModel' => $fnf)); $rowset = $proxy->getRows($select); $select = new Kwf_Model_Select(); $select->whereId(2); $fnf = new Kwf_Model_FnF(); $fnf->setData(array(array('id' => 2, 'name' => 'foo'), array('id' => 18, 'name' => 'bar'))); $proxy = new Kwf_Model_Proxy(array('proxyModel' => $fnf)); $rowset = $proxy->getRows($select); $this->assertEquals('Kwf_Model_Proxy_Rowset', get_class($rowset)); $this->assertEquals('Kwf_Model_Proxy_Row', get_class($rowset->current())); $this->assertEquals(1, count($rowset)); $this->assertEquals('foo', $rowset->current()->name); }
private function _synchronize($overrideMaxSyncDelay = self::SYNC_AFTER_DELAY) { $select = $this->_getSynchronizeVars($overrideMaxSyncDelay); if ($select['type'] !== self::SYNC_SELECT_TYPE_NOSYNC) { $start = microtime(true); $this->_beforeSynchronize(); // it's possible to use $this->getProxyModel()->copyDataFromModel() // but if < 20 rows are copied, array is faster than sql or csv $format = null; if ($select['type'] === self::SYNC_SELECT_TYPE_SELECT) { if (in_array(self::FORMAT_ARRAY, $this->getProxyModel()->getSupportedImportExportFormats()) && in_array(self::FORMAT_ARRAY, $this->getSourceModel()->getSupportedImportExportFormats())) { $format = self::FORMAT_ARRAY; } } if (!$format) { $format = self::_optimalImportExportFormat($this->getProxyModel(), $this->getSourceModel()); } $options = array(); $data = $this->getSourceModel()->export($format, $select['select']); $exportTime = microtime(true) - $start; $start = microtime(true); if ($select['type'] === self::SYNC_SELECT_TYPE_ALL && $this->_truncateBeforeFullImport) { $this->getProxyModel()->deleteRows($this->getProxyModel()->select()); } else { $options['replace'] = true; } if ($data) { if ($this->_callObserverForRowUpdates) { $pk = $this->getProxyModel()->getPrimaryKey(); $s = $this->getProxyModel()->select()->order($pk, 'DESC')->limit(1); $maxRow = $this->getProxyModel()->getRow($s); } $this->getProxyModel()->import($format, $data, $options); if ($this->_callObserverForRowUpdates) { foreach (parent::getRows($select['select']) as $row) { if (!$maxRow || $row->{$pk} > $maxRow->{$pk}) { $this->_observedRows['insert'][] = $row; } else { $this->_observedRows['update'][] = $row; } } } } $importTime = microtime(true) - $start; $tableName = ''; if ($this->getProxyModel() instanceof Kwf_Model_Db) { $tableName = $this->getProxyModel()->getTableName(); } $msg = date('Y-m-d H:i:s') . ' ' . str_replace('cache_', '', $tableName) . ' ' . $format; if (is_array($data)) { $msg .= " " . count($data) . " entries"; } else { if (is_string($data)) { $msg .= " " . strlen($data) . " bytes"; } } $msg .= ' export: ' . round($exportTime, 2) . 's'; $msg .= ' import: ' . round($importTime, 2) . 's'; //$msg .= ' SELECT: '.str_replace("\n", " ", print_r($select, true)); file_put_contents('log/mirrorcache', $msg . "\n", FILE_APPEND); return true; } return false; }