Class CRUD

Description

Implements interfaces:

  • Iterator (internal interface)

Database abstraction layer implementing CRUD procedures

Located in /class.crud.php (line 23)


                
			
Direct descendents
Class Description
Model The model class of the MVC architecture. Extends the CRUD database abstraction layer to enhance it. This class should not be used directly. It should be extended with methods specific to the database table it is interacting with.
Variable Summary
mixed $aData
mixed $oDatabase
mixed $oDataSet
mixed $sTableName
Method Summary
void __construct (string $sTableName, [mixed $oData = null])
string AsJSON ([bool $bIncludeReferences = false])
SimpleXMLElement AsXML ([ $bIncludeReferences = false], [bool $bProvideAll = false], bool $bIncludereferences)
string AsXMLString ([bool $bIncludeReferences = false], [bool $bProvideAll = false])
string buildWhereClause (array $keys, object $dataSet)
void Cleanup ()
integer Count ()
void Destroy ()
boolean DestroyDataByColumnValue (string $sColumn, string $sValue)
CRUD Find ([mixed $xId = null], [array $aClauses = array()])
int FindCount ([array $aClauses = array()])
array FindRelationship (string $sName)
array FindRelationship2 ( $sPrimaryTable, string $sRelatedTable, string $sThroughColumn, string $sPrimaryTableName)
array GetAll ()
CRUD GetDataByColumnValue (string $sColumn, string $sValue, [string $sOrder = ""])
StdClass GetRecord ()
string GetTableName ()
bool Insert ()
boolean IsEmpty ()
mixed Key ()
void Load (mixed $oRecord)
void Next ()
void PrepareColumns ()
bool RecordExists ()
void Rewind ()
boolean Save ()
bool SaveAll ()
boolean Update ()
string UpdateQuery ()
bool Valid ()
mixed __call (string $sName, array $aArguments)
void __clone ()
mixed __get (string $sName)
bool __isset (string $sName)
void __set (string $sName, string $sValue)
string __toString ()
void __unset (string $sName)
Variables
mixed $aData = array() (line 43)

The data generated by each query

  • access: protected
mixed $oDatabase = null (line 28)

Stores a database connection object

  • access: protected
mixed $oDataSet = null (line 38)

The data set to store query results

  • access: protected
mixed $sTableName = null (line 33)

The name of the database table

  • access: protected
Methods
Constructor __construct (line 60)

The constructor makes the necessary connection to the database (see Database::Construct) and attempts to load the schema of the specified table.

If the second argument of oData is supplied, the constructor will attempt to load that data into the class for later saving.

If there is a define defined called ENABLE_SCHEMA_CACHING, schema caching is turned on, allowing for faster subsequent page loads.

  • access: public
void __construct (string $sTableName, [mixed $oData = null])
  • string $sTableName: The name of the database table
  • mixed $oData: An array or object of data to load into the CRUD object

Redefined in descendants as:
  • Model::__construct() : The Model's constructor - accepts the name of the table and an optional set of data to load into the parent CRUD object. Invokes CRUD::__construct() and passes these two parameters along.
AsJSON (line 1790)

Returns the data currently stored in the CRUD object as a JSON (JavaScript object notation) string. If bIncludeReferences is true, then each reference to the table is considered and added to the XML document.

  • return: A JSON string representing the CRUD object
  • access: public
string AsJSON ([bool $bIncludeReferences = false])
  • bool $bIncludeReferences: Optional; Toggles whether references/relationships should be stored in the JSON string. Default: false
AsXML (line 1679)

Returns the data currently stored in the CRUD object a well formed XML document as a SimpleXMLElement object. This method requires the SimpleXML extension of PHP to be installed. If the SimpleXML extension is not installed, this method will throw an exception.

  • return: The data requested as a SimpleXMLElement object
  • access: public
SimpleXMLElement AsXML ([ $bIncludeReferences = false], [bool $bProvideAll = false], bool $bIncludereferences)
  • bool $bIncludereferences: Optional; Should this returned XML include references? Default false.
  • bool $bProvideAll: Optional; Should this returned XML include all records returned by the last Find() call? If not, only the current record stored is returned. Default false.
  • $bIncludeReferences
AsXMLString (line 1656)

Returns the data currently stored in the CRUD object a well formed XML document as a string representation. This requires the DOM and SimpleXML extensions of PHP to be installed. If either extension is not installed, this method will throw an exception.

  • return: A well formed XML document as a string representation
  • access: public
string AsXMLString ([bool $bIncludeReferences = false], [bool $bProvideAll = false])
  • bool $bIncludeReferences: Optional; Should this returned XML include references? Default false.
  • bool $bProvideAll: Optional; Should this returned XML include all records returned by the last Find() call? If not, only the current record stored is returned. Default false.
buildWhereClause (line 1475)

Helper method for generating a WHERE clause for a query string. WHERE clause is built by supplied keys and associated data

  • return: The WHERE clause
  • access: protected
string buildWhereClause (array $keys, object $dataSet)
  • array $keys: The keys of the dataset
  • object $dataSet: The dataset containing data for the WHERE clause
Cleanup (line 1606)

Removes all data and related data from the data array to prepare for the next load.

Prevents stale data from persisting when null values are encountered in the column of the next data set.

  • access: protected
void Cleanup ()
Count (line 908)

Gets the number of rows returned by the last Find() call. If Find() has not yet been called, this method will return This method is invoked through the __call() method to allow using the method name Count(), which is a reserved word in PHP.

  • return: The number of results in the data set
  • access: public
integer Count ()
Current (line 1528)

Returns the current object from the DataSet generated from the last call to Find().

This method is part of the PHP Iterator implementation

CRUD Current ()

Implementation of:
Iterator::current
Destroy (line 1440)

Destroys (deletes) the current data. This method will delete the primary record (assuming that the primary key for the data is set).

  • access: public
void Destroy ()

Redefined in descendants as:
  • Model::Destroy() : Wraps CRUD's Destroy() method to invoke the event OnBeforeDestroy() before calling CRUD::Destroy(), and to invoke the event OnAfterDestroy() afterwards. If either event returns false, execution of this method will stop and false will be returned.
DestroyDataByColumnValue (line 675)

This method will delete records from the table based on column value using the supplied column name (which may have had underscores removed and be cased differently) and column value.

This method is invoked through __call() when the user uses the CRUD::destroyBy[column]() "virtual" method.

  • return: True if successful/no error; throws an Exception otherwise
  • throws: Exception, QueryFailedException
  • access: protected
boolean DestroyDataByColumnValue (string $sColumn, string $sValue)
  • string $sColumn: The name of the column we are basing our delete from. This name may underscores removed and be cased differently
  • string $sValue: The value of the column in the first argument that determines which records will be deleted.
Find (line 113)

This method attempts to load a record from the database based on the passed ID, or a passed set of SQL query clauses. This method can be used retrieve one record from the database, or a set of records that can be iterated through.

  • return: returns a reference to itself to allow chaining
  • access: public
CRUD Find ([mixed $xId = null], [array $aClauses = array()])
  • mixed $xId: The ID of the data being found
  • array $aClauses: Additional databases clauses, including: join, where, order, offset and limit. All except for join are string that are directly appended to the query. Join is an array of referenced tables to inner join.
FindCount (line 395)

This method returns the number of records that match a passed set of SQL query clauses.

This method is very similiar to Find(), except that it returns an integer value representing the number of matching records.

  • return: Returns the number of database records that match the passed clauses
  • access: public
int FindCount ([array $aClauses = array()])
  • array $aClauses: Additional databases clauses, including: join and where. Where is a string that are directly appended to the query. Join is an array of referenced tables to inner join.
FindRelationship (line 782)

Attempts to find a relationship to the current database table by the relationship name provided.

  • return: An array containing information about the relationship, if found
  • access: protected
array FindRelationship (string $sName)
  • string $sName: The name of the relationship to find
FindRelationship2 (line 809)

Attempts to find a relationship between two specified tables based on the column they are related through.

  • return: An array containing information about the relationship, if found
  • access: protected
array FindRelationship2 ( $sPrimaryTable, string $sRelatedTable, string $sThroughColumn, string $sPrimaryTableName)
  • string $sPrimaryTableName: The name of the main table of the relationship
  • string $sRelatedTable: The name of the related table of the relationship
  • string $sThroughColumn: The name of the column of the primary table that the the related table is related through.
  • $sPrimaryTable
GetAll (line 759)

Returns an array containing all data stored in the current data set from the last query

  • return: An array of data from the dataset
  • access: public
array GetAll ()
GetDataByColumnValue (line 620)

This method will retrieve records from the table based on column value using the supplied column name (which may have had underscores removed and be cased differently) and column value.

This method is invoked through __call() when the user uses the CRUD::FindBy[column]() "virtual" method.

  • return: A reference to the current object to support chaining or secondary assignment
  • throws: Exception, QueryFailedException
  • access: protected
CRUD GetDataByColumnValue (string $sColumn, string $sValue, [string $sOrder = ""])
  • string $sColumn: The name of the column we are pulling records by. This name may underscores removed and be cased differently
  • string $sValue: The value of the column in the first argument that determines which records will be selected
  • string $sOrder: Optional; The order clause for the query
GetRecord (line 720)

Loops all data stored within this CRUD class and returns a standard class copy of the data as a StdClass.

NOTE: GetRecord() will move the internal pointers of all 1-M iterators loaded

  • return: A pure class copy of the data stored within the CRUD class
  • access: public
StdClass GetRecord ()
GetTableName (line 1637)

Returns the table name associated with this CRUD object

  • return: The name of the table associated with this CRUD object
  • access: public
string GetTableName ()
Insert (line 1236)

Builds an insert query based on the data stored within this CRUD class and executes it against the database.

  • return: True if the operation was successful, false on failure
  • access: protected
bool Insert ()
IsEmpty (line 894)

Determines whether or not there is currently data in the CRUD object. Data is loaded into CRUD through the Find() method, through specifying data into fields manually, or by passing data to the constructor. If any of these cases are met, this method will return true.

  • return: True if there is no data currently in CRUD, false otherwise
  • access: protected
boolean IsEmpty ()
Key (line 1546)

Gets the key of the current element in the dataset

mixed Key ()

Implementation of:
Iterator::key
Load (line 835)

Loads the specified data (either an array or object) into the CRUD object. This array/object to load can contained referenced data (through foreign keys) as either an array or object.

  • access: protected
void Load (mixed $oRecord)
  • mixed $oRecord: The data to load into the CRUD object
Next (line 1558)

Move to the next element in the dataset

void Next ()

Implementation of:
Iterator::next
PrepareColumns (line 89)

Grabs all columns for this table and adds each as a key in the data array for this object

  • access: protected
void PrepareColumns ()
RecordExists (line 1404)

Determines if the data currently stored within this CRUD class already exists in the database based on primary key data. This method is used to determine whether to perform an INSERT or UPDATE operation on a table with compound primary keys

  • return: True if the record exists, false if it does not
  • access: protected
bool RecordExists ()
Rewind (line 1504)

Returns to the first element in the dataset

void Rewind ()

Implementation of:
Iterator::rewind
Save (line 1110)

Based on presence of primary key data, either creates a new record, or updates theexisting record

  • return: True if the save was successful, false otherwise
  • access: public
boolean Save ()

Redefined in descendants as:
  • Model::Save() : Wraps CRUD's Save() method to invoke the events ValidateUpdate(), Validate(), OnBeforeUpdate(), OnBeforeSave(), respectively, before calling CRUD::Save() for an UPDATE query. Likewise, invokes the events ValidateInsert(), Validate(), OnBeforeInsert() and OnBeforeSave(), respectively, before calling CRUD::Save() for an INSERT statement.
SaveAll (line 1161)

SaveAll() is misleading and really means SaveDeep(). This method will attempt to save all of the data stored within this CRUD class, and calls SaveAll() on all dependent CRUD data that is currently loaded into the class.

  • return: True if the operation was successful, false if it failed
  • access: public
bool SaveAll ()

Redefined in descendants as:
  • Model::SaveAll() : This method does the same thing as Save(), but also saves all related data loaded into the CRUD object as well. See CRUD::SaveAll() for more details
Update (line 1320)

Responsible for updating the currently stored data for primary table and all foreign tables referenced

  • return: True if the update was successful, false otherwise
  • access: protected
boolean Update ()
UpdateQuery (line 1340)

Called by the Update() method to generate an update query for this table

  • return: The generated SQL query
  • access: protected
string UpdateQuery ()
Valid (line 1594)

Checks if the iterator is valid

bool Valid ()

Implementation of:
Iterator::valid
__call (line 1057)

Supports several "virtual" or magic methods, such as data manipulation/retrieval through getBy[column_name] and destroyBy[column_name], reserved word methods, such as empty(), and also provides access to public methods of the database, which fakes database class inheritance (which is needed to support multiple database drivers).

  • return: The return value of the method being called
  • throws: Exception
  • access: public
mixed __call (string $sName, array $aArguments)
  • string $sName: The name of the argument to be called magically
  • array $aArguments: An array of arguments to pass to the magically called method
__clone (line 1090)

Assists slightly in object cloning. If this table has a single primary key, the value of this key will be whiped out when cloning.

  • access: public
void __clone ()
__get (line 939)

Retrieves the specified attribute based on its name and presence in the data array

  • return: The value of the attribute if found
  • access: public
mixed __get (string $sName)
  • string $sName: The name of the attribute to retrieve
__isset (line 926)

Determines if the specified attribute is defined in the data array.

  • return: true if the attribute is set, false otherwise
  • access: public
bool __isset (string $sName)
  • string $sName: The name of the attribute we are searching for
__set (line 1008)

Attempts to set the value of a database column, or sets a relationship through the CRUD->[column_name] syntax.

  • throws: Exception
  • access: public
void __set (string $sName, string $sValue)
  • string $sName: The name of the column to set
  • string $sValue: The value to set the column specified in the first argument
__toString (line 1863)

Creates a readable, string representation of the object using print_r and returns that string.

  • return: A readable, string representation of the object
  • access: public
string __toString ()
__unset (line 1036)

Unsets the value of a database column. This will effectively remove the column from the known list of columns for this instance, causing a CRUD::Save() operation to not update the value.

  • access: public
void __unset (string $sName)
  • string $sName: The name of the database column to unset

Documentation generated on Thu, 25 Feb 2010 15:59:39 -0500 by phpDocumentor 1.4.3