Source for file Record.php
Documentation is available at Record.php
* $Id: Record.php 2294 2007-08-29 22:20:30Z zYne $
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
* All record classes should inherit this super class
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @version $Revision: 2294 $
* a Doctrine_Record is in dirty state when its properties are changed
* a Doctrine_Record is in transient dirty state when it is created
* and some of its fields are modified but it is NOT yet persisted into database
* a Doctrine_Record is in clean state when all of its properties are loaded from the database
* and none of its properties are changed
* a Doctrine_Record is in proxy state when its properties are not fully loaded
* a Doctrine_Record is in transient clean state when it is created and none of its fields are modified
* a Doctrine_Record is temporarily locked during deletes and saves
* This state is used internally to ensure that circular deletes
* and saves will not cause infinite loops
* @var Doctrine_Node_<TreeImpl> node object
* @var integer $_id the primary keys of this object
protected $_id =
array();
* @var array $_data the record data
* @var array $_values the values array, aggregate values and such are mapped into this array
* @var integer $_state the state of this record
* @var array $_modified an array containing properties that have been modified
* @var Doctrine_Validator_ErrorStack error stack object
* @var Doctrine_Record_Filter the filter object
* @var array $_references an array containing all the references
* @var integer $index this index is used for creating object identifiers
private static $_index =
1;
* @var integer $oid object identifier, each Record object has a unique object identifier
* @param Doctrine_Table|null$table a Doctrine_Table object or null,
* if null the table object is retrieved from current connection
* @param boolean $isNewEntry whether or not this record is transient
* @throws Doctrine_Connection_Exception if object is created using the new operator and there are no
* @throws Doctrine_Record_Exception if the cleanData operation fails somehow
public function __construct($table =
null, $isNewEntry =
false)
$exists =
( ! $isNewEntry);
// get the table of this class
// initialize the filter object
// Check if the current connection has the records table in its registry
// If not this record is only used for creating table definition and setting up
if ($this->_table->getConnection()->hasTable($this->_table->getComponentName())) {
$this->_oid =
self::$_index;
$keys =
$this->_table->getPrimaryKeys();
$count =
count($this->_data);
// set the default values for this record
if ($count <
$this->_table->getColumnCount()) {
$repository =
$this->_table->getRepository();
public static function _index()
* this method is used for setting up relations and attributes
* it should be implemented by child classes
* Empty tempalte method to provide concrete Record classes with the possibility
* to hook into the constructor procedure
* returns the object identifier
* @return boolean whether or not this record passes all column validations
if ( ! $this->_table->getAttribute(Doctrine::ATTR_VLD)) {
// Clear the stack from any previous errors.
// Run validation process
$validator->validateRecord($this);
if ($this->_state ==
self::STATE_TDIRTY ||
$this->_state ==
self::STATE_TCLEAN) {
* Empty template method to provide concrete Record classes with the possibility
* to hook into the validation procedure, doing any custom / specialized
* validations that are neccessary.
* Empty template method to provide concrete Record classes with the possibility
* to hook into the validation procedure only when the record is going to be
* Empty template method to provide concrete Record classes with the possibility
* to hook into the validation procedure only when the record is going to be
* inserted into the data store the first time.
* Empty template method to provide concrete Record classes with the possibility
* to hook into the serializing procedure.
* Empty template method to provide concrete Record classes with the possibility
* to hook into the serializing procedure.
* Empty template method to provide concrete Record classes with the possibility
* to hook into the serializing procedure.
* Empty template method to provide concrete Record classes with the possibility
* to hook into the serializing procedure.
* Empty template method to provide concrete Record classes with the possibility
* to hook into the saving procedure.
* Empty template method to provide concrete Record classes with the possibility
* to hook into the saving procedure.
* Empty template method to provide concrete Record classes with the possibility
* to hook into the deletion procedure.
* Empty template method to provide concrete Record classes with the possibility
* to hook into the deletion procedure.
* Empty template method to provide concrete Record classes with the possibility
* to hook into the saving procedure only when the record is going to be
* Empty template method to provide concrete Record classes with the possibility
* to hook into the saving procedure only when the record is going to be
* Empty template method to provide concrete Record classes with the possibility
* to hook into the saving procedure only when the record is going to be
* inserted into the data store the first time.
* Empty template method to provide concrete Record classes with the possibility
* to hook into the saving procedure only when the record is going to be
* inserted into the data store the first time.
* @return Doctrine_Validator_ErrorStack returns the errorStack associated with this record
* assigns / returns record errorStack
* @param Doctrine_Validator_ErrorStack errorStack to be assigned for this record
* @return void|Doctrine_Validator_ErrorStack returns the errorStack associated with this record
* sets the default values for records internal data
* @param boolean $overwrite whether or not to overwrite the already set values
if ( ! $this->_table->hasDefaultValues()) {
foreach ($this->_data as $column =>
$value) {
$default =
$this->_table->getDefaultValueOf($column);
if ($value ===
self::$_null ||
$overwrite) {
$this->_data[$column] =
$default;
$this->_state =
Doctrine_Record::STATE_TDIRTY;
* @param array $data data array to be cleaned
foreach ($this->getTable()->getColumnNames() as $name) {
if ( ! isset
($tmp[$name])) {
$data[$name] =
self::$_null;
$data[$name] =
$tmp[$name];
* hydrates this object from given array
public function hydrate(array $data)
$this->_values =
$this->cleanData($data);
* prepares identifiers for later use
* @param boolean $exists whether or not this record exists in persistent data store
private function prepareIdentifiers($exists =
true)
switch ($this->_table->getIdentifierType()) {
$name =
$this->_table->getIdentifier();
if (isset
($this->_data[$name]) &&
$this->_data[$name] !==
self::$_null) {
$this->_id[$name] =
$this->_data[$name];
$names =
$this->_table->getIdentifier();
foreach ($names as $name) {
if ($this->_data[$name] ===
self::$_null) {
$this->_id[$name] =
null;
$this->_id[$name] =
$this->_data[$name];
* this method is automatically called when this Doctrine_Record is serialized
unset
($vars['_references']);
unset
($vars['_errorStack']);
unset
($vars['_modified']);
$name =
$this->_table->getIdentifier();
foreach ($this->_data as $k =>
$v) {
unset
($vars['_data'][$k]);
} elseif ($v ===
self::$_null) {
unset
($vars['_data'][$k]);
switch ($this->_table->getTypeOf($k)) {
$vars['_data'][$k] =
serialize($vars['_data'][$k]);
$vars['_data'][$k] =
gzcompress($vars['_data'][$k]);
$vars['_data'][$k] =
$this->_table->enumIndex($k, $vars['_data'][$k]);
* this method is automatically called everytime a Doctrine_Record object is unserialized
* @param string $serialized Doctrine_Record as serialized string
* @throws Doctrine_Record_Exception if the cleanData operation fails somehow
$connection =
$manager->getConnectionForComponent(get_class($this));
$this->_oid =
self::$_index;
foreach($array as $k =>
$v) {
foreach ($this->_data as $k =>
$v) {
switch ($this->_table->getTypeOf($k)) {
$this->_table->getRepository()->add($this);
* returns / assigns the state of this record
* @param integer|string$state if set, this method tries to set the record state to $state
* @see Doctrine_Record::STATE_* constants
* @throws Doctrine_Record_State_Exception if trying to set an unknown state
public function state($state =
null)
if ($state >=
1 &&
$state <=
6) {
$const =
'Doctrine_Record::STATE_' .
$upper;
* refresh internal data from the database
* @throws Doctrine_Record_Exception When the refresh operation fails (when the database row
* this record represents does not exist anymore)
->from($this->_table->getComponentName())
->where(implode(' = ? AND ', $this->_table->getPrimaryKeys()) .
' = ?')
if (count($records) ===
0) {
* refres data of related objects from the database
* @param string $name name of a related component.
* if set, this method only refreshes the specified related component
* @return Doctrine_Record this object
foreach ($this->_table->getRelations() as $rel) {
$this->_references[$rel->getAlias()] =
$rel->fetchRelatedFor($this);
$rel =
$this->_table->getRelation($name);
$this->_references[$name] =
$rel->fetchRelatedFor($this);
* returns the table object for this record
* @return object Doctrine_Table a Doctrine_Table object
* return all the internal data
* @return array an array containing all the properties
* returns the value of a property, if the property is not yet loaded
* this method does NOT load it
* @param $name name of the property
* @throws Doctrine_Record_Exception if trying to get an unknown property
if ( ! isset
($this->_data[$name])) {
if ($this->_data[$name] ===
self::$_null)
return $this->_data[$name];
* loads all the unitialized properties from the database
// only load the data from database if the Doctrine_Record is in proxy state
* returns a value of a property or a related component
* @param mixed $name name of the property or related component
* @param boolean $load whether or not to invoke the loading procedure
* @throws Doctrine_Record_Exception if trying to get a value of unknown property / related component
public function get($name, $load =
true)
$lower =
strtolower($name);
$lower =
$this->_table->getColumnName($lower);
if (isset
($this->_data[$lower])) {
// check if the property is null (= it is the Doctrine_Null object located in self::$_null)
if ($this->_data[$lower] ===
self::$_null &&
$load) {
if ($this->_data[$lower] ===
self::$_null) {
$value =
$this->_data[$lower];
if (isset
($this->_id[$lower])) {
return $this->_id[$lower];
if ($name ===
$this->_table->getIdentifier()) {
if (isset
($this->_values[$lower])) {
$rel =
$this->_table->getRelation($name);
$this->_references[$name] =
$rel->fetchRelatedFor($this);
* This simple method is used for mapping values to $values property.
* Usually this method is used internally by Doctrine for the mapping of
* @param string $name the name of the mapped value
* @param mixed $value mixed value to be mapped
* method for altering properties and Doctrine_Record references
* if the load parameter is set to false this method will not try to load uninitialized record data
* @param mixed $name name of the property or reference
* @param mixed $value value of the property or reference
* @param boolean $load whether or not to refresh / load the uninitialized record data
* @throws Doctrine_Record_Exception if trying to set a value for unknown property / related component
* @throws Doctrine_Record_Exception if trying to set a value of wrong type for related component
* @return Doctrine_Record
public function set($name, $value, $load =
true)
$lower =
$this->_table->getColumnName($lower);
if (isset
($this->_data[$lower])) {
$type =
$this->_table->getTypeOf($name);
if ($id !==
null &&
$type !==
'object') {
$old =
$this->get($lower, $load);
$old =
$this->_data[$lower];
$this->_data[$lower] =
$value;
$rel =
$this->_table->getRelation($name);
// one-to-many or one-to-one relation
if ( ! $rel->isOneToOne()) {
// one-to-many relation found
throw
new Doctrine_Record_Exception("Couldn't call Doctrine::set(), second argument should be an instance of Doctrine_Collection when setting one-to-many references.");
if ($value !==
self::$_null) {
// one-to-one relation found
if ( ! ($value instanceof
Doctrine_Record)) {
throw
new Doctrine_Record_Exception("Couldn't call Doctrine::set(), second argument should be an instance of Doctrine_Record or Doctrine_Null when setting one-to-one references.");
$foreign =
$rel->getForeign();
if (!empty($foreign) &&
$foreign !=
$value->getTable()->getIdentifier())
$this->set($rel->getLocal(), $value->rawGet($foreign), false);
$this->set($rel->getLocal(), $value, false);
$value->set($rel->getForeign(), $this, false);
// join table relation found
throw
new Doctrine_Record_Exception("Couldn't call Doctrine::set(), second argument should be an instance of Doctrine_Collection when setting many-to-many references.");
if (isset
($this->_data[$lower])) {
if (isset
($this->_id[$lower])) {
if (isset
($this->_values[$lower])) {
if (isset
($this->_data[$name])) {
$this->_data[$name] =
array();
// todo: what to do with references ?
* applies the changes made to this object into database
* this method is smart enough to know if any changes are made
* and whether to use INSERT or UPDATE statement
* this method also saves the related components
* @param Doctrine_Connection $conn optional connection parameter
public function save(Doctrine_Connection $conn =
null)
$conn =
$this->_table->getConnection();
$conn->unitOfWork->saveGraph($this);
* Tries to save the object and all its related components.
* In contrast to Doctrine_Record::save(), this method does not
* throw an exception when validation fails but returns TRUE on
* success or FALSE on failure.
* @param Doctrine_Connection $conn optional connection parameter
* @return TRUE if the record was saved sucessfully without errors, FALSE otherwise.
public function trySave(Doctrine_Connection $conn =
null) {
* Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
* query, except that if there is already a row in the table with the same
* key field values, the REPLACE query just updates its values instead of
* The REPLACE type of query does not make part of the SQL standards. Since
* practically only MySQL and SQLIte implement it natively, this type of
* query isemulated through this method for other DBMS using standard types
* of queries inside a transaction to assure the atomicity of the operation.
* @param Doctrine_Connection $conn optional connection parameter
* @throws Doctrine_Connection_Exception if some of the key values was null
* @throws Doctrine_Connection_Exception if there were no key fields
* @throws PDOException if something fails at PDO level
* @return integer number of rows affected
public function replace(Doctrine_Connection $conn =
null)
$conn =
$this->_table->getConnection();
* returns an array of modified fields and associated values
$a[$v] =
$this->_data[$v];
* returns an array of modified fields and values with data preparation
* adds column aggregation inheritance and converts Records into primary key values
foreach ($array as $k =>
$v) {
$type =
$this->_table->getTypeOf($v);
if ($this->_data[$v] ===
self::$_null) {
$a[$v] =
$this->getTable()->getConnection()->convertBooleans($this->_data[$v]);
$a[$v] =
$this->_table->enumIndex($v, $this->_data[$v]);
if ($this->_data[$v] === null) {
throw new Doctrine_Record_Exception('Unexpected null value.');
$a[$v] =
$this->_data[$v];
$map =
$this->_table->inheritanceMap;
foreach ($map as $k =>
$v) {
$old =
$this->get($k, false);
if ((string)
$old !== (string)
$v ||
$old ===
null) {
* this class implements countable interface
* @return integer the number of columns in this record
* @return integer the number of columns in this record
* returns the record as an array
* @param boolean $deep - Return also the relations
public function toArray($deep =
false)
foreach ($this as $column =>
$value) {
if ($value ===
self::$_null) {
if ($this->_table->getIdentifierType() ==
Doctrine::IDENTIFIER_AUTOINC) {
$i =
$this->_table->getIdentifier();
$a[$key] =
$relation->toArray($deep);
* returns true if this record is persistent, otherwise false
* returns true if this record was modified, otherwise false
* method for checking existence of properties and Doctrine_Record references
* @param mixed $name name of the property or reference
if (isset
($this->_data[$name]) || isset
($this->_id[$name])) {
* @return Doctrine_Record_Iterator a Doctrine_Record_Iterator that iterates through the data
* deletes this data access object and all the related composites
* this operation is isolated by a transaction
* this event can be listened by the onPreDelete and onDelete listeners
* @return boolean true on success, false on failure
public function delete(Doctrine_Connection $conn =
null)
$conn =
$this->_table->getConnection();
return $conn->unitOfWork->delete($this);
* returns a copy of this object
* @return Doctrine_Record
if ($this->_table->getIdentifierType() ===
Doctrine::IDENTIFIER_AUTOINC) {
$id =
$this->_table->getIdentifier();
$ret =
$this->_table->create($data);
foreach ($data as $key =>
$val) {
* returns a copy of this object and all its related objects
* @return Doctrine_Record
foreach ($value as $record) {
} elseif ($id ===
true) {
$name =
$this->_table->getIdentifier();
$this->_data[$name] =
$id;
* returns the primary keys of this object
* returns the value of autoincremented primary key of this object (if any)
* this method is used internally be Doctrine_Query
* it is needed to provide compatibility between
* records and collections
* @return Doctrine_Record
* @throws Doctrine_Record_Exception if trying to get an unknown related component
* @return array all references
* @param Doctrine_Access $coll
final public function setRelated($alias, Doctrine_Access $coll)
* loads a related component
* @throws Doctrine_Table_Exception if trying to load an unknown related component
$rel =
$this->_table->getRelation($name);
$this->_references[$name] =
$rel->fetchRelatedFor($this);
* merges this record with an array of values
public function merge(array $values)
foreach ($this->_table->getColumnNames() as $value) {
if (isset
($values[$value])) {
$this->set($value, $values[$value]);
// silence all exceptions
* @param string|array$callback valid callback
* @param string $column column name
* @param mixed arg1 ... argN optional callback arguments
* @return Doctrine_Record
public function call($callback, $column)
$args[0] =
$this->get($column);
$this->_data[$column] =
$newvalue;
* getter for node assciated with this record
* @return mixed if tree returns Doctrine_Node otherwise returns false
if ( ! $this->_table->isTree()) {
if ( ! isset
($this->_node)) {
$this->getTable()->getOption('treeImpl'),
$this->getTable()->getOption('treeOptions')
* reverts this record to given version, this method only works if versioning plugin
* @throws Doctrine_Record_Exception if given version does not exist
* @param integer $version an integer > 1
* @return Doctrine_Record this object
public function revert($version)
->getTemplate('Doctrine_Template_Versionable')
->getVersion($this, $version);
if ( ! isset
($data[0])) {
* removes links from this record to given records
* @param string $alias related component alias
* @param array $ids the identifiers of the related records
* @return Doctrine_Record this object
public function unlink($alias, $ids)
$rel =
$this->getTable()->getRelation($alias);
->from($rel->getAssociationTable()->getComponentName())
->whereIn($rel->getForeign(), $ids);
$q->update($rel->getTable()->getComponentName())
->set($rel->getForeign(), '?', array(null))
->whereIn($rel->getTable()->getIdentifier(), $ids);
foreach ($this->_references[$alias] as $k =>
$record) {
* this method is a magic method that is being used for method overloading
* the function of this method is to try to find given method from the templates
* this record is using and if it finds given method it will execute it
* So, in sense, this method replicates the usage of mixins (as seen in some programming languages)
* @param string $method name of the method
* @param array $args method arguments
* @return mixed the return value of the given method
public function __call($method, $args)
foreach ($this->_table->getTemplates() as $template) {
* used to delete node from tree - MUST BE USE TO DELETE RECORD IF TABLE ACTS AS TREE
* returns a string representation of this object
return (string)
$this->_oid;