Base class for active records.
An active record creates an object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.
Active record objects are stateful, this is main difference between the TActiveRecord implementation and the TTableGateway implementation.
The essence of an Active Record is an object model of the domain (e.g. products, items) that incorporates both behavior and data in which the classes match very closely the record structure of an underlying database. Each Active Record is responsible for saving and loading to the database and also for any domain logic that acts on the data.
The Active Record provides methods that do the following:
- class UserRecord extends TActiveRecord
- {
- const TABLE='users'; //optional table name.
- public $username; //corresponds to the fieldname in the table
- public $email;
- //returns active record finder instance
- public static function finder($className=__CLASS__)
- {
- return parent::finder($className);
- }
- }
- //create a connection and give it to the ActiveRecord manager.
- $dsn = 'pgsql:host=localhost;dbname=test';
- $conn = new TDbConnection($dsn, 'dbuser','dbpass');
- TActiveRecordManager::getInstance()->setDbConnection($conn);
- //load the user record with username (primary key) 'admin'.
- $user = UserRecord::finder()->findByPk('admin');
- $user->email = 'admin@example.org';
- $user->save(); //update the 'admin' record.
Since v3.1.1, TActiveRecord starts to support column mapping. The physical column names (defined in database) can be mapped to logical column names (defined in active classes as public properties.) To use this feature, declare a static class variable COLUMN_MAPPING like the following:
In the above, the 'users' table consists of 'user_id' and 'email_address' columns, while the UserRecord class declares 'username' and 'email' properties. By using column mapping, we can regularize the naming convention of column names in active record.
- class UserRecord extends TActiveRecord
- {
- const TABLE='users';
- public static $COLUMN_MAPPING=array
- (
- 'user_id'=>'username',
- 'email_address'=>'email',
- );
- public $username;
- pulbic $email;
- }
Located in /Data/ActiveRecord/TActiveRecord.php (line 95)
TComponent | --TActiveRecord
This static variable defines the column mapping.
The keys are physical column names as defined in database, and the values are logical column names as defined as public variable/property names for the corresponding active record class.
This static variable defines the relationships.
The keys are public variable/property names defined in the AR class. Each value is an array, e.g. array(self::HAS_MANY, 'PlayerRecord').
Create a new instance of an active record with given $data. The record can be saved to the database specified by the $connection object.
Commit changes to the record, may insert, update or delete depending on the record state given in TObjectStateRegistery.
Copies data from an array or another object.
Find the number of records.
Deletes the current record from the database. Once deleted, this object can not be saved again in the same instance.
Delete multiple records using a criteria.
Alias for deleteByPk()
Delete records by primary key. Usage:
- $finder->deleteByPk($primaryKey); //delete 1 record
- $finder->deleteByPk($key1,$key2,...); //delete multiple records
- $finder->deleteByPk(array($key1,$key2,...)); //delete multiple records
For composite primary keys (determined from the table definitions):
- $finder->deleteByPk(array($key1,$key2)); //delete 1 record
- //delete multiple records
- $finder->deleteByPk(array($key1,$key2), array($key3,$key4),...);
- //delete multiple records
- $finder->deleteByPk(array( array($key1,$key2), array($key3,$key4), .. ));
Compare two records using their primary key values (all column values if table does not defined primary keys). The default uses simple == for comparison of their values. Set $strict=true for identity comparison.
Find one single record that matches the criteria.
Usage:
- $finder->find('username = :name AND password = :pass',
- array(':name'=>$name, ':pass'=>$pass));
- $finder->find('username = ? AND password = ?', array($name, $pass));
- $finder->find('username = ? AND password = ?', $name, $pass);
- //$criteria is of TActiveRecordCriteria
- $finder->find($criteria); //the 2nd parameter for find() is ignored.
Same as find() but returns an array of objects.
Fetches records using the sql clause "(fields) IN (values)", where fields is an array of column names and values is an array of values that the columns must have.
This method is to be used by the relationship handler.
Find multiple records matching a list of primary or composite keys.
For scalar primary keys:
- $finder->findAllByPk($key1, $key2, ...);
- $finder->findAllByPk(array($key1, $key2, ...));
For composite keys:
- $finder->findAllByPk(array($key1, $key2), array($key3, $key4), ...);
- $finder->findAllByPk(array(array($key1, $key2), array($key3, $key4), ...));
Find records using full SQL, returns corresponding record object.
The names of the column retrieved must be defined in your Active Record class.
Find one record using only the primary key or composite primary keys. Usage:
Find records using full SQL, returns corresponding record object.
The names of the column retrieved must be defined in your Active Record class.
Returns the instance of a active record finder for a particular class.
The finder objects are static instances for each ActiveRecord class. This means that event handlers bound to these finder instances are class wide. Create a new instance of the ActiveRecord class if you wish to bound the event handlers to object instance.
Retrieves the column value according to column name.
This method is used internally.
Create a new TSqlCriteria object from a string $criteria. The $args are additional parameters and are used in place of the $parameters if $parameters is not an array and $args is an arrary.
Gets the current Db connection, the connection object is obtained from the TActiveRecordManager if connection is currently null.
Gets the record manager for this object, the default is to call TActiveRecordManager::getInstance().
Returns the active record relationship handler for $RELATION with key value equal to the $property value.
Raised when a command is prepared and parameter binding is completed.
The parameter object is TDataGatewayEventParameter of which the TDataGatewayEventParameter::getCommand property can be inspected to obtain the sql query to be executed.
Note well that the finder objects obtained from ActiveRecord::finder() method are static objects. This means that the event handlers are bound to a static finder object and not to each distinct active record object.
Raised when a command is executed and the result from the database was returned.
The parameter object is TDataGatewayResultEventParameter of which the TDataGatewayEventParameter::getResult property contains the data return from the database. The data returned can be changed by setting the TDataGatewayEventParameter::setResult property.
Note well that the finder objects obtained from ActiveRecord::finder() method are static objects. This means that the event handlers are bound to a static finder object and not to each distinct active record object.
Populate the record with data, registers the object as clean.
Saves the current record to the database, insert or update is automatically determined.
Sets the column value according to column name.
This method is used internally.
Dynamic find method using parts of method name as search criteria.
Method name starting with "findBy" only returns 1 record. Method name starting with "findAllBy" returns 0 or more records. Method name starting with "deleteBy" deletes records by the trail criteria. The condition is taken as part of the method name after "findBy", "findAllBy" or "deleteBy".
The following are equivalent:
- $finder->findByName($name)
- $finder->find('Name = ?', $name);
- $finder->findByUsernameAndPassword($name,$pass); // OR may be used
- $finder->findBy_Username_And_Password($name,$pass); // _OR_ may be used
- $finder->find('Username = ? AND Password = ?', $name, $pass);
- $finder->findAllByAge($age);
- $finder->findAll('Age = ?', $age);
- $finder->deleteAll('Name = ?', $name);
- $finder->deleteByName($name);
Prevent __call() method creating __sleep() when serializing.
Prevent __call() method creating __wake() when unserializing.
Inherited From TComponent
TComponent::addParsedObject()
TComponent::attachEventHandler()
TComponent::canGetProperty()
TComponent::canSetProperty()
TComponent::createdOnTemplate()
TComponent::detachEventHandler()
TComponent::evaluateExpression()
TComponent::evaluateStatements()
TComponent::getEventHandlers()
TComponent::getSubProperty()
TComponent::hasEvent()
TComponent::hasEventHandler()
TComponent::hasProperty()
TComponent::raiseEvent()
TComponent::setSubProperty()
TComponent::__get()
TComponent::__set()
Documentation generated on Sun, 30 Sep 2007 19:10:17 -0400 by phpDocumentor 1.3.0RC4