PHP Class DatabaseObject

显示文件 Open project: sourcefabric/newscoop Class Usage Examples

Public Properties

Property Type Description
$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 Properties

Property Type Description
$eventDispatcher Newscoop\EventDispatcher\EventDispatcher
$resourceNames array

Public Methods

Method Description
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.

Protected Methods

Method Description
dispatchEvent ( string $event, string $subject, array $params = [] ) Dispatch event.
getResourceName ( ) : string Get resource name.
lockTables ( array $p_tables = [], $p_write = true ) fn getCacheKey
unlockTables ( )

Method Details

DatabaseObject() public method

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() public method

Returns true if cache use was enabled
public GetUseCache ( ) : boolean
return boolean

ProcessOptions() public static method

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], ...)
   
return string Original SQL query with the options appended at the end.

SetUseCache() public method

Sets cache enabled/disabled
public SetUseCache ( boolean $p_useCache ) : void
$p_useCache boolean
return void

commit() public method

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.
return boolean Return TRUE if the database was updated, false otherwise.

create() public method

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)
return boolean TRUE if the record was added, false if not.

delete() public method

Delete the row from the database.
public delete ( ) : boolean
return boolean TRUE if the record was deleted, false if not.

dispatchEvent() protected static method

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

dumpToHtml() public method

Output the raw values of this object so that it displays nice in HTML.
public dumpToHtml ( ) : void
return void

duplicateObject() public method

Copies the given object
public duplicateObject ( object $p_source ) : object
$p_source object
return object

exists() public method

Return true if the object exists in the database.
public exists ( ) : boolean
return boolean

fetch() public method

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.
return boolean TRUE on success, FALSE on failure

getCacheKey() public method

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

getColumnNames() public method

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.
return array

getData() public method

Return the row as an array indexed by the column names.
public getData ( ) : array
return array

getDbTableName() public method

Return the name of the database table.
public getDbTableName ( ) : string
return string

getKey() public method

Return the key as an array indexed by column names.
public getKey ( ) : array
return array

getKeyColumnNames() public method

Return the column names used for the primary key.
public getKeyColumnNames ( ) : array
return array

getKeyWhereClause() public method

Return a string for the primary key of the table.
public getKeyWhereClause ( ) : string
return string

getProperty() public method

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.
return 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() protected method

Get resource name.
protected getResourceName ( ) : string
return string

keyValuesExist() public method

Return true if the object has all the values required to fetch a unique record from the table.
public keyValuesExist ( $p_recordSet = null ) : boolean
return boolean

lockTables() protected method

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

modifyKeyValue() public method

Remember the old value of the key.
public modifyKeyValue ( string $p_columnName, string $p_value ) : void
$p_columnName string
$p_value string
return void

readFromCache() public method

Initializes the current object from cache if it exists
public readFromCache ( array $p_recordSet = null ) : mixed
$p_recordSet array
return mixed object The cached object on success boolean FALSE if the object did not exist

resetCache() public method

public resetCache ( )

sameAs() public method

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
return boolean

setColumnNames() public method

All column values will be initialized to null.
public setColumnNames ( array $p_columnNames ) : void
$p_columnNames array The column names in the database.
return void

setEventDispatcher() public static method

Set event dispatcher.
public static setEventDispatcher ( Newscoop\EventDispatcher\EventDispatcher $dispatcher ) : void
$dispatcher Newscoop\EventDispatcher\EventDispatcher
return void

setKey() public method

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',...
   
return void

setProperty() public method

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.
return boolean TRUE on success, FALSE on error.

setResourceNames() public static method

Set resource names.
public static setResourceNames ( array $names ) : void
$names array
return void

unlockTables() protected method

protected unlockTables ( )

update() public method

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.
return boolean Return TRUE on success, FALSE on error.

writeCache() public method

Writes the object to cache.
public writeCache ( ) : boolean
return boolean TRUE on success, FALSE on failure

Property Details

$eventDispatcher protected_oe static_oe property

protected static EventDispatcher,Newscoop\EventDispatcher $eventDispatcher
return Newscoop\EventDispatcher\EventDispatcher

$m_columnNames public_oe property

Redefine this in the subclass.
public array $m_columnNames
return array

$m_data public_oe property

An array of (ColumnName => Value).
public array $m_data
return array

$m_dbTableName public_oe property

Redefine this in the subclass.
public string $m_dbTableName
return string

$m_exists public_oe property

TRUE if the object exists in the database, FALSE if not, NULL if unknown.
public bool $m_exists
return boolean

$m_keyColumnNames public_oe property

Redefine this in the subclass.
public array $m_keyColumnNames
return array

$m_keyIsAutoIncrement public_oe property

Whether or not the primary key is an auto-increment field.
public bool $m_keyIsAutoIncrement
return boolean

$m_oldKeyValues public_oe property

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
return array

$resourceNames protected_oe static_oe property

protected static array $resourceNames
return array