class System_ModelMapper { protected $_dbTable; // protected $_modelName = ''; // , - public function __construct() { parent::__construct(); if (empty($this->_modelName)) { $parts = explode('_', get_class($this)); $this->_modelName = str_replace('Mapper', '', $parts[2]); } } // dbtable /** * @return /Zend_Db_Table_Abstract */ public function getDbTable() { if (null === $this->_dbTable) { $this->setDbTable('Application_Model_DbTable_' . $this->_modelName); } return $this->_dbTable; } public function setDbTable($dbTable) { if (is_string($dbTable)) { $dbTable = new $dbTable(); } if (!$dbTable instanceof Zend_Db_Table_Abstract) { throw new Exception('Invalid table data gateway provided'); } $this->_dbTable = $dbTable; return $this; } /** * * @param array $params * @return System_Model */ public function getModel($params = array()) { $getInstance = 'Application_Model_' . $this->_modelName; return new $getInstance($params); } … }
/** * * @param array $data * @return string */ protected function objectCacheId($data) { $fields = array_keys($data); $values = md5(json_encode(array_values($data))); return 'find_' . $this->_modelName . '_' . join('_', $fields) . '_' . $values; } /** * * @param $object System_Model * @return string */ public function getObjectCacheTag($object) { return 'object_' . $this->_modelName . '_' .$object->get_id(); } /** * * @param numeric $id ID * @param mixed $obj , * @param bool $cache * @return bool|System_Model */ public function find($id, $obj = false, $cache = false) { return $this->findByFields(array('id' => $id), $obj, $cache); } /** * * @param array $data * @param mixed $obj , * @param bool $cache * @return bool|System_Model */ public function findByFields($data, $obj = false, $cache = false) { // - , ( , ( :) ) if ($cache) { $cacheId = $this->objectCacheId($data); if (Zend_Registry::isRegistered(CACHE_NAME) { /** @var $cache System_Cache_Core */ $cache =& Zend_Registry::get(CACHE_NAME); // - if ($cache->test($cacheId)) { return $cache->load($cacheId); } } else { $cache = false; } } // Zend_Db_Table $select = $this->getDbTable()->select(); foreach ($data as $field => $value) { $select->where($select->getAdapter()->quoteIdentifier($field) . ' = ?', $value); } $row = $this->getDbTable()->fetchRow($select); if ($row) { if ($obj === false) { $obj = $this->getModel(); } $obj->setOptions($row->toArray()); } else { $obj = false; } // - if ($cache) { $cache->save($obj, $cacheId); } return $obj; }
/** * , * @param array $data * @param bool|string|array $order * @param bool|System_Paginator $paginator * @return string */ protected function listCacheId($data = array(), $order = false, $paginator = false) { $fields = array_keys($data); $values = md5(json_encode(array_values($data))); return sprintf('%s_%s_%s_%s_%s', $this->getListCacheTag(), join('_', $fields), $values, empty($order) ? '' : md5(json_encode($order)), is_object($paginator) ? $paginator->page . '_' . $paginator->limit : '' ); } /** * * @return string */ public function getListCacheTag() { return 'list_' . $this->_modelName; } /** * * @param array $data * @param bool|string|array $order * @param bool|System_Paginator $paginator * @param bool|string $cache */ public function fetchByFields($data = array(), $order = false, $paginator = false, $cache = false) { if ($cache) { $cacheId = $this->listCacheId($data, $order, $paginator); $cache .= 'Cache'; if (Zend_Registry::isRegistered(CACHE_NAME)) { /** @var $cache System_Cache_Core */ $cache =& Zend_Registry::get(CACHE_NAME); if ($cache->test($cacheId)) { return $cache->load($cacheId); } } else { $cache = false; } } // , , $select = $this->getDbTable()->select(); $select_paginator = $this->getDbTable()->select(true); foreach ($data as $field => $value) { $s = '='; // value ('=', 2) ('<=', 10) if (is_array($value)) { $s = $value[0]; $value = $value[1]; } $select->where($select->getAdapter()->quoteIdentifier($field) . " $s ?", $value); $select_paginator->where($select->getAdapter()->quoteIdentifier($field) . " $s ?", $value); } // if (!empty($order)) { $select->order($order); } else { $select->order('id ASC'); } // , if (is_object($paginator)) { // , $fetch_count = $this->getDbTable()->fetchRow($select_paginator->columns('count(id) as _c'))->toArray(); $paginator->total = $fetch_count['_c']; // , , if ($paginator->page > $paginator->getLastPage()) $paginator->page = $paginator->getLastPage(); // $select->limitPage($paginator->page, $paginator->limit); } $resultSet = $this->getDbTable()->fetchAll($select); $result = $this->rowsToObj($resultSet); // - ( limit) if (is_object($paginator)) { $paginator->inlist = count($result); } if ($cache) { $cache->save($result, $cacheId); } return $result; } /** * * @param bool|string|array $order * @param bool|System_Paginator $paginator * @param bool|string $cache * @return array|bool */ public function fetchAll($order = false, $paginator = false, $cache = false) { return $this->fetchByFields(array(), $order, $paginator, $cache); } /** * * @param Zend_Db_Table_Rowset_Abstract $rowset * @return array|bool */ protected function rowsToObj($rowset) { if (!empty($rowset)) { $entries = array(); foreach ($rowset as $row) { /** @var $entry System_Model */ $entry = $this->getModel($row->toArray()); $entries[$entry->get_id()] = $entry; } return $entries; } return false; }
class System_Cache_Core extends Zend_Cache_Core { /** * save */ public function save($data, $id = null, $tags = array(), $specificLifetime = false, $priority = 8) { // $ida = explode('_', $id); // , switch ($ida[0]) { case 'list': // 2 ( ) $tag = join('_', array_splice($ida, 0, 2)); $this->updateTagList($tag, $id); break; case 'find': // - if ($data instanceof System_Model) { $tag = $data->get_mapper()->getObjectCacheTag($data); $this->updateTagList($tag, $id); } break; } // return parent::save($data, $id, $tags, $specificLifetime, $priority); } /** * * @param string $tag * @param string $cacheId */ public function updateTagList($tag, $cacheId) { // $list = $this->getListByTag($tag); $list[] = $cacheId; // $this->saveListByTag($tag, $list); } /** * * @param string $tag */ protected function getListByTag($tag) { $tagcacheId = '_taglist_' . $tag; $list = array(); if ($this->test($tagcacheId)) { $list = $this->load($tagcacheId); } return $list; } /** * * @param string $tag * @param array $list */ protected function saveListByTag($tag, $list) { $tagcacheId = '_taglist_' . $tag; $this->save($list, $tagcacheId); } /** * * @param System_Model $object */ public function removeByObject($object = null) { if ($object instanceof System_Model) { // $this->removeByTag($object->get_mapper()->getListCacheTag()); // if ($object->get_id()) { $this->removeByTag($object->get_mapper()->getObjectCacheTag($object)); } } } /** * * @param string $tag */ public function removeByTag($tag) { // $list = $this->getListByTag($tag); // foreach ((array)$list as $cacheId) { $this->remove($cacheId); } // , , $this->saveListByTag($tag, array()); } }
/** * * @param System_Model $object * @param boolean $isInsert * @return array|bool|mixed */ public function save($object, $isInsert = false) { $data = $object->toArray(); $find = array('id = ?' => $object->get_id()); if (null === ($id_value = $object->get_id())) { $isInsert = true; unset($data['id']); } if ($isInsert) { $pk = $this->getDbTable()->insert($data); if ($pk) { $object->set_id($pk); } $this->resetCache(); return $pk; } else { // - return $this->getDbTable()->update($data, $find) && $this->resetCache($object); } } /** * * @param $object System_Model * @return array|bool|mixed */ public function insert($object) { return $this->save($object, true); } /** * * @param $object System_Model * @return bool */ public function remove($object) { $primary = $this->getDbTable()->get_primary(); $where = array('id = ?' => $object->get_id()); // - return ($this->getDbTable()->delete($where) && $this->resetCache($object)); } /** * * @param System_Model $object * @param array $cacheIds * @return bool */ public function resetCache($object = null, $cacheIds = array()) { // if (Zend_Registry::isRegistered(CACHE_NAME)) { /** @var $cache System_Cache_Core */ $cache = Zend_Registry::get(CACHE_NAME); if (!empty($object)) { // $cache->removeByObject($object); } else { // $cache->removeByTag($this->getListCacheTag()); } foreach ($cacheIds as $cacheId) { $cache->remove($cacheId); } } return true; } }
Source: https://habr.com/ru/post/135047/
All Articles