2006-08-22 22:03:16 +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>.
|
|
|
|
*/
|
2006-09-04 02:37:54 +04:00
|
|
|
|
2006-08-22 22:03:16 +04:00
|
|
|
/**
|
|
|
|
* Doctrine_Connection
|
|
|
|
*
|
|
|
|
* @package Doctrine ORM
|
|
|
|
* @url www.phpdoctrine.com
|
|
|
|
* @license LGPL
|
|
|
|
*/
|
|
|
|
abstract class Doctrine_Connection extends Doctrine_Configurable implements Countable, IteratorAggregate {
|
|
|
|
/**
|
2006-09-20 13:46:52 +04:00
|
|
|
* @var $dbh the database handler
|
2006-08-22 22:03:16 +04:00
|
|
|
*/
|
|
|
|
private $dbh;
|
|
|
|
/**
|
2006-09-20 13:46:52 +04:00
|
|
|
* @var Doctrine_Transaction $transaction the transaction object
|
2006-08-22 22:03:16 +04:00
|
|
|
*/
|
2006-09-20 13:46:52 +04:00
|
|
|
private $transaction;
|
2006-08-22 22:03:16 +04:00
|
|
|
/**
|
2006-09-20 13:46:52 +04:00
|
|
|
* @var Doctrine_UnitOfWork $unitOfWork the unit of work object
|
|
|
|
*/
|
|
|
|
private $unitOfWork;
|
|
|
|
/**
|
|
|
|
* @var array $tables an array containing all the initialized Doctrine_Table objects
|
|
|
|
* keys representing Doctrine_Table component names and values as Doctrine_Table objects
|
2006-08-22 22:03:16 +04:00
|
|
|
*/
|
|
|
|
protected $tables = array();
|
2006-09-26 21:20:22 +04:00
|
|
|
/**
|
|
|
|
* @var Doctrine_DataDict $dataDict
|
|
|
|
*/
|
|
|
|
private $dataDict;
|
2006-08-22 22:03:16 +04:00
|
|
|
/**
|
|
|
|
* the constructor
|
|
|
|
*
|
|
|
|
* @param Doctrine_Manager $manager the manager object
|
|
|
|
* @param PDO $pdo the database handler
|
|
|
|
*/
|
|
|
|
public function __construct(Doctrine_Manager $manager,PDO $pdo) {
|
|
|
|
$this->dbh = $pdo;
|
2006-09-20 13:35:29 +04:00
|
|
|
|
2006-09-20 13:55:44 +04:00
|
|
|
$this->transaction = new Doctrine_Connection_Transaction($this);
|
|
|
|
$this->unitOfWork = new Doctrine_Connection_UnitOfWork($this);
|
2006-08-22 22:03:16 +04:00
|
|
|
|
|
|
|
$this->setParent($manager);
|
|
|
|
|
|
|
|
$this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
|
|
|
|
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
|
|
|
|
$this->getAttribute(Doctrine::ATTR_LISTENER)->onOpen($this);
|
|
|
|
}
|
2006-09-20 13:46:52 +04:00
|
|
|
/**
|
|
|
|
* getUnitOfWork
|
|
|
|
*
|
|
|
|
* returns the unit of work object
|
|
|
|
*
|
|
|
|
* @return Doctrine_UnitOfWork
|
|
|
|
*/
|
|
|
|
public function getUnitOfWork() {
|
2006-09-20 13:55:44 +04:00
|
|
|
return $this->unitOfWork;
|
2006-09-20 13:46:52 +04:00
|
|
|
}
|
2006-08-22 22:03:16 +04:00
|
|
|
/**
|
2006-09-20 13:35:29 +04:00
|
|
|
* getTransaction
|
2006-08-22 22:03:16 +04:00
|
|
|
*
|
2006-09-20 13:35:29 +04:00
|
|
|
* returns the current transaction object
|
|
|
|
*
|
|
|
|
* @return Doctrine_Transaction
|
2006-08-22 22:03:16 +04:00
|
|
|
*/
|
2006-09-20 13:35:29 +04:00
|
|
|
public function getTransaction() {
|
2006-09-20 13:46:52 +04:00
|
|
|
return $this->transaction;
|
2006-08-22 22:03:16 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns the manager that created this connection
|
|
|
|
*
|
|
|
|
* @return Doctrine_Manager
|
|
|
|
*/
|
|
|
|
public function getManager() {
|
|
|
|
return $this->getParent();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns the database handler of which this connection uses
|
|
|
|
*
|
2006-09-26 01:08:02 +04:00
|
|
|
* @return PDO the database handler
|
2006-08-22 22:03:16 +04:00
|
|
|
*/
|
|
|
|
public function getDBH() {
|
|
|
|
return $this->dbh;
|
|
|
|
}
|
2006-09-26 01:08:02 +04:00
|
|
|
/**
|
|
|
|
* returns a datadict object
|
|
|
|
*
|
|
|
|
* @return Doctrine_DataDict
|
|
|
|
*/
|
|
|
|
public function getDataDict() {
|
2006-09-26 21:20:22 +04:00
|
|
|
if(isset($this->dataDict))
|
|
|
|
return $this->dataDict;
|
|
|
|
|
|
|
|
$driver = $this->dbh->getAttribute(PDO::ATTR_DRIVER_NAME);
|
|
|
|
switch($driver) {
|
|
|
|
case "mysql":
|
|
|
|
$this->dataDict = new Doctrine_DataDict_Mysql($this);
|
|
|
|
break;
|
|
|
|
case "sqlite":
|
|
|
|
case "sqlite2":
|
|
|
|
$this->dataDict = new Doctrine_DataDict_Sqlite($this);
|
|
|
|
break;
|
|
|
|
case "pgsql":
|
|
|
|
$this->dataDict = new Doctrine_DataDict_Pgsql($this);
|
|
|
|
break;
|
|
|
|
case "oci":
|
|
|
|
case "oci8":
|
|
|
|
$this->dataDict = new Doctrine_DataDict_Oracle($this);
|
|
|
|
break;
|
|
|
|
case "mssql":
|
|
|
|
$this->dataDict = new Doctrine_DataDict_Mssql($this);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Doctrine_Connection_Exception("No datadict driver availible for ".$driver);
|
|
|
|
}
|
|
|
|
return $this->dataDict;
|
|
|
|
}
|
2006-10-23 00:50:27 +04:00
|
|
|
/**
|
|
|
|
* Set the transacton isolation level.
|
|
|
|
* (implemented by the connection drivers)
|
|
|
|
*
|
|
|
|
* @param string standard isolation level
|
|
|
|
* READ UNCOMMITTED (allows dirty reads)
|
|
|
|
* READ COMMITTED (prevents dirty reads)
|
|
|
|
* REPEATABLE READ (prevents nonrepeatable reads)
|
|
|
|
* SERIALIZABLE (prevents phantom reads)
|
|
|
|
* @throws Doctrine_Connection_Mysql_Exception if using unknown isolation level
|
|
|
|
* @throws PDOException if something fails at the PDO level
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setTransactionIsolation($isolation) {
|
|
|
|
throw new Doctrine_Connection_Exception('Transaction isolation levels not supported by this database driver.');
|
|
|
|
}
|
2006-09-15 13:08:44 +04:00
|
|
|
/**
|
2006-09-30 16:36:03 +04:00
|
|
|
* getRegexpOperator
|
2006-09-15 13:08:44 +04:00
|
|
|
* returns the regular expression operator
|
|
|
|
* (implemented by the connection drivers)
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getRegexpOperator() {
|
|
|
|
throw new Doctrine_Connection_Exception('Regular expression operator is not supported by this database driver.');
|
|
|
|
}
|
2006-08-22 22:03:16 +04:00
|
|
|
/**
|
|
|
|
* query
|
|
|
|
* queries the database with Doctrine Query Language
|
|
|
|
*
|
|
|
|
* @param string $query DQL query
|
|
|
|
* @param array $params query parameters
|
|
|
|
*/
|
|
|
|
final public function query($query,array $params = array()) {
|
|
|
|
$parser = new Doctrine_Query($this);
|
|
|
|
|
|
|
|
return $parser->query($query, $params);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* queries the database with limit and offset
|
|
|
|
* added to the query and returns a PDOStatement object
|
|
|
|
*
|
|
|
|
* @param string $query
|
|
|
|
* @param integer $limit
|
|
|
|
* @param integer $offset
|
|
|
|
* @return PDOStatement
|
|
|
|
*/
|
|
|
|
public function select($query,$limit = 0,$offset = 0) {
|
|
|
|
if($limit > 0 || $offset > 0)
|
|
|
|
$query = $this->modifyLimitQuery($query,$limit,$offset);
|
|
|
|
|
|
|
|
return $this->dbh->query($query);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param string $query sql query
|
|
|
|
* @param array $params query parameters
|
|
|
|
*
|
|
|
|
* @return PDOStatement
|
|
|
|
*/
|
|
|
|
public function execute($query, array $params = array()) {
|
|
|
|
if( ! empty($params)) {
|
|
|
|
$stmt = $this->dbh->prepare($query);
|
|
|
|
$stmt->execute($params);
|
|
|
|
return $stmt;
|
|
|
|
} else {
|
|
|
|
return $this->dbh->query($query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* whether or not this connection has table $name initialized
|
|
|
|
*
|
|
|
|
* @param $mixed $name
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function hasTable($name) {
|
|
|
|
return isset($this->tables[$name]);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns a table object for given component name
|
|
|
|
*
|
|
|
|
* @param string $name component name
|
|
|
|
* @return object Doctrine_Table
|
|
|
|
*/
|
|
|
|
public function getTable($name) {
|
|
|
|
if(isset($this->tables[$name]))
|
|
|
|
return $this->tables[$name];
|
|
|
|
|
|
|
|
$class = $name."Table";
|
|
|
|
|
|
|
|
if(class_exists($class) && in_array("Doctrine_Table", class_parents($class))) {
|
2006-09-22 23:23:57 +04:00
|
|
|
return new $class($name, $this);
|
2006-08-22 22:03:16 +04:00
|
|
|
} else {
|
|
|
|
|
2006-09-22 23:23:57 +04:00
|
|
|
return new Doctrine_Table($name, $this);
|
2006-08-22 22:03:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns an array of all initialized tables
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getTables() {
|
|
|
|
return $this->tables;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns an iterator that iterators through all
|
|
|
|
* initialized table objects
|
|
|
|
*
|
|
|
|
* @return ArrayIterator
|
|
|
|
*/
|
|
|
|
public function getIterator() {
|
|
|
|
return new ArrayIterator($this->tables);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns the count of initialized table objects
|
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function count() {
|
|
|
|
return count($this->tables);
|
|
|
|
}
|
|
|
|
/**
|
2006-09-20 15:29:35 +04:00
|
|
|
* addTable
|
|
|
|
* adds a Doctrine_Table object into connection registry
|
|
|
|
*
|
2006-08-22 22:03:16 +04:00
|
|
|
* @param $objTable a Doctrine_Table object to be added into registry
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function addTable(Doctrine_Table $objTable) {
|
|
|
|
$name = $objTable->getComponentName();
|
|
|
|
|
|
|
|
if(isset($this->tables[$name]))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
$this->tables[$name] = $objTable;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/**
|
2006-09-20 15:29:35 +04:00
|
|
|
* create
|
2006-08-22 22:03:16 +04:00
|
|
|
* creates a record
|
|
|
|
*
|
|
|
|
* create creates a record
|
|
|
|
* @param string $name component name
|
|
|
|
* @return Doctrine_Record Doctrine_Record object
|
|
|
|
*/
|
|
|
|
public function create($name) {
|
|
|
|
return $this->getTable($name)->create();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* flush
|
|
|
|
* saves all the records from all tables
|
|
|
|
* this operation is isolated using a transaction
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function flush() {
|
|
|
|
$this->beginTransaction();
|
|
|
|
$this->saveAll();
|
|
|
|
$this->commit();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* saveAll
|
2006-09-20 15:29:35 +04:00
|
|
|
* persists all the records from all tables
|
2006-08-22 22:03:16 +04:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function saveAll() {
|
2006-09-20 13:46:52 +04:00
|
|
|
$tree = $this->unitOfWork->buildFlushTree($this->tables);
|
2006-08-22 22:03:16 +04:00
|
|
|
|
|
|
|
foreach($tree as $name) {
|
|
|
|
$table = $this->tables[$name];
|
|
|
|
|
|
|
|
foreach($table->getRepository() as $record) {
|
|
|
|
$this->save($record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach($tree as $name) {
|
|
|
|
$table = $this->tables[$name];
|
|
|
|
foreach($table->getRepository() as $record) {
|
|
|
|
$record->saveAssociations();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* clear
|
|
|
|
* clears all repositories
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function clear() {
|
|
|
|
foreach($this->tables as $k => $table) {
|
|
|
|
$table->getRepository()->evictAll();
|
|
|
|
$table->clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
2006-09-20 15:29:35 +04:00
|
|
|
* evictTables
|
|
|
|
* evicts all tables
|
|
|
|
*
|
2006-08-22 22:03:16 +04:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function evictTables() {
|
|
|
|
$this->tables = array();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* close
|
|
|
|
* closes the connection
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function close() {
|
|
|
|
$this->getAttribute(Doctrine::ATTR_LISTENER)->onPreClose($this);
|
|
|
|
|
|
|
|
$this->clear();
|
|
|
|
$this->state = Doctrine_Connection::STATE_CLOSED;
|
|
|
|
|
|
|
|
$this->getAttribute(Doctrine::ATTR_LISTENER)->onClose($this);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* get the current transaction nesting level
|
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getTransactionLevel() {
|
2006-09-20 13:46:52 +04:00
|
|
|
return $this->transaction->getTransactionLevel();
|
2006-08-22 22:03:16 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* beginTransaction
|
|
|
|
* starts a new transaction
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function beginTransaction() {
|
2006-09-20 13:46:52 +04:00
|
|
|
$this->transaction->beginTransaction();
|
2006-08-22 22:03:16 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* commits the current transaction
|
|
|
|
* if lockmode is optimistic this method starts a transaction
|
|
|
|
* and commits it instantly
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function commit() {
|
2006-09-20 13:46:52 +04:00
|
|
|
$this->transaction->commit();
|
2006-08-22 22:03:16 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* rollback
|
|
|
|
* rolls back all transactions
|
|
|
|
*
|
|
|
|
* this method also listens to onPreTransactionRollback and onTransactionRollback
|
|
|
|
* eventlisteners
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function rollback() {
|
2006-09-20 13:46:52 +04:00
|
|
|
$this->transaction->rollback();
|
2006-08-22 22:03:16 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns maximum identifier values
|
|
|
|
*
|
|
|
|
* @param array $names an array of component names
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getMaximumValues(array $names) {
|
|
|
|
$values = array();
|
|
|
|
foreach($names as $name) {
|
|
|
|
$table = $this->tables[$name];
|
|
|
|
$keys = $table->getPrimaryKeys();
|
|
|
|
$tablename = $table->getTableName();
|
|
|
|
|
|
|
|
if(count($keys) == 1 && $keys[0] == $table->getIdentifier()) {
|
|
|
|
// record uses auto_increment column
|
|
|
|
|
|
|
|
$sql = "SELECT MAX(".$table->getIdentifier().") FROM ".$tablename;
|
|
|
|
$stmt = $this->dbh->query($sql);
|
|
|
|
$data = $stmt->fetch(PDO::FETCH_NUM);
|
|
|
|
$values[$tablename] = $data[0];
|
|
|
|
|
|
|
|
$stmt->closeCursor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $values;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* saves a collection
|
|
|
|
*
|
|
|
|
* @param Doctrine_Collection $coll
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function saveCollection(Doctrine_Collection $coll) {
|
|
|
|
$this->beginTransaction();
|
|
|
|
|
|
|
|
foreach($coll as $key=>$record):
|
|
|
|
$record->save();
|
|
|
|
endforeach;
|
|
|
|
|
|
|
|
$this->commit();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* deletes all records from collection
|
|
|
|
*
|
|
|
|
* @param Doctrine_Collection $coll
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function deleteCollection(Doctrine_Collection $coll) {
|
|
|
|
$this->beginTransaction();
|
|
|
|
foreach($coll as $k=>$record) {
|
|
|
|
$record->delete();
|
|
|
|
}
|
|
|
|
$this->commit();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* saves the given record
|
|
|
|
*
|
|
|
|
* @param Doctrine_Record $record
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function save(Doctrine_Record $record) {
|
2006-09-30 16:36:03 +04:00
|
|
|
$record->getTable()->getAttribute(Doctrine::ATTR_LISTENER)->onPreSave($record);
|
|
|
|
|
|
|
|
|
2006-08-22 22:03:16 +04:00
|
|
|
switch($record->getState()):
|
|
|
|
case Doctrine_Record::STATE_TDIRTY:
|
2006-09-30 16:36:03 +04:00
|
|
|
$this->transaction->insert($record);
|
2006-08-22 22:03:16 +04:00
|
|
|
break;
|
|
|
|
case Doctrine_Record::STATE_DIRTY:
|
|
|
|
case Doctrine_Record::STATE_PROXY:
|
2006-09-30 16:36:03 +04:00
|
|
|
$this->transaction->update($record);
|
2006-08-22 22:03:16 +04:00
|
|
|
break;
|
|
|
|
case Doctrine_Record::STATE_CLEAN:
|
|
|
|
case Doctrine_Record::STATE_TCLEAN:
|
|
|
|
// do nothing
|
|
|
|
break;
|
|
|
|
endswitch;
|
2006-09-30 16:36:03 +04:00
|
|
|
|
|
|
|
$record->getTable()->getAttribute(Doctrine::ATTR_LISTENER)->onSave($record);
|
2006-08-22 22:03:16 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* saves all related records to $record
|
|
|
|
*
|
|
|
|
* @param Doctrine_Record $record
|
|
|
|
*/
|
2006-09-30 16:36:03 +04:00
|
|
|
public function saveRelated(Doctrine_Record $record) {
|
2006-08-22 22:03:16 +04:00
|
|
|
$saveLater = array();
|
|
|
|
foreach($record->getReferences() as $k=>$v) {
|
2006-08-31 13:04:14 +04:00
|
|
|
$fk = $record->getTable()->getRelation($k);
|
2006-09-29 00:57:39 +04:00
|
|
|
if($fk instanceof Doctrine_Relation_ForeignKey ||
|
|
|
|
$fk instanceof Doctrine_Relation_LocalKey) {
|
2006-09-30 16:36:03 +04:00
|
|
|
if($fk->isComposite()) {
|
|
|
|
$local = $fk->getLocal();
|
|
|
|
$foreign = $fk->getForeign();
|
|
|
|
|
|
|
|
if($record->getTable()->hasPrimaryKey($fk->getLocal())) {
|
|
|
|
if( ! $record->exists())
|
|
|
|
$saveLater[$k] = $fk;
|
|
|
|
else
|
|
|
|
$v->save();
|
|
|
|
} else {
|
|
|
|
// ONE-TO-ONE relationship
|
|
|
|
$obj = $record->get($fk->getTable()->getComponentName());
|
|
|
|
|
|
|
|
if($obj->getState() != Doctrine_Record::STATE_TCLEAN)
|
|
|
|
$obj->save();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2006-09-29 00:57:39 +04:00
|
|
|
} elseif($fk instanceof Doctrine_Relation_Association) {
|
2006-08-22 22:03:16 +04:00
|
|
|
$v->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $saveLater;
|
|
|
|
}
|
|
|
|
/**
|
2006-09-20 13:35:29 +04:00
|
|
|
* deletes all related composites
|
|
|
|
* this method is always called internally when a record is deleted
|
2006-08-22 22:03:16 +04:00
|
|
|
*
|
2006-09-20 13:35:29 +04:00
|
|
|
* @return void
|
2006-08-22 22:03:16 +04:00
|
|
|
*/
|
2006-09-20 13:35:29 +04:00
|
|
|
final public function deleteComposites(Doctrine_Record $record) {
|
|
|
|
foreach($record->getTable()->getRelations() as $fk) {
|
|
|
|
switch($fk->getType()):
|
|
|
|
case Doctrine_Relation::ONE_COMPOSITE:
|
|
|
|
case Doctrine_Relation::MANY_COMPOSITE:
|
2006-09-30 16:36:03 +04:00
|
|
|
$obj = $record->get($fk->getAlias());
|
2006-09-20 13:35:29 +04:00
|
|
|
$obj->delete();
|
|
|
|
break;
|
|
|
|
endswitch;
|
2006-08-22 22:03:16 +04:00
|
|
|
}
|
2006-09-20 13:35:29 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* saveAssociations
|
|
|
|
* save the associations of many-to-many relations
|
|
|
|
* this method also deletes associations that do not exist anymore
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
final public function saveAssociations(Doctrine_Record $record) {
|
|
|
|
foreach($record->getTable()->table->getRelations() as $rel):
|
|
|
|
$table = $rel->getTable();
|
|
|
|
$name = $table->getComponentName();
|
|
|
|
$alias = $this->table->getAlias($name);
|
2006-08-22 22:03:16 +04:00
|
|
|
|
2006-09-29 00:57:39 +04:00
|
|
|
if($rel instanceof Doctrine_Relation_Association) {
|
2006-08-22 22:03:16 +04:00
|
|
|
|
2006-10-08 22:43:13 +04:00
|
|
|
$asf = $rel->getAssociationFactory();
|
2006-08-22 22:03:16 +04:00
|
|
|
|
2006-10-08 22:43:13 +04:00
|
|
|
if($record->hasReference($alias)) {
|
2006-08-22 22:03:16 +04:00
|
|
|
|
2006-10-08 22:43:13 +04:00
|
|
|
$new = $record->getReference($alias);
|
2006-08-22 22:03:16 +04:00
|
|
|
|
2006-10-08 22:43:13 +04:00
|
|
|
if( ! $this->hasOriginalsFor($alias))
|
|
|
|
$record->loadReference($alias);
|
2006-08-22 22:03:16 +04:00
|
|
|
|
2006-10-08 22:43:13 +04:00
|
|
|
|
|
|
|
$operations = Doctrine_Relation::getDeleteOperations($this->originals[$alias],$new);
|
|
|
|
|
|
|
|
foreach($operations as $r) {
|
|
|
|
$query = "DELETE FROM ".$asf->getTableName()." WHERE ".$fk->getForeign()." = ?"
|
2006-09-20 13:35:29 +04:00
|
|
|
." AND ".$fk->getLocal()." = ?";
|
2006-10-08 22:43:13 +04:00
|
|
|
$this->table->getConnection()->execute($query, array($r->getIncremented(),$record->getIncremented()));
|
|
|
|
}
|
|
|
|
|
2006-10-20 22:21:42 +04:00
|
|
|
$operations = Doctrine_Relation::getInsertOperations($record->obtainOriginals($alias),$new);
|
2006-10-08 22:43:13 +04:00
|
|
|
foreach($operations as $r) {
|
|
|
|
$reldao = $asf->create();
|
|
|
|
$reldao->set($fk->getForeign(),$r);
|
|
|
|
$reldao->set($fk->getLocal(),$this);
|
|
|
|
$reldao->save();
|
|
|
|
|
|
|
|
}
|
2006-10-20 22:21:42 +04:00
|
|
|
$record->assignOriginals($alias, clone $this->references[$alias]);
|
2006-10-08 22:43:13 +04:00
|
|
|
}
|
2006-09-29 00:57:39 +04:00
|
|
|
} elseif($fk instanceof Doctrine_Relation_ForeignKey ||
|
|
|
|
$fk instanceof Doctrine_Relation_LocalKey) {
|
2006-08-22 22:03:16 +04:00
|
|
|
|
2006-10-08 22:43:13 +04:00
|
|
|
if($fk->isOneToOne()) {
|
2006-10-20 22:21:42 +04:00
|
|
|
if($record->obtainOriginals($alias) && $record->obtainOriginals($alias)->obtainIdentifier() != $this->references[$alias]->obtainIdentifier())
|
|
|
|
$record->obtainOriginals($alias)->delete();
|
2006-10-08 22:43:13 +04:00
|
|
|
} else {
|
|
|
|
if(isset($this->references[$alias])) {
|
|
|
|
$new = $this->references[$alias];
|
2006-08-22 22:03:16 +04:00
|
|
|
|
2006-10-08 22:43:13 +04:00
|
|
|
if( ! isset($this->originals[$alias]))
|
|
|
|
$record->loadReference($alias);
|
2006-08-22 22:03:16 +04:00
|
|
|
|
2006-10-08 22:43:13 +04:00
|
|
|
$operations = Doctrine_Relation::getDeleteOperations($this->originals[$alias], $new);
|
2006-08-22 22:03:16 +04:00
|
|
|
|
2006-10-08 22:43:13 +04:00
|
|
|
foreach($operations as $r) {
|
|
|
|
$r->delete();
|
2006-09-20 13:35:29 +04:00
|
|
|
}
|
2006-10-08 22:43:13 +04:00
|
|
|
|
|
|
|
$record->assignOriginals($alias, clone $this->references[$alias]);
|
|
|
|
}
|
|
|
|
}
|
2006-09-20 13:35:29 +04:00
|
|
|
}
|
|
|
|
endforeach;
|
2006-08-22 22:03:16 +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
|
|
|
|
*/
|
|
|
|
final public function delete(Doctrine_Record $record) {
|
2006-09-20 15:29:35 +04:00
|
|
|
if( ! $record->exists())
|
|
|
|
return false;
|
2006-09-30 16:36:03 +04:00
|
|
|
|
2006-09-20 15:29:35 +04:00
|
|
|
$this->beginTransaction();
|
2006-08-22 22:03:16 +04:00
|
|
|
|
2006-09-30 16:36:03 +04:00
|
|
|
$record->getTable()->getListener()->onPreDelete($record);
|
|
|
|
|
2006-09-20 15:29:35 +04:00
|
|
|
$this->deleteComposites($record);
|
2006-09-30 16:36:03 +04:00
|
|
|
|
2006-09-20 15:29:35 +04:00
|
|
|
$this->transaction->addDelete($record);
|
2006-08-22 22:03:16 +04:00
|
|
|
|
2006-09-30 16:36:03 +04:00
|
|
|
$record->getTable()->getListener()->onDelete($record);
|
|
|
|
|
2006-09-20 15:29:35 +04:00
|
|
|
$this->commit();
|
|
|
|
return true;
|
2006-08-22 22:03:16 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns a string representation of this object
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function __toString() {
|
|
|
|
return Doctrine_Lib::getConnectionAsString($this);
|
|
|
|
}
|
|
|
|
}
|
2006-09-04 02:46:30 +04:00
|
|
|
|