📄 datamanager.class.php
字号:
<?php /** * Data manager class * * This class implements methods for managing data objects, database rows etc. One * of its features is automatinc caching of loaded data. * * @package System * @version 1.0 * @http://www.projectpier.org/ * */ abstract class DataManager { /** * Database table where items are saved * * @var string */ private $table_name; /** * Item cache array * * @var array */ private $cache = array(); /** * Class of items that this manager is handling * * @var string */ private $item_class = ''; /** * Cache items * * @var boolean */ private $caching = true; /** * Construct and set item class * * @access public * @param string $item_class Value of class of items that this manager is handling * @param string $table Table where data is stored * @param boolean $caching Caching stamp value * @return DataManager */ function __construct($item_class, $table_name, $caching = true) { $this->setItemClass($item_class); $this->setTableName($table_name); $this->setCaching($caching); } // end func __construct // --------------------------------------------------- // Definition methods // --------------------------------------------------- /** * Return array of object columns * * @access public * @param void * @return array */ abstract function getColumns(); /** * Return column type * * @access public * @param string $column_name * @return string */ abstract function getColumnType($column_name); /** * Return array of PK columns. If only one column is PK returns its name as string * * @access public * @param void * @return array or string */ abstract function getPkColumns(); /** * Return name of first auto_incremenent column if it exists * * @access public * @param void * @return string */ abstract function getAutoIncrementColumn(); /** * Return array of lazy load columns * * @access public * @param void * @return array */ function getLazyLoadColumns() { return array(); } // getLazyLoadColumnss /** * Check if specific column is lazy load column * * @access public * @param string $column_name * @return boolean */ function isLazyLoadColumn($column_name) { return in_array($column_name, $this->getLazyLoadColumns()); } // isLazyLoadColumn /** * Return all columns that are not martked as lazy load * * @access public * @param boolean $escape_column_names * @return array */ function getLoadColumns($escape_column_names = false) { // Prepare $load_columns = array(); // Loop... foreach ($this->getColumns() as $column) { if (!$this->isLazyLoadColumn($column)) { $load_columns[] = $escape_column_names ? DB::escapeField($column) : $column; } // if } // foreach // Done... return $load_columns; } // getLoadColumns // --------------------------------------------------- // Finders // --------------------------------------------------- /** * Do a SELECT query over database with specified arguments * * @access public * @param array $arguments Array of query arguments. Fields: * * - one - select first row * - conditions - additional conditions * - order - order by string * - offset - limit offset, valid only if limit is present * - limit * * @return one or many objects * @throws DBQueryError */ function find($arguments = null) { // Collect attributes... $one = (boolean) array_var($arguments, 'one', false); $conditions = $this->prepareConditions( array_var($arguments, 'conditions', '') ); $order_by = array_var($arguments, 'order', ''); $offset = (integer) array_var($arguments, 'offset', 0); $limit = (integer) array_var($arguments, 'limit', 0); // Prepare query parts $where_string = trim($conditions) == '' ? '' : "WHERE $conditions"; $order_by_string = trim($order_by) == '' ? '' : "ORDER BY $order_by"; $limit_string = $limit > 0 ? "LIMIT $offset, $limit" : ''; // Prepare SQL $sql = "SELECT * FROM " . $this->getTableName(true) . " $where_string $order_by_string $limit_string"; // Run! $rows = DB::executeAll($sql); // Empty? if (!is_array($rows) || (count($rows) < 1)) { return null; } // if // If we have one load it, else loop and load many if ($one) { return $this->loadFromRow($rows[0]); } else { $objects = array(); foreach ($rows as $row) { $object = $this->loadFromRow($row); if (instance_of($object, $this->getItemClass())) { $objects[] = $object; } // if } // foreach return count($objects) ? $objects : null; } // if } // find /** * Find all records * * @access public * @param array $arguments * @return array */ function findAll($arguments = null) { if (!is_array($arguments)) { $arguments = array(); } // if $arguments['one'] = false; return $this->find($arguments); } // findAll /** * Find one specific record * * @access public * @param array $arguments * @return array */ function findOne($arguments = null) { if (!is_array($arguments)) { $arguments = array(); } // if $arguments['one'] = true; return $this->find($arguments); } // findOne /** * Return object by its PK value * * @access public * @param mixed $id * @param boolean $force_reload If value of this variable is true cached value * will be skipped and new data will be loaded from database * @return object */ function findById($id, $force_reload = false) { return $this->load($id, $force_reload); } // findById /** * Return number of rows in this table * * @access public * @param string $conditions Query conditions * @return integer */ function count($conditions = null) { // Don't do COUNT(*) if we have one PK column $escaped_pk = is_array($pk_columns = $this->getPkColumns()) ? '*' : DB::escapeField($pk_columns); $conditions = $this->prepareConditions($conditions); $where_string = trim($conditions) == '' ? '' : "WHERE $conditions"; $row = DB::executeOne("SELECT COUNT($escaped_pk) AS 'row_count' FROM " . $this->getTableName(true) . " $where_string"); return (integer) array_var($row, 'row_count', 0); } // count /** * Delete rows from this table that match specific conditions * * @access public * @param string $conditions Query conditions * @return boolean */ function delete($conditions = null) { $conditions = $this->prepareConditions($conditions); $where_string = trim($conditions) == '' ? '' : "WHERE $conditions"; return DB::execute("DELETE FROM " . $this->getTableName(true) . " $where_string"); } // delete /** * This function will return paginated result. Result is array where first element is * array of returned object and second populated pagination object that can be used for * obtaining and rendering pagination data using various helpers. * * Items and pagination array vars are indexed with 0 for items and 1 for pagination * because you can't use associative indexing with list() construct * * @access public * @param array $arguments Query argumens (@see find()) Limit and offset are ignored! * @param integer $items_per_page Number of items per page * @param integer $current_page Current page number * @return array */ function paginate($arguments = null, $items_per_page = 10, $current_page = 1) { if (!is_array($arguments)) { $arguments = array(); } // if $conditions = array_var($arguments, 'conditions'); $pagination = new DataPagination($this->count($conditions), $items_per_page, $current_page); $arguments['offset'] = $pagination->getLimitStart(); $arguments['limit'] = $pagination->getItemsPerPage(); $items = $this->findAll($arguments); return array($items, $pagination); } // paginate /** * Get conditions as argument and return them in the string (if array walk through and escape values) * * @param mixed $conditions * @return string */ function prepareConditions($conditions) { if (is_array($conditions)) { $conditions_sql = array_shift($conditions); $conditions_arguments = count($conditions) ? $conditions : null; return DB::prepareString($conditions_sql, $conditions_arguments); } // if return $conditions; } // prepareConditions /** * Load specific item. If we can't load data return NULL, else return item object * * @access public * @param mixed $id Item ID * @param boolean $force_reload If this value is true cached value (if set) will be skipped * and object data will be loaded from database * @return DataObject */ function load($id, $force_reload = false) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -