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.
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
- {
- public $username; //corresponds to the fieldname in the table
- public $email;
- public static final $_tablename='users'; //optional table name.
- //returns active record finder instance
- public static function finder()
- {
- return self::getRecordFinder('UserRecord');
- }
- }
- //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.
Located in /Data/ActiveRecord/TActiveRecord.php (line 66)
TComponent | --TActiveRecord
Create a new instance of an active record with given $data. The record can be saved to the database specified by the $connection 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.
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), .. ));
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.
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 one record using only the primary key or composite primary keys. Usage:
Find records using full SQL, returns corresponding record object.
Gets the current Db connection, the connection object is obtained from the TActiveRecordManager if connection is currently null.
Returns the instance of a active record finder for a particular class.
Gets the record manager for this object, the default is to call TActiveRecordManager::getInstance().
Populate the record with data, registers the object as clean.
Saves the current record to the database, insert or update is automatically determined.
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. The condition is taken as part of the method name after "findBy" or "findAllBy".
The following are equivalent:
- $finder->findByName($name)
- $finder->find('Name = ?', $name);
- $finder->findByUsernameAndPassword($name,$pass);
- $finder->findBy_Username_And_Password($name,$pass);
- $finder->find('Username = ? AND Password = ?', $name, $pass);
- $finder->findAllByAge($age);
- $finder->findAll('Age = ?', $age);
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, 14 Jan 2007 21:40:56 -0500 by phpDocumentor 1.3.0RC4