PHP Class DatabaseObject

Afficher le fichier Open project: sourcefabric/newscoop Class Usage Examples

Méthodes publiques

Свойство 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

Свойство Type Description
$eventDispatcher Newscoop\EventDispatcher\EventDispatcher
$resourceNames array

Méthodes publiques

Méthode 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.

Méthodes protégées

Méthode 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 méthode

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 méthode

Returns true if cache use was enabled
public GetUseCache ( ) : boolean
Résultat boolean

ProcessOptions() public static méthode

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

SetUseCache() public méthode

Sets cache enabled/disabled
public SetUseCache ( boolean $p_useCache ) : void
$p_useCache boolean
Résultat void

commit() public méthode

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

create() public méthode

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

delete() public méthode

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

dispatchEvent() protected static méthode

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

dumpToHtml() public méthode

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

duplicateObject() public méthode

Copies the given object
public duplicateObject ( object $p_source ) : object
$p_source object
Résultat object

exists() public méthode

Return true if the object exists in the database.
public exists ( ) : boolean
Résultat boolean

fetch() public méthode

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

getCacheKey() public méthode

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

getColumnNames() public méthode

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.
Résultat array

getData() public méthode

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

getDbTableName() public méthode

Return the name of the database table.
public getDbTableName ( ) : string
Résultat string

getKey() public méthode

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

getKeyColumnNames() public méthode

Return the column names used for the primary key.
public getKeyColumnNames ( ) : array
Résultat array

getKeyWhereClause() public méthode

Return a string for the primary key of the table.
public getKeyWhereClause ( ) : string
Résultat string

getProperty() public méthode

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.
Résultat 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 méthode

Get resource name.
protected getResourceName ( ) : string
Résultat string

keyValuesExist() public méthode

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

lockTables() protected méthode

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

modifyKeyValue() public méthode

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

readFromCache() public méthode

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

resetCache() public méthode

public resetCache ( )

sameAs() public méthode

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
Résultat boolean

setColumnNames() public méthode

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

setEventDispatcher() public static méthode

Set event dispatcher.
public static setEventDispatcher ( Newscoop\EventDispatcher\EventDispatcher $dispatcher ) : void
$dispatcher Newscoop\EventDispatcher\EventDispatcher
Résultat void

setKey() public méthode

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',...
   
Résultat void

setProperty() public méthode

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

setResourceNames() public static méthode

Set resource names.
public static setResourceNames ( array $names ) : void
$names array
Résultat void

unlockTables() protected méthode

protected unlockTables ( )

update() public méthode

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

writeCache() public méthode

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

Property Details

$eventDispatcher protected_oe static_oe property

protected static EventDispatcher,Newscoop\EventDispatcher $eventDispatcher
Résultat Newscoop\EventDispatcher\EventDispatcher

$m_columnNames public_oe property

Redefine this in the subclass.
public array $m_columnNames
Résultat array

$m_data public_oe property

An array of (ColumnName => Value).
public array $m_data
Résultat array

$m_dbTableName public_oe property

Redefine this in the subclass.
public string $m_dbTableName
Résultat string

$m_exists public_oe property

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

$m_keyColumnNames public_oe property

Redefine this in the subclass.
public array $m_keyColumnNames
Résultat array

$m_keyIsAutoIncrement public_oe property

Whether or not the primary key is an auto-increment field.
public bool $m_keyIsAutoIncrement
Résultat 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
Résultat array

$resourceNames protected_oe static_oe property

protected static array $resourceNames
Résultat array