📄 dataobject.class.php
字号:
$sql = $this->getUpdateQuery(); // Nothing to update... if (is_null($sql)) { return true; } // if // Save... if (!DB::execute($sql)) { return false; } // if $this->setLoaded(true); // Done! return true; } // if } // doSave /** * Delete object row from database * * @access public * @param void * @return boolean * @throws DBQueryError */ private function doDelete() { return DB::execute("DELETE FROM " . $this->getTableName(true) . " WHERE " . $this->manager()->getConditionsById( $this->getInitialPkValue() )); } // doDelete /** * Prepare insert query * * @access private * @param void * @return string */ private function getInsertQuery() { // Prepare data $columns = array(); $values = array(); // Loop fields foreach ($this->getColumns() as $column) { // If this field autoincrement? $auto_increment = $this->isAutoIncrementColumn($column); // If not add it... if (!$auto_increment || $this->isColumnModified($column)) { // Add field... $columns[] = DB::escapeField($column); $values[] = DB::escape($this->phpToRaw($this->getColumnValue($column), $this->getColumnType($column))); // Switch type... //switch ($this->getColumnType($column)) { // case DATA_TYPE_BOOLEAN: // $key_value = $this->getColumnValue($column) ? 1 : 0; // break; // default: // $key_value = $this->getColumnValue($column); //} // switch // Add value... //$values[] = DB::escape($key_value); } // if } // foreach // And put it all together return sprintf("INSERT INTO %s (%s) VALUES (%s)", $this->getTableName(true), implode(', ', $columns), implode(', ', $values) ); // sprintf } // getInsertQuery /** * Prepare update query * * @access private * @param void * @return string */ private function getUpdateQuery() { // Prepare data... $columns = array(); // Check number of modified fields if (!$this->isObjectModified()) return null; // Loop fields foreach ($this->getColumns() as $column) { // Is this field modified? if ($this->isColumnModified($column)) { $columns[] = sprintf('%s = %s', DB::escapeField($column), DB::escape($this->phpToRaw($this->getColumnValue($column), $this->getColumnType($column)))); } // if } // foreach // Prepare update SQL return sprintf("UPDATE %s SET %s WHERE %s", $this->getTableName(true), implode(', ', $columns), $this->manager()->getConditionsById( $this->getInitialPkValue() )); } // getUpdateQuery /** * Return field type value * * @access private * @param string $field Field name * @return string */ function getColumnValueType($field) { return isset($this->__fields[$field]['type']) ? $this->__fields[$field]['type'] : DATA_TYPE_NONE; } // getColumnValueType /** * Convert raw value from database to PHP value * * @access public * @param mixed $value * @param string $type * @return mixed */ function rawToPHP($value, $type = DATA_TYPE_STRING) { // NULL! if (is_null($value)) { return null; } // if // Switch type... switch ($type) { // String case DATA_TYPE_STRING: return strval($value); // Integer case DATA_TYPE_INTEGER: return intval($value); // Float case DATA_TYPE_FLOAT: return floatval($value); // Boolean case DATA_TYPE_BOOLEAN: return (boolean) $value; // Date and time case DATA_TYPE_DATETIME: case DATA_TYPE_DATE: case DATA_TYPE_TIME: if ($value instanceof DateTimeValue) { return $value; } else { if ($value == EMPTY_DATETIME) { return null; } // if return DateTimeValueLib::makeFromString($value); } // if } // switch } // rawToPHP /** * Convert PHP value to value for database * * @access public * @param mixed $value * @param string $type * @return string */ function phpToRaw($value, $type = DATA_TYPE_STRING) { // Switch type... switch ($type) { // String case DATA_TYPE_STRING: return strval($value); // Integer case DATA_TYPE_INTEGER: return intval($value); // Float case DATA_TYPE_FLOAT: return floatval($value); // Boolean case DATA_TYPE_BOOLEAN: return (boolean) $value ? 1 : 0; // Date and time case DATA_TYPE_DATETIME: case DATA_TYPE_DATE: case DATA_TYPE_TIME: if (empty($value)) { return EMPTY_DATETIME; } // if if ($value instanceof DateTimeValue) { return $value->toMySQL(); } elseif (is_numeric($value)) { return date(DATE_MYSQL, $value); } else { return EMPTY_DATETIME; } // if } // switch } // phpToRaw // --------------------------------------------------------------- // Flags // --------------------------------------------------------------- /** * Return value of $is_new variable * * @access public * @param void * @return boolean */ function isNew() { return (boolean) $this->is_new; } // isNew /** * Set new stamp value * * @access public * @param boolean $value New value * @return void */ function setNew($value) { $this->is_new = (boolean) $value; } // setNew /** * Returns true if this object has modified columns * * @access public * @param void * @return boolean */ function isModified() { return is_array($this->modified_columns) && (boolean) count($this->modified_columns); } // isModified /** * Return value of $is_deleted variable * * @access public * @param void * @return boolean */ function isDeleted() { return (boolean) $this->is_deleted; } // isDeleted /** * Set deleted stamp value * * @access public * @param boolean $value New value * @return void */ function setDeleted($value) { $this->is_deleted = (boolean) $value; } // setDeleted /** * Return value of $is_loaded variable * * @access public * @param void * @return boolean */ function isLoaded() { return (boolean) $this->is_loaded; } // isLoaded /** * Set loaded stamp value * * @access public * @param boolean $value New value * @return void */ function setLoaded($value) { $this->is_loaded = (boolean) $value; $this->setNew(!$this->is_loaded); //$this->is_new = !$this->is_loaded; //if ($this->is_loaded) $this->setNew(false); } // setLoaded /** * Check if this object is modified (one or more column value are modified) * * @access public * @param void * @return boolean */ function isObjectModified() { return (boolean) count($this->modified_columns); } // isObjectModified /** * Check if specific column is modified * * @access public * @param string $column_name Column name * @return boolean */ function isColumnModified($column_name) { return in_array($column_name, $this->modified_columns); } // isColumnModified /** * Report modified column * * @access public * @param string $column_name * @return null */ protected function addModifiedColumn($column_name) { if (!in_array($column_name, $this->modified_columns)) { $this->modified_columns[] = $column_name; } // if } // addModifiedColumn /** * Returns true if PK column value is updated * * @access public * @param void * @return boolean */ function isPkUpdated() { return count($this->updated_pks); } // isPkUpdated /** * Reset modification idicators. Usefull when you use setXXX functions * but you don't want to modify anything (just loading data from database * in fresh object using setColumnValue function) * * @access public * @param void * @return void */ function notModified() { $this->modified_columns = array(); $this->updated_pks = array(); } // notModified /** * Returns an array of protected attributes * * @param void * @return array */ function getProtectedAttributes() { return $this->attr_protected; } // getProtectedAttributes /** * Add one or multiple protected attributes * * @param void * @return null */ function addProtectedAttribute() { $args = func_get_args(); if (is_array($args)) { foreach ($args as $arg) { if (!in_array($arg, $this->attr_protected)) { if ($this->columnExists($arg)) { $this->attr_protected[] = $arg; } // if } // if } // foreach } // if } // addProtectedAttribute /** * Return an array of acceptable attributes * * @param void * @return array */ function getAcceptableAttributes() { return $this->attr_acceptable; } // getAcceptAttributes /** * Add one or many acceptable attributes * * @param void * @return null */ function addAcceptableAttribute() { $args = func_get_args(); if (is_array($args)) { foreach ($args as $arg) { if (!in_array($arg, $this->attr_acceptable)) { if ($this->columnExists($arg)) { $this->attr_acceptable[] = $arg; } // if } // if } // foreach } // if } // addAcceptableAttribute // --------------------------------------------------------------- // Validators // --------------------------------------------------------------- /** * Validates presence of specific field. Presence of value is determined * by the empty function * * @access public * @param string $field Field name * @param boolean $trim_string If value is string trim it before checks to avoid * returning true for strings like ' '. * @return boolean */ function validatePresenceOf($field, $trim_string = true) { $value = $this->getColumnValue($field); if (is_string($value) && $trim_string) { $value = trim($value); } // if return !empty($value); } // validatePresenceOf /** * This validator will return true if $value is unique (there is no row with such value in that field) * * @access public * @param string $field Filed name * @param mixed $value Value that need to be checked * @return boolean */ function validateUniquenessOf() { // Don't do COUNT(*) if we have one PK column $escaped_pk = is_array($pk_columns = $this->getPkColumns()) ? '*' : DB::escapeField($pk_columns); // Get columns $columns = func_get_args(); if (!is_array($columns) || count($columns) < 1) { return true; } // if // Check if we have existsing columns foreach ($columns as $column) { if (!$this->columnExists($column)) { return false; } // if } // foreach // Get where parets $where_parts = array(); foreach ($columns as $column) { $where_parts[] = DB::escapeField($column) . ' = ' . DB::escape($this->getColumnValue($column)); } // if // If we have new object we need to test if there is any other object // with this value. Else we need to check if there is any other EXCEPT // this one with that value if ($this->isNew()) { $sql = sprintf("SELECT COUNT($escaped_pk) AS 'row_count' FROM %s WHERE %s", $this->getTableName(true), implode(' AND ', $where_parts)); } else { // Prepare PKs part... $pks = $this->getPkColumns(); $pk_values = array(); if (is_array($pks)) { foreach ($pks as $pk) { $pk_values[] = sprintf('%s <> %s', DB::escapeField($pk), DB::escape($this->getColumnValue($pk))); } // foreach } else { $pk_values[] = sprintf('%s <> %s', DB::escapeField($pks), DB::escape($this->getColumnValue($pks))); } // if // Prepare SQL $sql = sprintf("SELECT COUNT($escaped_pk) AS 'row_count' FROM %s WHERE (%s) AND (%s)", $this->getTableName(true), implode(' AND ', $where_parts), implode(' AND ', $pk_values)); } // if $row = DB::executeOne($sql); return array_var($row, 'row_count', 0) < 1; } // validateUniquenessOf /** * Validate max value of specific field. If that field is string time * max lenght will be validated * * @access public * @param string $column * @param integer $max Maximal value * @return null */ function validateMaxValueOf($column, $max) { // Field does not exists if (!$this->columnExists($column)) { return false; } // if // Get value... $value = $this->getColumnValue($column); // Integer and float... if (is_int($value) || is_float($column)) { return $column <= $max; // String... } elseif (is_string($value)) { return strlen($value) <= $max; // Any other value... } else { return $column <= $max; } // if } // validateMaxValueOf /** * Valicate minimal value of specific field. If string minimal lenght is checked * * @access public * @param string $column * @param integer $min Minimal value * @return boolean */ function validateMinValueOf($column, $min) { // Field does not exists if (!$this->columnExists($column)) { return false; } // if // Get value... $value = $this->getColumnValue($column); // Integer and float... if (is_int($value) || is_float($value)) { return $column >= $min; // String... } elseif (is_string($value)) { return strlen($value) >= $min; // Any other value... } else { return $column >= $min; } // if } // validateMinValueOf /** * This function will validate format of specified columns value * * @access public * @param string $column Column name * @param string $pattern * @return boolean */ function validateFormatOf($column, $pattern) { if (!$this->columnExists($column)) { return false; } // if $value = $this->getColumnValue($column); return preg_match($pattern, $value); } // validateFormatOf } // end class DataObject?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -