5 # Part of the ScoutLib application support library 6 # Copyright 2016 Edward Almasy and Internet Scout Research Group 7 # http://scout.wisc.edu 16 # ---- PUBLIC INTERFACE -------------------------------------------------- 32 # set up database access values 33 $ClassName = get_class($this);
34 static::SetDatabaseAccessValues($ClassName);
35 $this->ItemIdColumnName = self::$ItemIdColumnNames[$ClassName];
36 $this->ItemNameColumnName = self::$ItemNameColumnNames[$ClassName];
37 $this->ItemTableName = self::$ItemTableNames[$ClassName];
40 $this->
Id = static::GetCanonicalId(
$Id);
42 # load item info from database 44 $this->DB->Query(
"SELECT * FROM `".$this->ItemTableName.
"`" 45 .
" WHERE `".$this->ItemIdColumnName.
"` = " 47 $this->ValueCache = $this->DB->FetchRow();
49 # error out if item not found in database 50 if ($this->ValueCache === FALSE)
52 throw new InvalidArgumentException(
"Attempt to load ".$ClassName
53 .
" with unknown ID (".
$Id.
").");
62 # delete item from database 63 $this->DB->Query(
"DELETE FROM `".$this->ItemTableName.
"`" 64 .
" WHERE `".$this->ItemIdColumnName.
"` = ".intval($this->
Id));
105 $NameColumn = strlen($this->ItemNameColumnName)
106 ? $this->ItemNameColumnName
152 return $this->
UpdateValue(
"LastModifiedBy", $NewValue);
164 # check for NULL ID (usually used to indicate no value set) 170 # if an object was passed in 173 # make sure that the object passed in matches our called 174 # class or something that descends from our called class 175 $CalledClassName = get_called_class();
176 $ObjClassName = get_class(
$Id);
177 if (!is_a(
$Id, $CalledClassName))
180 "Called ".$CalledClassName.
"::ItemExists " 181 .
"on an object of type ".$ObjClassName
182 .
", which is unrelated to ".$CalledClassName);
185 # call the object's ItemExists method 186 # (we want to do this rather than just setting $ClassName 187 # and continuing so that we'll properly handle subclasses 188 # that override ItemExists) 189 return $ObjClassName::ItemExists(
$Id->Id());
192 # set up database access values 193 $ClassName = get_called_class();
194 static::SetDatabaseAccessValues($ClassName);
196 # build database query to check for item 197 $Query =
"SELECT COUNT(*) AS ItemCount" 198 .
" FROM ".self::$ItemTableNames[$ClassName]
199 .
" WHERE ".self::$ItemIdColumnNames[$ClassName].
" = ".intval(
$Id);
201 # check for item and return result to caller 203 $ItemCount =
$DB->Query($Query,
"ItemCount");
204 return ($ItemCount > 0) ? TRUE : FALSE;
208 # ---- PRIVATE INTERFACE ------------------------------------------------- 229 # set up database access values 230 $ClassName = get_called_class();
231 static::SetDatabaseAccessValues($ClassName);
233 # set up query to add item to database 234 $Query =
"INSERT INTO `".self::$ItemTableNames[$ClassName].
"`";
236 # add initial values to query if supplied 241 foreach ($Values as $Column => $Value)
243 $Assignments[] =
"`".$Column.
"` = '".addslashes($Value).
"'";
245 $Query .= implode(
", ", $Assignments);
248 # add item to database 252 # retrieve ID for newly-created item 253 $NewItemId =
$DB->LastInsertId();
256 $NewItem =
new $ClassName($NewItemId);
258 # return new item object to caller 270 if (!isset(self::$ItemIdColumnNames[$ClassName]))
272 self::$ItemIdColumnNames[$ClassName] = $ClassName.
"Id";
273 self::$ItemNameColumnNames[$ClassName] = $ClassName.
"Name";
287 return $this->DB->UpdateValue($this->ItemTableName, $ColumnName, $NewValue,
288 "`".$this->ItemIdColumnName.
"` = ".intval($this->
Id),
305 $Timestamp = strtotime($NewValue);
306 if ($Timestamp === FALSE)
308 throw new Exception(
"Unable to parse incoming date (".$NewValue.
").");
DateLastModified($NewValue=DB_NOVALUE)
Get/set when item was last modified.
static $ItemIdColumnNames
DateCreated($NewValue=DB_NOVALUE)
Get/set when item was created.
Name($NewValue=DB_NOVALUE)
Get/set name of item.
static CreateWithValues($Values)
Create a new item, using specified initial database values.
UpdateValue($ColumnName, $NewValue=DB_NOVALUE)
Convenience function to supply parameters to Database::UpdateValue().
SQL database abstraction object with smart query caching.
CreatedBy($NewValue=DB_NOVALUE)
Get/set ID of user who created the item.
static $ItemNameColumnNames
LastModifiedBy($NewValue=DB_NOVALUE)
Get/set ID of user who last modified the item.
static SetDatabaseAccessValues($ClassName)
Set the database access values (table name, ID column name, name column name) for specified class...
static Pluralize($Word)
Pluralize an English word.
const NO_ITEM
ID value used to indicate no item.
Common base class for persistent items store in database.
__construct($Id)
Constructor, used to load existing items.
const SQL_DATE_FORMAT
Format to feed to date() to get SQL-compatible date/time string.
static ItemExists($Id)
Check whether an item exists with the specified ID.
UpdateDateValue($ColumnName, $NewValue=DB_NOVALUE)
Convenience function to supply parameters to Database::UpdateValue(), with preprocessing of new value...
static GetCanonicalId($Id)
Normalize item ID to canonical form.