2006-10-09 22:00:14 +04:00
< ? php
/*
* $Id $
*
* 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 >.
*/
Doctrine :: autoload ( 'Doctrine_Access' );
/**
* Doctrine_Record
* All record classes should inherit this super class
*
* @ author Konsta Vesterinen
* @ license LGPL
* @ package Doctrine
*/
abstract class Doctrine_Record extends Doctrine_Access implements Countable , IteratorAggregate , Serializable {
/**
* STATE CONSTANTS
*/
/**
* DIRTY STATE
* a Doctrine_Record is in dirty state when its properties are changed
*/
const STATE_DIRTY = 1 ;
/**
* TDIRTY STATE
* 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
*/
const STATE_TDIRTY = 2 ;
/**
* CLEAN STATE
* a Doctrine_Record is in clean state when all of its properties are loaded from the database
* and none of its properties are changed
*/
const STATE_CLEAN = 3 ;
/**
* PROXY STATE
* a Doctrine_Record is in proxy state when its properties are not fully loaded
*/
const STATE_PROXY = 4 ;
/**
* NEW TCLEAN
* a Doctrine_Record is in transient clean state when it is created and none of its fields are modified
*/
const STATE_TCLEAN = 5 ;
/**
* DELETED STATE
* a Doctrine_Record turns into deleted state when it is deleted
*/
const STATE_DELETED = 6 ;
2006-10-14 20:58:59 +04:00
/**
* the following protected variables use '_' prefixes , the reason for this is to allow child
* classes call for example $this -> id , $this -> state for getting the values of columns named 'id' and 'state'
* rather than the values of these protected variables
*/
2006-10-09 22:00:14 +04:00
/**
* @ var object Doctrine_Table $table the factory that created this data access object
*/
2006-10-14 20:58:59 +04:00
protected $_table ;
2006-10-09 22:00:14 +04:00
/**
* @ var integer $id the primary keys of this object
*/
2006-10-14 20:58:59 +04:00
protected $_id = array ();
2006-10-09 22:00:14 +04:00
/**
* @ var array $data the record data
*/
2006-10-14 20:58:59 +04:00
protected $_data = array ();
2006-10-09 22:00:14 +04:00
/**
* @ var integer $state the state of this record
* @ see STATE_ * constants
*/
2006-10-14 20:58:59 +04:00
protected $_state ;
2006-10-09 22:00:14 +04:00
/**
* @ var array $modified an array containing properties that have been modified
*/
2006-10-14 20:58:59 +04:00
protected $_modified = array ();
/**
* @ var Doctrine_Validator_ErrorStack error stack object
*/
protected $_errorStack ;
2006-10-09 22:00:14 +04:00
/**
* @ var array $references an array containing all the references
*/
private $references = array ();
/**
* @ var array $originals an array containing all the original references
*/
private $originals = array ();
/**
* @ var integer $index this index is used for creating object identifiers
*/
private static $index = 1 ;
/**
* @ var Doctrine_Null $null a Doctrine_Null object used for extremely fast
* null value testing
*/
private static $null ;
/**
* @ var integer $oid object identifier
*/
private $oid ;
/**
* constructor
* @ param Doctrine_Table | null $table a Doctrine_Table object or null ,
* if null the table object is retrieved from current connection
*
* @ throws Doctrine_Connection_Exception if object is created using the new operator and there are no
* open connections
* @ throws Doctrine_Record_Exception if the cleanData operation fails somehow
*/
public function __construct ( $table = null ) {
if ( isset ( $table ) && $table instanceof Doctrine_Table ) {
2006-10-14 20:58:59 +04:00
$this -> _table = $table ;
$exists = ( ! $this -> _table -> isNewEntry ());
2006-10-09 22:00:14 +04:00
} else {
2006-10-14 20:58:59 +04:00
$this -> _table = Doctrine_Manager :: getInstance () -> getCurrentConnection () -> getTable ( get_class ( $this ));
2006-10-09 22:00:14 +04:00
$exists = false ;
}
// 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
// relations.
2006-10-14 20:58:59 +04:00
if ( $this -> _table -> getConnection () -> hasTable ( $this -> _table -> getComponentName ())) {
2006-10-09 22:00:14 +04:00
$this -> oid = self :: $index ;
self :: $index ++ ;
2006-10-14 20:58:59 +04:00
$keys = $this -> _table -> getPrimaryKeys ();
2006-10-09 22:00:14 +04:00
if ( ! $exists ) {
// listen the onPreCreate event
2006-10-14 20:58:59 +04:00
$this -> _table -> getAttribute ( Doctrine :: ATTR_LISTENER ) -> onPreCreate ( $this );
2006-10-09 22:00:14 +04:00
} else {
// listen the onPreLoad event
2006-10-14 20:58:59 +04:00
$this -> _table -> getAttribute ( Doctrine :: ATTR_LISTENER ) -> onPreLoad ( $this );
2006-10-09 22:00:14 +04:00
}
// get the data array
2006-10-14 20:58:59 +04:00
$this -> _data = $this -> _table -> getData ();
2006-10-09 22:00:14 +04:00
// get the column count
2006-10-14 20:58:59 +04:00
$count = count ( $this -> _data );
2006-10-09 22:00:14 +04:00
// clean data array
$this -> cleanData ();
$this -> prepareIdentifiers ( $exists );
if ( ! $exists ) {
if ( $count > 0 )
2006-10-14 20:58:59 +04:00
$this -> _state = Doctrine_Record :: STATE_TDIRTY ;
2006-10-09 22:00:14 +04:00
else
2006-10-14 20:58:59 +04:00
$this -> _state = Doctrine_Record :: STATE_TCLEAN ;
2006-10-09 22:00:14 +04:00
// set the default values for this record
$this -> setDefaultValues ();
// listen the onCreate event
2006-10-14 20:58:59 +04:00
$this -> _table -> getAttribute ( Doctrine :: ATTR_LISTENER ) -> onCreate ( $this );
2006-10-09 22:00:14 +04:00
} else {
2006-10-14 20:58:59 +04:00
$this -> _state = Doctrine_Record :: STATE_CLEAN ;
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
if ( $count < $this -> _table -> getColumnCount ()) {
$this -> _state = Doctrine_Record :: STATE_PROXY ;
2006-10-09 22:00:14 +04:00
}
// listen the onLoad event
2006-10-14 20:58:59 +04:00
$this -> _table -> getAttribute ( Doctrine :: ATTR_LISTENER ) -> onLoad ( $this );
2006-10-09 22:00:14 +04:00
}
2006-10-14 20:58:59 +04:00
$this -> _errorStack = new Doctrine_Validator_ErrorStack ();
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
$repository = $this -> _table -> getRepository ();
2006-10-09 22:00:14 +04:00
$repository -> add ( $this );
}
}
/**
* initNullObject
*
* @ param Doctrine_Null $null
* @ return void
*/
public static function initNullObject ( Doctrine_Null $null ) {
self :: $null = $null ;
}
/**
* @ return Doctrine_Null
*/
public static function getNullObject () {
return self :: $null ;
}
/**
* setUp
* this method is used for setting up relations and attributes
* it should be implemented by child classes
*
* @ return void
*/
public function setUp () { }
/**
* getOID
* returns the object identifier
*
* @ return integer
*/
public function getOID () {
return $this -> oid ;
}
/**
* isValid
*
* @ return boolean whether or not this record passes all column validations
*/
public function isValid () {
2006-10-14 20:58:59 +04:00
if ( ! $this -> _table -> getAttribute ( Doctrine :: ATTR_VLD ))
2006-10-09 22:00:14 +04:00
return true ;
2006-10-10 20:15:43 +04:00
// Clear the stack from any previous errors.
2006-10-14 20:58:59 +04:00
$this -> _errorStack -> clear ();
2006-10-10 20:15:43 +04:00
// Run validation process
2006-10-09 22:00:14 +04:00
$validator = new Doctrine_Validator ();
$validator -> validateRecord ( $this );
$this -> validate ();
2006-10-14 20:58:59 +04:00
if ( $this -> _state == self :: STATE_TDIRTY || $this -> _state == self :: STATE_TCLEAN ) {
2006-10-11 20:24:18 +04:00
$this -> validateOnInsert ();
} else {
$this -> validateOnUpdate ();
}
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
return $this -> _errorStack -> count () == 0 ? true : false ;
2006-10-09 22:00:14 +04:00
}
/**
* Emtpy template method to provide concrete Record classes with the possibility
* to hook into the validation procedure , doing any custom / specialized
* validations that are neccessary .
*/
protected function validate () {}
2006-10-11 20:24:18 +04:00
/**
* Empty tempalte method to provide concrete Record classes with the possibility
* to hook into the validation procedure only when the record is going to be
* updated .
*/
protected function validateOnUpdate () {}
/**
* Empty tempalte 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 .
*/
protected function validateOnInsert () {}
2006-10-09 22:00:14 +04:00
/**
* getErrorStack
*
* @ return Doctrine_Validator_ErrorStack returns the errorStack associated with this record
*/
public function getErrorStack () {
2006-10-14 20:58:59 +04:00
return $this -> _errorStack ;
2006-10-09 22:00:14 +04:00
}
/**
* setDefaultValues
* sets the default values for records internal data
*
* @ param boolean $overwrite whether or not to overwrite the already set values
* @ return boolean
*/
public function setDefaultValues ( $overwrite = false ) {
2006-10-14 20:58:59 +04:00
if ( ! $this -> _table -> hasDefaultValues ())
2006-10-09 22:00:14 +04:00
return false ;
2006-10-14 20:58:59 +04:00
foreach ( $this -> _data as $column => $value ) {
$default = $this -> _table -> getDefaultValueOf ( $column );
2006-10-09 22:00:14 +04:00
if ( $default === null )
$default = self :: $null ;
if ( $value === self :: $null || $overwrite ) {
2006-10-14 20:58:59 +04:00
$this -> _data [ $column ] = $default ;
$this -> _modified [] = $column ;
$this -> _state = Doctrine_Record :: STATE_TDIRTY ;
2006-10-09 22:00:14 +04:00
}
}
}
/**
* cleanData
* this method does several things to records internal data
*
* 1. It unserializes array and object typed columns
* 2. Uncompresses gzip typed columns
* 3. Gets the appropriate enum values for enum typed columns
* 4. Initializes special null object pointer for null values ( for fast column existence checking purposes )
*
*
* example :
*
* $data = array ( " name " => " John " , " lastname " => null , " id " => 1 , " unknown " => " unknown " );
* $names = array ( " name " , " lastname " , " id " );
* $data after operation :
* $data = array ( " name " => " John " , " lastname " => Object ( Doctrine_Null ));
*
* here column 'id' is removed since its auto - incremented primary key ( read - only )
*
* @ throws Doctrine_Record_Exception if unserialization of array / object typed column fails or
* if uncompression of gzip typed column fails
*
* @ return integer
*/
private function cleanData ( $debug = false ) {
2006-10-14 20:58:59 +04:00
$tmp = $this -> _data ;
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
$this -> _data = array ();
2006-10-09 22:00:14 +04:00
$count = 0 ;
2006-10-14 20:58:59 +04:00
foreach ( $this -> _table -> getColumnNames () as $name ) {
$type = $this -> _table -> getTypeOf ( $name );
2006-10-09 22:00:14 +04:00
if ( ! isset ( $tmp [ $name ])) {
2006-10-14 20:58:59 +04:00
$this -> _data [ $name ] = self :: $null ;
2006-10-09 22:00:14 +04:00
} else {
switch ( $type ) :
case " array " :
case " object " :
if ( $tmp [ $name ] !== self :: $null ) {
if ( is_string ( $tmp [ $name ])) {
$value = unserialize ( $tmp [ $name ]);
if ( $value === false )
throw new Doctrine_Record_Exception ( " Unserialization of $name failed. " . var_dump ( substr ( $tmp [ $lower ], 0 , 30 ) . " ... " , true ));
} else
$value = $tmp [ $name ];
2006-10-14 20:58:59 +04:00
$this -> _data [ $name ] = $value ;
2006-10-09 22:00:14 +04:00
}
break ;
case " gzip " :
if ( $tmp [ $name ] !== self :: $null ) {
$value = gzuncompress ( $tmp [ $name ]);
if ( $value === false )
throw new Doctrine_Record_Exception ( " Uncompressing of $name failed. " );
2006-10-14 20:58:59 +04:00
$this -> _data [ $name ] = $value ;
2006-10-09 22:00:14 +04:00
}
break ;
case " enum " :
2006-10-14 20:58:59 +04:00
$this -> _data [ $name ] = $this -> _table -> enumValue ( $name , $tmp [ $name ]);
2006-10-09 22:00:14 +04:00
break ;
default :
2006-10-14 20:58:59 +04:00
$this -> _data [ $name ] = $tmp [ $name ];
2006-10-09 22:00:14 +04:00
endswitch ;
$count ++ ;
}
}
return $count ;
}
/**
* prepareIdentifiers
* prepares identifiers for later use
*
* @ param boolean $exists whether or not this record exists in persistent data store
* @ return void
*/
private function prepareIdentifiers ( $exists = true ) {
2006-10-14 20:58:59 +04:00
switch ( $this -> _table -> getIdentifierType ()) :
2006-10-09 22:00:14 +04:00
case Doctrine_Identifier :: AUTO_INCREMENT :
case Doctrine_Identifier :: SEQUENCE :
2006-10-14 20:58:59 +04:00
$name = $this -> _table -> getIdentifier ();
2006-10-09 22:00:14 +04:00
if ( $exists ) {
2006-10-14 20:58:59 +04:00
if ( isset ( $this -> _data [ $name ]) && $this -> _data [ $name ] !== self :: $null )
$this -> _id [ $name ] = $this -> _data [ $name ];
2006-10-09 22:00:14 +04:00
}
2006-10-14 20:58:59 +04:00
unset ( $this -> _data [ $name ]);
2006-10-09 22:00:14 +04:00
break ;
case Doctrine_Identifier :: NORMAL :
2006-10-14 20:58:59 +04:00
$this -> _id = array ();
$name = $this -> _table -> getIdentifier ();
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
if ( isset ( $this -> _data [ $name ]) && $this -> _data [ $name ] !== self :: $null )
$this -> _id [ $name ] = $this -> _data [ $name ];
2006-10-09 22:00:14 +04:00
break ;
case Doctrine_Identifier :: COMPOSITE :
2006-10-14 20:58:59 +04:00
$names = $this -> _table -> getIdentifier ();
2006-10-09 22:00:14 +04:00
foreach ( $names as $name ) {
2006-10-14 20:58:59 +04:00
if ( $this -> _data [ $name ] === self :: $null )
$this -> _id [ $name ] = null ;
2006-10-09 22:00:14 +04:00
else
2006-10-14 20:58:59 +04:00
$this -> _id [ $name ] = $this -> _data [ $name ];
2006-10-09 22:00:14 +04:00
}
break ;
endswitch ;
}
/**
* serialize
* this method is automatically called when this Doctrine_Record is serialized
*
* @ return array
*/
public function serialize () {
2006-10-14 20:58:59 +04:00
$this -> _table -> getAttribute ( Doctrine :: ATTR_LISTENER ) -> onSleep ( $this );
2006-10-09 22:00:14 +04:00
$vars = get_object_vars ( $this );
unset ( $vars [ 'references' ]);
unset ( $vars [ 'originals' ]);
2006-10-14 20:58:59 +04:00
unset ( $vars [ '_table' ]);
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
$name = $this -> _table -> getIdentifier ();
$this -> _data = array_merge ( $this -> _data , $this -> _id );
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
foreach ( $this -> _data as $k => $v ) {
2006-10-09 22:00:14 +04:00
if ( $v instanceof Doctrine_Record )
2006-10-14 20:58:59 +04:00
unset ( $vars [ '_data' ][ $k ]);
2006-10-09 22:00:14 +04:00
elseif ( $v === self :: $null ) {
2006-10-14 20:58:59 +04:00
unset ( $vars [ '_data' ][ $k ]);
2006-10-09 22:00:14 +04:00
} else {
2006-10-14 20:58:59 +04:00
switch ( $this -> _table -> getTypeOf ( $k )) :
2006-10-09 22:00:14 +04:00
case " array " :
case " object " :
2006-10-14 20:58:59 +04:00
$vars [ '_data' ][ $k ] = serialize ( $vars [ '_data' ][ $k ]);
2006-10-09 22:00:14 +04:00
break ;
endswitch ;
}
}
return serialize ( $vars );
}
/**
* unseralize
* 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
* @ return void
*/
public function unserialize ( $serialized ) {
$manager = Doctrine_Manager :: getInstance ();
$connection = $manager -> getCurrentConnection ();
$this -> oid = self :: $index ;
self :: $index ++ ;
2006-10-14 20:58:59 +04:00
$this -> _table = $connection -> getTable ( get_class ( $this ));
2006-10-09 22:00:14 +04:00
$array = unserialize ( $serialized );
foreach ( $array as $name => $values ) {
$this -> $name = $values ;
}
2006-10-14 20:58:59 +04:00
$this -> _table -> getRepository () -> add ( $this );
2006-10-09 22:00:14 +04:00
$this -> cleanData ();
$this -> prepareIdentifiers ( $this -> exists ());
2006-10-14 20:58:59 +04:00
$this -> _table -> getAttribute ( Doctrine :: ATTR_LISTENER ) -> onWakeUp ( $this );
2006-10-09 22:00:14 +04:00
}
/**
* getState
* returns the current state of the object
*
* @ see Doctrine_Record :: STATE_ * constants
* @ return integer
*/
2006-10-26 01:12:30 +04:00
public function getState () {
2006-10-14 20:58:59 +04:00
return $this -> _state ;
2006-10-09 22:00:14 +04:00
}
2006-10-23 21:34:36 +04:00
/**
* state
* returns / assigns the state of this record
*
* @ param integer | string $state if set , this method tries to set the record state to $state
2006-10-26 01:12:30 +04:00
* @ see Doctrine_Record :: STATE_ * constants
2006-10-23 21:34:36 +04:00
*
* @ throws Doctrine_Record_State_Exception if trying to set an unknown state
* @ return null | integer
*/
public function state ( $state = null ) {
if ( $state == null ) {
return $this -> _state ;
}
2006-10-26 00:02:40 +04:00
$err = false ;
2006-10-23 21:34:36 +04:00
if ( is_integer ( $state )) {
2006-10-26 01:12:30 +04:00
2006-10-23 21:34:36 +04:00
if ( $state >= 1 && $state <= 6 )
$this -> _state = $state ;
2006-10-26 00:02:40 +04:00
else
$err = true ;
2006-10-23 21:34:36 +04:00
} elseif ( is_string ( $state )) {
$upper = strtoupper ( $state );
switch ( $upper ) {
case 'DIRTY' :
case 'CLEAN' :
case 'TDIRTY' :
case 'TCLEAN' :
case 'PROXY' :
case 'DELETED' :
$this -> _state = constant ( 'Doctrine_Record::STATE_' . $upper );
break ;
2006-10-26 00:02:40 +04:00
default :
$err = true ;
2006-10-23 21:34:36 +04:00
}
2006-10-26 00:02:40 +04:00
}
2006-10-23 21:34:36 +04:00
2006-10-26 00:02:40 +04:00
if ( $err )
throw new Doctrine_Record_State_Exception ( 'Unknown record state ' . $state );
2006-10-23 21:34:36 +04:00
}
2006-10-09 22:00:14 +04:00
/**
* refresh
* 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 )
* @ return boolean
*/
final public function refresh () {
$id = $this -> obtainIdentifier ();
if ( ! is_array ( $id ))
$id = array ( $id );
if ( empty ( $id ))
return false ;
$id = array_values ( $id );
2006-10-14 20:58:59 +04:00
$query = $this -> _table -> getQuery () . " WHERE " . implode ( " = ? AND " , $this -> _table -> getPrimaryKeys ()) . " = ? " ;
$stmt = $this -> _table -> getConnection () -> execute ( $query , $id );
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
$this -> _data = $stmt -> fetch ( PDO :: FETCH_ASSOC );
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
if ( ! $this -> _data )
2006-10-09 22:00:14 +04:00
throw new Doctrine_Record_Exception ( 'Failed to refresh. Record does not exist anymore' );
2006-10-14 20:58:59 +04:00
$this -> _data = array_change_key_case ( $this -> _data , CASE_LOWER );
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
$this -> _modified = array ();
2006-10-09 22:00:14 +04:00
$this -> cleanData ( true );
$this -> prepareIdentifiers ();
2006-10-14 20:58:59 +04:00
$this -> _state = Doctrine_Record :: STATE_CLEAN ;
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
$this -> _table -> getAttribute ( Doctrine :: ATTR_LISTENER ) -> onLoad ( $this );
2006-10-09 22:00:14 +04:00
return true ;
}
/**
* factoryRefresh
* refreshes the data from outer source ( Doctrine_Table )
*
* @ throws Doctrine_Record_Exception When the primary key of this record doesn ' t match the primary key fetched from a collection
* @ return void
*/
final public function factoryRefresh () {
2006-10-14 20:58:59 +04:00
$this -> _data = $this -> _table -> getData ();
$old = $this -> _id ;
2006-10-09 22:00:14 +04:00
$this -> cleanData ();
$this -> prepareIdentifiers ();
2006-10-14 20:58:59 +04:00
if ( $this -> _id != $old )
2006-10-09 22:00:14 +04:00
throw new Doctrine_Record_Exception ( " The refreshed primary key doesn't match the one in the record memory. " , Doctrine :: ERR_REFRESH );
2006-10-14 20:58:59 +04:00
$this -> _state = Doctrine_Record :: STATE_CLEAN ;
$this -> _modified = array ();
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
$this -> _table -> getAttribute ( Doctrine :: ATTR_LISTENER ) -> onLoad ( $this );
2006-10-09 22:00:14 +04:00
}
/**
* getTable
* returns the table object for this record
*
* @ return object Doctrine_Table a Doctrine_Table object
*/
final public function getTable () {
2006-10-14 20:58:59 +04:00
return $this -> _table ;
2006-10-09 22:00:14 +04:00
}
/**
* getData
* return all the internal data
*
* @ return array an array containing all the properties
*/
final public function getData () {
2006-10-14 20:58:59 +04:00
return $this -> _data ;
2006-10-09 22:00:14 +04:00
}
/**
* rawGet
* 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
* @ return mixed
*/
public function rawGet ( $name ) {
2006-10-14 20:58:59 +04:00
if ( ! isset ( $this -> _data [ $name ]))
2006-10-09 22:00:14 +04:00
throw new Doctrine_Record_Exception ( 'Unknown property ' . $name );
2006-10-14 20:58:59 +04:00
if ( $this -> _data [ $name ] === self :: $null )
2006-10-09 22:00:14 +04:00
return null ;
2006-10-14 20:58:59 +04:00
return $this -> _data [ $name ];
2006-10-09 22:00:14 +04:00
}
/**
* load
* loads all the unitialized properties from the database
*
* @ return boolean
*/
public function load () {
// only load the data from database if the Doctrine_Record is in proxy state
2006-10-14 20:58:59 +04:00
if ( $this -> _state == Doctrine_Record :: STATE_PROXY ) {
2006-10-18 23:55:14 +04:00
$this -> refresh ();
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
$this -> _state = Doctrine_Record :: STATE_CLEAN ;
2006-10-09 22:00:14 +04:00
return true ;
}
return false ;
}
/**
* get
* returns a value of a property or a related component
*
* @ param mixed $name name of the property or related component
* @ param boolean $invoke whether or not to invoke the onGetProperty listener
* @ throws Doctrine_Record_Exception if trying to get a value of unknown property / related component
* @ return mixed
*/
public function get ( $name , $invoke = true ) {
2006-10-14 20:58:59 +04:00
$listener = $this -> _table -> getAttribute ( Doctrine :: ATTR_LISTENER );
2006-10-09 22:00:14 +04:00
$value = self :: $null ;
$lower = strtolower ( $name );
2006-10-14 20:58:59 +04:00
if ( isset ( $this -> _data [ $lower ])) {
2006-10-09 22:00:14 +04:00
// check if the property is null (= it is the Doctrine_Null object located in self::$null)
2006-10-18 23:55:14 +04:00
if ( $this -> _data [ $lower ] === self :: $null )
2006-10-09 22:00:14 +04:00
$this -> load ();
2006-10-20 22:21:42 +04:00
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
if ( $this -> _data [ $lower ] === self :: $null )
2006-10-09 22:00:14 +04:00
$value = null ;
else
2006-10-14 20:58:59 +04:00
$value = $this -> _data [ $lower ];
2006-10-09 22:00:14 +04:00
}
if ( $value !== self :: $null ) {
2006-10-14 20:58:59 +04:00
$value = $this -> _table -> invokeGet ( $this , $name , $value );
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
if ( $invoke && $name !== $this -> _table -> getIdentifier ())
return $this -> _table -> getAttribute ( Doctrine :: ATTR_LISTENER ) -> onGetProperty ( $this , $name , $value );
2006-10-09 22:00:14 +04:00
else
return $value ;
return $value ;
}
2006-10-14 20:58:59 +04:00
if ( isset ( $this -> _id [ $lower ]))
return $this -> _id [ $lower ];
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
if ( $name === $this -> _table -> getIdentifier ())
2006-10-09 22:00:14 +04:00
return null ;
2006-10-14 20:58:59 +04:00
$rel = $this -> _table -> getRelation ( $name );
2006-10-09 22:00:14 +04:00
try {
if ( ! isset ( $this -> references [ $name ]))
$this -> loadReference ( $name );
} catch ( Doctrine_Table_Exception $e ) {
throw new Doctrine_Record_Exception ( " Unknown property / related component ' $name '. " );
}
return $this -> references [ $name ];
}
/**
* set
* 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 = strtolower ( $name );
2006-10-14 20:58:59 +04:00
if ( isset ( $this -> _data [ $lower ])) {
2006-10-09 22:00:14 +04:00
if ( $value instanceof Doctrine_Record ) {
$id = $value -> getIncremented ();
if ( $id !== null )
$value = $id ;
}
if ( $load )
$old = $this -> get ( $lower , false );
else
2006-10-14 20:58:59 +04:00
$old = $this -> _data [ $lower ];
2006-10-09 22:00:14 +04:00
if ( $old !== $value ) {
2006-10-14 20:58:59 +04:00
$value = $this -> _table -> invokeSet ( $this , $name , $value );
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
$value = $this -> _table -> getAttribute ( Doctrine :: ATTR_LISTENER ) -> onSetProperty ( $this , $name , $value );
2006-10-09 22:00:14 +04:00
if ( $value === null )
$value = self :: $null ;
2006-10-14 20:58:59 +04:00
$this -> _data [ $lower ] = $value ;
$this -> _modified [] = $lower ;
switch ( $this -> _state ) :
2006-10-09 22:00:14 +04:00
case Doctrine_Record :: STATE_CLEAN :
2006-10-14 20:58:59 +04:00
$this -> _state = Doctrine_Record :: STATE_DIRTY ;
2006-10-09 22:00:14 +04:00
break ;
case Doctrine_Record :: STATE_TCLEAN :
2006-10-14 20:58:59 +04:00
$this -> _state = Doctrine_Record :: STATE_TDIRTY ;
2006-10-09 22:00:14 +04:00
break ;
endswitch ;
}
} else {
try {
$this -> coreSetRelated ( $name , $value );
} catch ( Doctrine_Table_Exception $e ) {
throw new Doctrine_Record_Exception ( " Unknown property / related component ' $name '. " );
}
}
}
public function coreSetRelated ( $name , $value ) {
2006-10-14 20:58:59 +04:00
$rel = $this -> _table -> getRelation ( $name );
2006-10-09 22:00:14 +04:00
// one-to-many or one-to-one relation
if ( $rel instanceof Doctrine_Relation_ForeignKey ||
$rel instanceof Doctrine_Relation_LocalKey ) {
if ( ! $rel -> isOneToOne ()) {
// one-to-many relation found
if ( ! ( $value instanceof Doctrine_Collection ))
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. " );
$value -> setReference ( $this , $rel );
} else {
// 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 when setting one-to-one references. " );
if ( $rel instanceof Doctrine_Relation_LocalKey ) {
$this -> set ( $rel -> getLocal (), $value , false );
} else {
$value -> set ( $rel -> getForeign (), $this , false );
}
}
} elseif ( $rel instanceof Doctrine_Relation_Association ) {
// join table relation found
if ( ! ( $value instanceof Doctrine_Collection ))
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. " );
}
$this -> references [ $name ] = $value ;
}
/**
* contains
*
* @ param string $name
* @ return boolean
*/
public function contains ( $name ) {
$lower = strtolower ( $name );
2006-10-14 20:58:59 +04:00
if ( isset ( $this -> _data [ $lower ]))
2006-10-09 22:00:14 +04:00
return true ;
2006-10-14 20:58:59 +04:00
if ( isset ( $this -> _id [ $lower ]))
2006-10-09 22:00:14 +04:00
return true ;
if ( isset ( $this -> references [ $name ]))
return true ;
return false ;
}
/**
* @ param string $name
* @ return void
*/
public function __unset ( $name ) {
2006-10-14 20:58:59 +04:00
if ( isset ( $this -> _data [ $name ]))
$this -> _data [ $name ] = array ();
2006-10-09 22:00:14 +04:00
// 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
*
2006-10-30 02:24:50 +03:00
* this method also saves the related components
2006-10-09 22:00:14 +04:00
*
2006-10-30 02:24:50 +03:00
* @ param Doctrine_Connection $conn
2006-10-09 22:00:14 +04:00
* @ return void
*/
2006-10-30 02:24:50 +03:00
public function save ( Doctrine_Connection $conn = null ) {
2006-10-09 22:00:14 +04:00
if ( $conn === null ) {
2006-10-14 20:58:59 +04:00
$conn = $this -> _table -> getConnection ();
2006-10-09 22:00:14 +04:00
}
$conn -> beginTransaction ();
2006-10-31 02:00:09 +03:00
$saveLater = $conn -> getUnitOfWork () -> saveRelated ( $this );
2006-10-09 22:00:14 +04:00
if ( $this -> isValid ()) {
$conn -> save ( $this );
} else {
$conn -> getTransaction () -> addInvalid ( $this );
}
foreach ( $saveLater as $fk ) {
$table = $fk -> getTable ();
2006-10-14 20:58:59 +04:00
$alias = $this -> _table -> getAlias ( $table -> getComponentName ());
2006-10-09 22:00:14 +04:00
if ( isset ( $this -> references [ $alias ])) {
$obj = $this -> references [ $alias ];
$obj -> save ();
}
}
// save the MANY-TO-MANY associations
2006-10-31 02:00:09 +03:00
$conn -> getUnitOfWork () -> saveAssociations ( $this );
//$this->saveAssociations();
2006-10-09 22:00:14 +04:00
$conn -> commit ();
}
/**
* returns an array of modified fields and associated values
* @ return array
*/
2006-10-30 02:24:50 +03:00
public function getModified () {
2006-10-09 22:00:14 +04:00
$a = array ();
2006-10-14 20:58:59 +04:00
foreach ( $this -> _modified as $k => $v ) {
$a [ $v ] = $this -> _data [ $v ];
2006-10-09 22:00:14 +04:00
}
return $a ;
}
/**
2006-10-20 22:21:42 +04:00
* getPrepared
*
2006-10-09 22:00:14 +04:00
* returns an array of modified fields and values with data preparation
* adds column aggregation inheritance and converts Records into primary key values
*
2006-10-20 22:21:42 +04:00
* @ param array $array
2006-10-09 22:00:14 +04:00
* @ return array
*/
2006-10-20 22:21:42 +04:00
public function getPrepared ( array $array = array ()) {
2006-10-09 22:00:14 +04:00
$a = array ();
if ( empty ( $array ))
2006-10-14 20:58:59 +04:00
$array = $this -> _modified ;
2006-10-09 22:00:14 +04:00
foreach ( $array as $k => $v ) {
2006-10-14 20:58:59 +04:00
$type = $this -> _table -> getTypeOf ( $v );
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
if ( $this -> _data [ $v ] === self :: $null ) {
2006-10-09 22:00:14 +04:00
$a [ $v ] = null ;
continue ;
}
switch ( $type ) {
case 'array' :
case 'object' :
2006-10-14 20:58:59 +04:00
$a [ $v ] = serialize ( $this -> _data [ $v ]);
2006-10-09 22:00:14 +04:00
break ;
case 'gzip' :
2006-10-14 20:58:59 +04:00
$a [ $v ] = gzcompress ( $this -> _data [ $v ], 5 );
2006-10-09 22:00:14 +04:00
break ;
case 'boolean' :
2006-10-14 20:58:59 +04:00
$a [ $v ] = ( int ) $this -> _data [ $v ];
2006-10-09 22:00:14 +04:00
break ;
case 'enum' :
2006-10-14 20:58:59 +04:00
$a [ $v ] = $this -> _table -> enumIndex ( $v , $this -> _data [ $v ]);
2006-10-09 22:00:14 +04:00
break ;
default :
2006-10-14 20:58:59 +04:00
if ( $this -> _data [ $v ] instanceof Doctrine_Record )
$this -> _data [ $v ] = $this -> _data [ $v ] -> getIncremented ();
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
$a [ $v ] = $this -> _data [ $v ];
2006-10-09 22:00:14 +04:00
}
}
2006-10-14 20:58:59 +04:00
foreach ( $this -> _table -> getInheritanceMap () as $k => $v ) {
2006-10-09 22:00:14 +04:00
$old = $this -> get ( $k , false );
if (( string ) $old !== ( string ) $v || $old === null ) {
$a [ $k ] = $v ;
2006-10-14 20:58:59 +04:00
$this -> _data [ $k ] = $v ;
2006-10-09 22:00:14 +04:00
}
}
return $a ;
}
/**
* count
* this class implements countable interface
*
2006-10-24 21:02:47 +04:00
* @ return integer the number of columns in this record
2006-10-09 22:00:14 +04:00
*/
public function count () {
2006-10-14 20:58:59 +04:00
return count ( $this -> _data );
2006-10-09 22:00:14 +04:00
}
/**
* alias for count ()
*
2006-10-24 21:02:47 +04:00
* @ return integer the number of columns in this record
2006-10-09 22:00:14 +04:00
*/
2006-10-24 21:02:47 +04:00
public function columnCount () {
2006-10-09 22:00:14 +04:00
return $this -> count ();
}
/**
* toArray
* returns the record as an array
*
* @ return array
*/
public function toArray () {
$a = array ();
foreach ( $this as $column => $value ) {
$a [ $column ] = $value ;
}
2006-10-14 20:58:59 +04:00
if ( $this -> _table -> getIdentifierType () == Doctrine_Identifier :: AUTO_INCREMENT ) {
$i = $this -> _table -> getIdentifier ();
2006-10-09 22:00:14 +04:00
$a [ $i ] = $this -> getIncremented ();
}
return $a ;
}
/**
* exists
* returns true if this record is persistent , otherwise false
*
* @ return boolean
*/
public function exists () {
2006-10-14 20:58:59 +04:00
return ( $this -> _state !== Doctrine_Record :: STATE_TCLEAN &&
$this -> _state !== Doctrine_Record :: STATE_TDIRTY );
2006-10-09 22:00:14 +04:00
}
/**
* method for checking existence of properties and Doctrine_Record references
* @ param mixed $name name of the property or reference
* @ return boolean
*/
public function hasRelation ( $name ) {
2006-10-14 20:58:59 +04:00
if ( isset ( $this -> _data [ $name ]) || isset ( $this -> _id [ $name ]))
2006-10-09 22:00:14 +04:00
return true ;
2006-10-14 20:58:59 +04:00
return $this -> _table -> hasRelation ( $name );
2006-10-09 22:00:14 +04:00
}
/**
* getIterator
* @ return Doctrine_Record_Iterator a Doctrine_Record_Iterator that iterates through the data
*/
public function getIterator () {
return new Doctrine_Record_Iterator ( $this );
}
/**
* getOriginals
* returns an original collection of related component
*
2006-10-31 02:00:09 +03:00
* @ return Doctrine_Collection | false
2006-10-09 22:00:14 +04:00
*/
2006-10-31 02:00:09 +03:00
public function obtainOriginals ( $name ) {
if ( isset ( $this -> originals [ $name ]))
return $this -> originals [ $name ];
return false ;
2006-10-09 22:00:14 +04:00
}
/**
* 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 ) {
if ( $conn == null ) {
2006-10-14 20:58:59 +04:00
$conn = $this -> _table -> getConnection ();
2006-10-09 22:00:14 +04:00
}
return $conn -> delete ( $this );
}
/**
* copy
* returns a copy of this object
*
* @ return Doctrine_Record
*/
public function copy () {
2006-10-17 01:08:14 +04:00
$ret = $this -> _table -> create ( $this -> _data );
$modified = array ();
foreach ( $this -> _data as $key => $val )
if ( ! ( $val instanceof Doctrine_Null ))
$ret -> _modified [] = $key ;
return $ret ;
2006-10-09 22:00:14 +04:00
}
/**
* assignIdentifier
*
* @ param integer $id
* @ return void
*/
final public function assignIdentifier ( $id = false ) {
if ( $id === false ) {
2006-10-14 20:58:59 +04:00
$this -> _id = array ();
2006-10-09 22:00:14 +04:00
$this -> cleanData ();
2006-10-14 20:58:59 +04:00
$this -> _state = Doctrine_Record :: STATE_TCLEAN ;
$this -> _modified = array ();
2006-10-09 22:00:14 +04:00
} elseif ( $id === true ) {
$this -> prepareIdentifiers ( false );
2006-10-14 20:58:59 +04:00
$this -> _state = Doctrine_Record :: STATE_CLEAN ;
$this -> _modified = array ();
2006-10-09 22:00:14 +04:00
} else {
2006-10-14 20:58:59 +04:00
$name = $this -> _table -> getIdentifier ();
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
$this -> _id [ $name ] = $id ;
$this -> _state = Doctrine_Record :: STATE_CLEAN ;
$this -> _modified = array ();
2006-10-09 22:00:14 +04:00
}
}
/**
* assignOriginals
*
* @ param string $alias
* @ param Doctrine_Collection $coll
* @ return void
*/
public function assignOriginals ( $alias , Doctrine_Collection $coll ) {
$this -> originals [ $alias ] = $coll ;
}
/**
* returns the primary keys of this object
*
* @ return array
*/
final public function obtainIdentifier () {
2006-10-14 20:58:59 +04:00
return $this -> _id ;
2006-10-09 22:00:14 +04:00
}
/**
* returns the value of autoincremented primary key of this object ( if any )
*
* @ return integer
*/
final public function getIncremented () {
2006-10-14 20:58:59 +04:00
$id = current ( $this -> _id );
2006-10-09 22:00:14 +04:00
if ( $id === false )
return null ;
return $id ;
}
/**
* getLast
* this method is used internally be Doctrine_Query
* it is needed to provide compatibility between
* records and collections
*
* @ return Doctrine_Record
*/
public function getLast () {
return $this ;
}
/**
* hasRefence
* @ param string $name
* @ return boolean
*/
public function hasReference ( $name ) {
return isset ( $this -> references [ $name ]);
}
/**
* obtainReference
*
* @ param string $name
* @ throws Doctrine_Record_Exception if trying to get an unknown related component
*/
public function obtainReference ( $name ) {
if ( isset ( $this -> references [ $name ]))
return $this -> references [ $name ];
throw new Doctrine_Record_Exception ( " Unknown reference $name " );
}
/**
* initalizes a one - to - many / many - to - many relation
*
* @ param Doctrine_Collection $coll
* @ param Doctrine_Relation $connector
* @ return boolean
*/
public function initReference ( Doctrine_Collection $coll , Doctrine_Relation $connector ) {
$alias = $connector -> getAlias ();
if ( isset ( $this -> references [ $alias ]))
return false ;
if ( ! $connector -> isOneToOne ()) {
if ( ! ( $connector instanceof Doctrine_Relation_Association ))
$coll -> setReference ( $this , $connector );
$this -> references [ $alias ] = $coll ;
$this -> originals [ $alias ] = clone $coll ;
return true ;
}
return false ;
}
public function lazyInitRelated ( Doctrine_Collection $coll , Doctrine_Relation $connector ) {
}
/**
* addReference
* @ param Doctrine_Record $record
* @ param mixed $key
* @ return void
*/
public function addReference ( Doctrine_Record $record , Doctrine_Relation $connector , $key = null ) {
$alias = $connector -> getAlias ();
$this -> references [ $alias ] -> add ( $record , $key );
$this -> originals [ $alias ] -> add ( $record , $key );
}
/**
* getReferences
* @ return array all references
*/
public function getReferences () {
return $this -> references ;
}
/**
* setRelated
*
* @ param string $alias
* @ param Doctrine_Access $coll
*/
final public function setRelated ( $alias , Doctrine_Access $coll ) {
$this -> references [ $alias ] = $coll ;
$this -> originals [ $alias ] = $coll ;
}
/**
* loadReference
* loads a related component
*
* @ throws Doctrine_Table_Exception if trying to load an unknown related component
* @ param string $name
* @ return void
*/
final public function loadReference ( $name ) {
2006-10-14 20:58:59 +04:00
$fk = $this -> _table -> getRelation ( $name );
2006-10-09 22:00:14 +04:00
if ( $fk -> isOneToOne ()) {
$this -> references [ $name ] = $fk -> fetchRelatedFor ( $this );
} else {
$coll = $fk -> fetchRelatedFor ( $this );
$this -> references [ $name ] = $coll ;
$this -> originals [ $name ] = clone $coll ;
}
}
/**
* binds One - to - One composite relation
*
* @ param string $objTableName
* @ param string $fkField
* @ return void
*/
2006-10-30 02:24:50 +03:00
final public function ownsOne ( $componentName , $foreignKey , $localKey = null ) {
$this -> _table -> bind ( $componentName , $foreignKey , Doctrine_Relation :: ONE_COMPOSITE , $localKey );
2006-10-09 22:00:14 +04:00
}
/**
* binds One - to - Many composite relation
*
* @ param string $objTableName
* @ param string $fkField
* @ return void
*/
final public function ownsMany ( $componentName , $foreignKey , $localKey = null ) {
2006-10-30 02:24:50 +03:00
$this -> _table -> bind ( $componentName , $foreignKey , Doctrine_Relation :: MANY_COMPOSITE , $localKey );
2006-10-09 22:00:14 +04:00
}
/**
* binds One - to - One aggregate relation
*
* @ param string $objTableName
* @ param string $fkField
* @ return void
*/
final public function hasOne ( $componentName , $foreignKey , $localKey = null ) {
2006-10-30 02:24:50 +03:00
$this -> _table -> bind ( $componentName , $foreignKey , Doctrine_Relation :: ONE_AGGREGATE , $localKey );
2006-10-09 22:00:14 +04:00
}
/**
* binds One - to - Many aggregate relation
*
* @ param string $objTableName
* @ param string $fkField
* @ return void
*/
final public function hasMany ( $componentName , $foreignKey , $localKey = null ) {
2006-10-30 02:24:50 +03:00
$this -> _table -> bind ( $componentName , $foreignKey , Doctrine_Relation :: MANY_AGGREGATE , $localKey );
2006-10-09 22:00:14 +04:00
}
/**
* setPrimaryKey
* @ param mixed $key
*/
final public function setPrimaryKey ( $key ) {
2006-10-14 20:58:59 +04:00
$this -> _table -> setPrimaryKey ( $key );
2006-10-09 22:00:14 +04:00
}
/**
* hasColumn
* sets a column definition
*
* @ param string $name
* @ param string $type
* @ param integer $length
* @ param mixed $options
* @ return void
*/
final public function hasColumn ( $name , $type , $length = 2147483647 , $options = " " ) {
2006-10-14 20:58:59 +04:00
$this -> _table -> setColumn ( $name , $type , $length , $options );
2006-10-09 22:00:14 +04:00
}
/**
* countRelated
*
* @ param string $name the name of the related component
* @ return integer
*/
public function countRelated ( $name ) {
2006-10-14 20:58:59 +04:00
$rel = $this -> _table -> getRelation ( $name );
2006-10-09 22:00:14 +04:00
$componentName = $rel -> getTable () -> getComponentName ();
$alias = $rel -> getTable () -> getAlias ( get_class ( $this ));
$query = new Doctrine_Query ();
$query -> from ( $componentName . '(' . 'COUNT(1)' . ')' ) -> where ( $componentName . '.' . $alias . '.' . $this -> getTable () -> getIdentifier () . ' = ?' );
$array = $query -> execute ( array ( $this -> getIncremented ()));
return $array [ 0 ][ 'COUNT(1)' ];
}
/**
* merge
* merges this record with an array of values
*
* @ param array $values
* @ return void
*/
public function merge ( array $values ) {
2006-10-14 20:58:59 +04:00
foreach ( $this -> _table -> getColumnNames () as $value ) {
2006-10-09 22:00:14 +04:00
try {
if ( isset ( $values [ $value ]))
$this -> set ( $value , $values [ $value ]);
} catch ( Exception $e ) {
// silence all exceptions
}
}
}
2006-10-13 01:01:53 +04:00
public function setAttribute ( $attr , $value ) {
2006-10-14 20:58:59 +04:00
$this -> _table -> setAttribute ( $attr , $value );
2006-10-13 01:01:53 +04:00
}
public function setTableName ( $tableName ) {
2006-10-14 20:58:59 +04:00
$this -> _table -> setTableName ( $tableName );
2006-10-13 01:01:53 +04:00
}
public function setInheritanceMap ( $map ) {
2006-10-24 21:02:47 +04:00
$this -> _table -> setOption ( 'inheritanceMap' , $map );
2006-10-13 01:01:53 +04:00
}
public function setEnumValues ( $column , $values ) {
2006-10-14 20:58:59 +04:00
$this -> _table -> setEnumValues ( $column , $values );
2006-10-13 01:01:53 +04:00
}
2006-10-24 21:02:47 +04:00
public function option ( $name , $value = null ) {
if ( $value == null )
$this -> _table -> getOption ( $name );
else
$this -> _table -> setOption ( $name , $value );
}
2006-10-09 22:00:14 +04:00
/**
2006-10-13 01:01:53 +04:00
* addListener
*
* @ param Doctrine_DB_EventListener_Interface | Doctrine_Overloadable $listener
* @ return Doctrine_DB
2006-10-09 22:00:14 +04:00
*/
2006-10-13 01:01:53 +04:00
public function addListener ( $listener , $name = null ) {
2006-10-14 20:58:59 +04:00
$this -> _table -> addListener ( $listener , $name = null );
2006-10-13 01:01:53 +04:00
return $this ;
}
/**
* getListener
*
* @ return Doctrine_DB_EventListener_Interface | Doctrine_Overloadable
*/
public function getListener () {
2006-10-14 20:58:59 +04:00
return $this -> _table -> getListener ();
2006-10-13 01:01:53 +04:00
}
/**
* setListener
*
* @ param Doctrine_DB_EventListener_Interface | Doctrine_Overloadable $listener
* @ return Doctrine_DB
*/
public function setListener ( $listener ) {
2006-10-14 20:58:59 +04:00
$this -> _table -> setListener ( $listener );
2006-10-13 01:01:53 +04:00
return $this ;
}
/**
* call
*
* @ 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 = func_get_args ();
array_shift ( $args );
2006-10-09 22:00:14 +04:00
2006-10-13 01:01:53 +04:00
if ( isset ( $args [ 0 ])) {
$column = $args [ 0 ];
$args [ 0 ] = $this -> get ( $column );
2006-10-09 22:00:14 +04:00
2006-10-13 01:01:53 +04:00
$newvalue = call_user_func_array ( $callback , $args );
2006-10-09 22:00:14 +04:00
2006-10-14 20:58:59 +04:00
$this -> _data [ $column ] = $newvalue ;
2006-10-09 22:00:14 +04:00
}
return $this ;
}
/**
* returns a string representation of this object
*/
public function __toString () {
return Doctrine_Lib :: getRecordAsString ( $this );
}
}