PHP Класс DatabaseObject

Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$m_columnNames array Redefine this in the subclass.
$m_data array An array of (ColumnName => Value).
$m_dbTableName string Redefine this in the subclass.
$m_exists boolean TRUE if the object exists in the database, FALSE if not, NULL if unknown.
$m_keyColumnNames array Redefine this in the subclass.
$m_keyIsAutoIncrement boolean Whether or not the primary key is an auto-increment field.
$m_oldKeyValues array If the key values of an object are changed, we need to remember the old values in order to change to the new values. This is the array of the old values.

Защищенные свойства (Protected)

Свойство Тип Описание
$eventDispatcher Newscoop\EventDispatcher\EventDispatcher
$resourceNames array

Открытые методы

Метод Описание
DatabaseObject ( array $p_columnNames = null ) DatabaseObject represents a row in a database table.
GetUseCache ( ) : boolean Returns true if cache use was enabled
ProcessOptions ( string $p_queryStr, array $p_sqlOptions ) : string This is used by subclasses to add extra SQL options to the end of a query.
Search ( $p_className, array $p_columns = null, array $p_sqlOptions = null ) : array Do a simple search.
SetUseCache ( boolean $p_useCache ) : void Sets cache enabled/disabled
commit ( array $p_ignoreColumns = null ) : boolean Commit the data stored in memory to the database.
create ( array $p_values = null ) : boolean Create the record in the database for this object.
delete ( ) : boolean Delete the row from the database.
dumpToHtml ( ) : void Output the raw values of this object so that it displays nice in HTML.
duplicateObject ( object $p_source ) : object Copies the given object
exists ( ) : boolean Return true if the object exists in the database.
fetch ( array $p_recordSet = null, boolean $p_forceExists = false ) : boolean Fetch a single record from the database for the given key.
getCacheKey ( $p_recordSet = null ) Generates the cache key for the object.
getColumnNames ( boolean $p_withTablePrefix = false ) : array Return the column names of this table.
getData ( ) : array Return the row as an array indexed by the column names.
getDbTableName ( ) : string Return the name of the database table.
getKey ( ) : array Return the key as an array indexed by column names.
getKeyColumnNames ( ) : array Return the column names used for the primary key.
getKeyWhereClause ( ) : string Return a string for the primary key of the table.
getProperty ( string $p_dbColumnName, boolean $p_forceFetchFromDatabase = false ) : mixed Return the data stored in the given column.
keyValuesExist ( $p_recordSet = null ) : boolean Return true if the object has all the values required to fetch a unique record from the table.
modifyKeyValue ( string $p_columnName, string $p_value ) : void Remember the old value of the key.
readFromCache ( array $p_recordSet = null ) : mixed Initializes the current object from cache if it exists
resetCache ( )
sameAs ( mix $p_otherObject ) : boolean Returns true if the current object is the same type as the given object then has the same value.
setColumnNames ( array $p_columnNames ) : void Initialize the column names for this object.
setEventDispatcher ( Newscoop\EventDispatcher\EventDispatcher $dispatcher ) : void Set event dispatcher.
setKey ( array $p_columnNames ) : void This function has two modes of operation: You can change the columns used for the key values, or you can change the key values themselves.
setProperty ( string $p_dbColumnName, string $p_value, boolean $p_commit = true, boolean $p_isSql = false ) : boolean Set the given column name to the given value.
setResourceNames ( array $names ) : void Set resource names.
update ( array $p_columns = null, boolean $p_commit = true, boolean $p_isSql = false ) : boolean Update the database row with the given values.
writeCache ( ) : boolean Writes the object to cache.

Защищенные методы

Метод Описание
dispatchEvent ( string $event, string $subject, array $params = [] ) Dispatch event.
getResourceName ( ) : string Get resource name.
lockTables ( array $p_tables = [], $p_write = true ) fn getCacheKey
unlockTables ( )

Описание методов

DatabaseObject() публичный Метод

This class is meant to be subclassed in order to implement a specific table in the database.
public DatabaseObject ( array $p_columnNames = null )
$p_columnNames array The column names of this table. These are optional.

GetUseCache() публичный Метод

Returns true if cache use was enabled
public GetUseCache ( ) : boolean
Результат boolean

ProcessOptions() публичный статический Метод

This is used by subclasses to add extra SQL options to the end of a query.
public static ProcessOptions ( string $p_queryStr, array $p_sqlOptions ) : string
$p_queryStr string The current SQL query.
$p_sqlOptions array
Available options:
'LIMIT' => [max_rows_to_fetch]
'LIMIT' => array('START'=>[Starting_index],'MAX_ROWS'=>[Max_rows_to_fetch]
'ORDER BY' => [column_name]
'ORDER BY' => array([column_name_1], [column_name_2], ...)
'ORDER BY' => array([column_name_1]=>[ASC|DESC], [column_name_2]=>[ASC|DESC], ...)
   
Результат string Original SQL query with the options appended at the end.

SetUseCache() публичный Метод

Sets cache enabled/disabled
public SetUseCache ( boolean $p_useCache ) : void
$p_useCache boolean
Результат void

commit() публичный Метод

This is useful if you make a bunch of setProperty() calls at once and you dont want to update the database every time. Instead you can set all the variables without committing them, then call this function.
public commit ( array $p_ignoreColumns = null ) : boolean
$p_ignoreColumns array Specify column names to ignore when doing the commit.
Результат boolean Return TRUE if the database was updated, false otherwise.

create() публичный Метод

This will use the currently set index values for the key. No other values are set unless they are passed in through the $p_values parameter.
public create ( array $p_values = null ) : boolean
$p_values array Extra values to be set at create time, in the form of: (DB Column Name) => (value)
Результат boolean TRUE if the record was added, false if not.

delete() публичный Метод

Delete the row from the database.
public delete ( ) : boolean
Результат boolean TRUE if the record was deleted, false if not.

dispatchEvent() защищенный статический Метод

Dispatch event.
protected static dispatchEvent ( string $event, string $subject, array $params = [] )
$event string
$subject string
$params array

dumpToHtml() публичный Метод

Output the raw values of this object so that it displays nice in HTML.
public dumpToHtml ( ) : void
Результат void

duplicateObject() публичный Метод

Copies the given object
public duplicateObject ( object $p_source ) : object
$p_source object
Результат object

exists() публичный Метод

Return true if the object exists in the database.
public exists ( ) : boolean
Результат boolean

fetch() публичный Метод

Fetch a single record from the database for the given key.
public fetch ( array $p_recordSet = null, boolean $p_forceExists = false ) : boolean
$p_recordSet array If the record has already been fetched and we just need to assign the data to the object's internal member variable.
$p_forceExists boolean prevents to check for the object existence in the database, a performance thing for situations where we know it exists.
Результат boolean TRUE on success, FALSE on failure

getCacheKey() публичный Метод

Generates the cache key for the object.
public getCacheKey ( $p_recordSet = null )

getColumnNames() публичный Метод

Return the column names of this table.
public getColumnNames ( boolean $p_withTablePrefix = false ) : array
$p_withTablePrefix boolean Set to true if you want to prefix the column names with the table name. Default is false.
Результат array

getData() публичный Метод

Return the row as an array indexed by the column names.
public getData ( ) : array
Результат array

getDbTableName() публичный Метод

Return the name of the database table.
public getDbTableName ( ) : string
Результат string

getKey() публичный Метод

Return the key as an array indexed by column names.
public getKey ( ) : array
Результат array

getKeyColumnNames() публичный Метод

Return the column names used for the primary key.
public getKeyColumnNames ( ) : array
Результат array

getKeyWhereClause() публичный Метод

Return a string for the primary key of the table.
public getKeyWhereClause ( ) : string
Результат string

getProperty() публичный Метод

Return the data stored in the given column.
public getProperty ( string $p_dbColumnName, boolean $p_forceFetchFromDatabase = false ) : mixed
$p_dbColumnName string
$p_forceFetchFromDatabase boolean Get the data from the database instead of cached value that is stored in the object.
Результат mixed Return a string if the property exists, NULL if the value doesnt exist, or a PEAR_Error if $p_forceFetchFromDatabase is TRUE and there was a problem fetching the data.

getResourceName() защищенный Метод

Get resource name.
protected getResourceName ( ) : string
Результат string

keyValuesExist() публичный Метод

Return true if the object has all the values required to fetch a unique record from the table.
public keyValuesExist ( $p_recordSet = null ) : boolean
Результат boolean

lockTables() защищенный Метод

fn getCacheKey
protected lockTables ( array $p_tables = [], $p_write = true )
$p_tables array

modifyKeyValue() публичный Метод

Remember the old value of the key.
public modifyKeyValue ( string $p_columnName, string $p_value ) : void
$p_columnName string
$p_value string
Результат void

readFromCache() публичный Метод

Initializes the current object from cache if it exists
public readFromCache ( array $p_recordSet = null ) : mixed
$p_recordSet array
Результат mixed object The cached object on success boolean FALSE if the object did not exist

resetCache() публичный Метод

public resetCache ( )

sameAs() публичный Метод

Returns true if the current object is the same type as the given object then has the same value.
public sameAs ( mix $p_otherObject ) : boolean
$p_otherObject mix
Результат boolean

setColumnNames() публичный Метод

All column values will be initialized to null.
public setColumnNames ( array $p_columnNames ) : void
$p_columnNames array The column names in the database.
Результат void

setEventDispatcher() публичный статический Метод

Set event dispatcher.
public static setEventDispatcher ( Newscoop\EventDispatcher\EventDispatcher $dispatcher ) : void
$dispatcher Newscoop\EventDispatcher\EventDispatcher
Результат void

setKey() публичный Метод

Changing key values is tricky because you have to remember the old values in order to set the new values.
public setKey ( array $p_columnNames ) : void
$p_columnNames array
Can be either:
[0] => 'column name 1', [1] => 'column name 2', ...
or:
['column name 1'] => 'value', ['column name 2'] => 'value',...
   
Результат void

setProperty() публичный Метод

The object's internal variable will also be updated. If the value hasnt changed, the database will not be updated. Note: You cannot set $p_commit to FALSE and $p_isSql to TRUE at the same time.
public setProperty ( string $p_dbColumnName, string $p_value, boolean $p_commit = true, boolean $p_isSql = false ) : boolean
$p_dbColumnName string The name of the column that is to be updated.
$p_value string The value to set.
$p_commit boolean If set to true, the value will be written to the database immediately. If set to false, the value will not be written to the database. Default is true.
$p_isSql boolean Set this to TRUE if p_value consists of SQL commands. There is no way to know what the result of the command is, so we will need to refetch the value from the database in order to update the internal variable's value.
Результат boolean TRUE on success, FALSE on error.

setResourceNames() публичный статический Метод

Set resource names.
public static setResourceNames ( array $names ) : void
$names array
Результат void

unlockTables() защищенный Метод

protected unlockTables ( )

update() публичный Метод

Update the database row with the given values.
public update ( array $p_columns = null, boolean $p_commit = true, boolean $p_isSql = false ) : boolean
$p_columns array Array of (Column_Name => Value)
$p_commit boolean If set to true, the value will be written to the database immediately. If set to false, the value will not be written to the database. Default is true.
$p_isSql boolean Set this to TRUE if the values of p_columns contains SQL commands. There is no way to know what the result of the command is, so we will need to refetch the row from the database in order to update the internal variable's value.
Результат boolean Return TRUE on success, FALSE on error.

writeCache() публичный Метод

Writes the object to cache.
public writeCache ( ) : boolean
Результат boolean TRUE on success, FALSE on failure

Описание свойств

$eventDispatcher защищенное статическое свойство

protected static EventDispatcher,Newscoop\EventDispatcher $eventDispatcher
Результат Newscoop\EventDispatcher\EventDispatcher

$m_columnNames публичное свойство

Redefine this in the subclass.
public array $m_columnNames
Результат array

$m_data публичное свойство

An array of (ColumnName => Value).
public array $m_data
Результат array

$m_dbTableName публичное свойство

Redefine this in the subclass.
public string $m_dbTableName
Результат string

$m_exists публичное свойство

TRUE if the object exists in the database, FALSE if not, NULL if unknown.
public bool $m_exists
Результат boolean

$m_keyColumnNames публичное свойство

Redefine this in the subclass.
public array $m_keyColumnNames
Результат array

$m_keyIsAutoIncrement публичное свойство

Whether or not the primary key is an auto-increment field.
public bool $m_keyIsAutoIncrement
Результат boolean

$m_oldKeyValues публичное свойство

If the key values of an object are changed, we need to remember the old values in order to change to the new values. This is the array of the old values.
public array $m_oldKeyValues
Результат array

$resourceNames защищенное статическое свойство

protected static array $resourceNames
Результат array