📄 datamanager.class.php
字号:
// Is manager ready to do the job? if (!$this->isReady()) { return null; } // if // If caching and we dont need to reload check the cache... if (!$force_reload && $this->getCaching()) { $item = $this->getCachedItem($id); if (instance_of($item, $this->getItemClass())) { return $item; } // if } // if // Get object from row... $object = $this->loadFromRow($this->loadRow($id)); // Check item... if (!instance_of($object, $this->getItemClass())) { return null; } // if // If loaded cache and return... if ($object->isLoaded()) { if ($this->getCaching()) { $this->cacheItem($object); } // if return $object; } // if // Item not loaded... return null; } // end func load /** * Load row from database based on ID * * @access public * @param mixed $id * @return array */ function loadRow($id) { $sql = sprintf("SELECT %s FROM %s WHERE %s", implode(', ', $this->getLoadColumns(true)), $this->getTableName(true), $this->getConditionsById($id) ); // sprintf return DB::executeOne($sql); } // loadRow /** * Load item from database row * * @access public * @param array $row Row from witch we need to load data... * @return DataObject */ function loadFromRow($row) { // Is manager ready? if (!$this->isReady()) { return null; } // if // OK, get class and construct item... $class = $this->getItemClass(); $item = new $class(); // If not valid item break if (!instance_of($item, 'DataObject')) { return null; } // if // Load item... if ($item->loadFromRow($row) && $item->isLoaded()) { if ($this->getCaching()) { $this->cacheItem($item); } // if return $item; } // if // Item not loaded, from some reason return null; } // end func loadFromRow /** * Return condition part of query by value(s) of PK column(s) * * @access public * @param array or string $id * @return string */ function getConditionsById($id) { // Prepare data... $pks = $this->getPkColumns(); // Multiple PKs? if (is_array($pks)) { // Ok, prepare it... $where = array(); // Loop PKs foreach ($pks as $column) { if (isset($id[$column])) { $where[] = sprintf('%s = %s', DB::escapeField($column), DB::escape($id[$column])); } // if } // foreach // Join... if (is_array($where) && count($where)) { return count($where) > 1 ? implode(' AND ', $where) : $where[0]; } else { return ''; } // if } else { return sprintf('%s = %s', DB::escapeField($pks), DB::escape($id)); } // if } // getConditionsById // ---------------------------------------------------- // Caching // ---------------------------------------------------- /** * Get specific item from cache * * @access public * @param mixed $id Item ID * @return DataObject */ function getCachedItem($id) { // Multicolumn PK if (is_array($id)) { // Lock first cache level $array = $this->cache; // Loop IDs until we reach the end foreach ($id as $id_field) { if (is_array($array) && isset($array[$id_field])) { $array = $array[$id_field]; } // if } // if // If we have valid instance return it if (instance_of($array, 'DataObject')) { return $array; } // if } else { // If we have it in cache return it... if (isset($this->cache[$id]) && instance_of($this->cache[$id], $this->getItemClass())) { return $this->cache[$id]; } // if } // if // Item not cache... return null; } // end func getCacheItem /** * Add this item to cache * * @access public * @param DataObject $item Item that need to be cached * @return boolean */ function cacheItem($item) { // Check item instance... if (!instance_of($item, 'DataObject') || !$item->isLoaded()) { return false; } // if // Get PK column(s) $id = $item->getPkColumns(); // If array them we have item with multiple items... if (is_array($id)) { // First level is cahce $array = $this->cache; // Set counter $iteration = 0; // Loop fields foreach ($id as $id_field) { // Value of this field... $field_value = $item->getColumnValue($id_field); // Increment counter $iteration++; // Last field? Cache object here if ($iteration == count($id)) { $array[$field_value] = $item; // Prepare for next iteration and continue... } else { if (!isset($array[$field_value]) || !is_array($array[$field_value])) { $array[$field_value] = array(); } // if $array =& $array[$field_value]; } // if } // foreach } else { $this->cache[$item->getColumnValue($id)] = $item; } // if // Done... return true; } // end func setCacheItem /** * Clear the item cache * * @access public * @param void * @return void */ function clearCache() { $this->cache = array(); } // end func clearCache // --------------------------------------------------- // Getters and setters // --------------------------------------------------- /** * Get the value of item class * * @access public * @param void * @return string */ function getItemClass() { return $this->item_class; } // end func getItemClass /** * Set value of item class. This function will set the value only when item class is * defined, else it will return FALSE. * * @access public * @param string $value New item class value * @return null */ function setItemClass($value) { $this->item_class = trim($value); } // end func setItemClass /** * Return table name. Options include adding table prefix in front of table name (true by * default) and escaping resulting name, usefull for using in queries (false by default) * * @access public * @param boolean $escape Return escaped table name * @param boolean $with_prefix Include table prefix. This functionality is added when * installer was built so user can set custom table prefix, not default 'pm_' * @return string */ function getTableName($escape = false, $with_prefix = true) { $table_name = $with_prefix ? TABLE_PREFIX . $this->table_name : $this->table_name; return $escape ? DB::escapeField($table_name) : $table_name; } // end func getTableName /** * Set items table * * @access public * @param string $value Table name * @return void */ function setTableName($value) { $this->table_name = trim($value); } // end func setTableName /** * Return value of caching stamp * * @access public * @param void * @return boolean */ function getCaching() { return (boolean) $this->caching; } // end func getCaching /** * Set value of caching property * * @access public * @param boolean $value New caching value * @return void */ function setCaching($value) { $this->caching = (boolean) $value; } // end func setCaching /** * Check if manager is ready to do the job * * @access private * @param void * @return boolean */ function isReady() { return class_exists($this->item_class); } // end func isReady } // end func DataManager?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -