1
0
mirror of synced 2024-12-13 14:56:01 +03:00

Refactored Doctrine_Connection

This commit is contained in:
zYne 2006-10-31 10:42:46 +00:00
parent c5f7572cf2
commit c6bd1147fe
3 changed files with 68 additions and 40 deletions

View File

@ -223,16 +223,38 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
$query = 'DELETE FROM '. $table . ' WHERE ' . implode(' AND ', $condition);
$affectedRows = $this->dbh->exec($query);
$insert = implode(', ', array_keys($values));
$query = 'INSERT INTO ' . $table . ' (' . $insert . ') VALUES (' . substr(str_repeat('?, ', count($values)), 0, -2) . ')';
$stmt = $this->dbh->prepare($query);
$stmt->execute($values);
$this->insert($table, $values);
$affectedRows++;
return $affectedRows;
}
/**
* Inserts a table row with specified data.
*
* @param string $table The table to insert data into.
* @param array $values An associateve array containing column-value pairs.
* @return boolean
*/
public function insert($table, array $values = array()) {
if(empty($values))
return false;
// column names are specified as array keys
$cols = array_keys($values);
// build the statement
$query = "INSERT INTO $table "
. '(' . implode(', ', $cols) . ') '
. 'VALUES (' . substr(str_repeat('?, ', count($values)), 0, -2) . ')';
// prepare and execute the statement
$stmt = $this->dbh->prepare($query);
$stmt->execute(array_values($values));
return true;
}
/**
* returns the next value in the given sequence
*
@ -427,36 +449,14 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
* saves all the records from all tables
* this operation is isolated using a transaction
*
* @throws PDOException if something went wrong at database level
* @return void
*/
public function flush() {
$this->beginTransaction();
$this->saveAll();
$this->unitOfWork->saveAll();
$this->commit();
}
/**
* saveAll
* persists all the records from all tables
*
* @return void
*/
private function saveAll() {
$tree = $this->unitOfWork->buildFlushTree($this->tables);
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) {
$this->unitOfWork->saveAssociations($record);
}
}
}
/**
* clear
* clears all repositories
@ -503,6 +503,10 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
/**
* beginTransaction
* starts a new transaction
*
* this method can be listened by onPreBeginTransaction and onBeginTransaction
* listener methods
*
* @return void
*/
public function beginTransaction() {
@ -579,6 +583,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
$record->getTable()->getListener()->onDelete($record);
$this->commit();
return true;
}
/**

View File

@ -282,7 +282,7 @@ class Doctrine_Connection_Transaction implements Countable, IteratorAggregate {
/**
* inserts a record into database
*
* @param Doctrine_Record $record
* @param Doctrine_Record $record record to be inserted
* @return boolean
*/
public function insert(Doctrine_Record $record) {
@ -296,26 +296,17 @@ class Doctrine_Connection_Transaction implements Countable, IteratorAggregate {
$table = $record->getTable();
$keys = $table->getPrimaryKeys();
$seq = $record->getTable()->getSequenceName();
if( ! empty($seq)) {
$id = $this->getNextID($seq);
$id = $this->nextId($seq);
$name = $record->getTable()->getIdentifier();
$array[$name] = $id;
}
$strfields = join(", ", array_keys($array));
$strvalues = substr(str_repeat("?, ", count($array)), 0, -2);
$sql = "INSERT INTO ".$record->getTable()->getTableName()." (".$strfields.") VALUES (".$strvalues.")";
$stmt = $this->conn->getDBH()->prepare($sql);
$stmt->execute(array_values($array));
$this->conn->insert($table->getTableName(), $array);
if(count($keys) == 1 && $keys[0] == $table->getIdentifier()) {
$id = $this->conn->getDBH()->lastInsertID();

View File

@ -142,6 +142,7 @@ class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable {
* saveRelated
* saves all related records to $record
*
* @throws PDOException if something went wrong at database level
* @param Doctrine_Record $record
*/
public function saveRelated(Doctrine_Record $record) {
@ -185,6 +186,7 @@ class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable {
* 3, 4 and 5, this method would first destroy the associations to 1 and 2 and then
* save new associations to 4 and 5
*
* @throws PDOException if something went wrong at database level
* @param Doctrine_Record $record
* @return void
*/
@ -200,6 +202,7 @@ class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable {
* deletes all related composites
* this method is always called internally when a record is deleted
*
* @throws PDOException if something went wrong at database level
* @return void
*/
public function deleteComposites(Doctrine_Record $record) {
@ -213,6 +216,35 @@ class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable {
endswitch;
}
}
/**
* saveAll
* persists all the pending records from all tables
*
* @throws PDOException if something went wrong at database level
* @return void
*/
public function saveAll() {
// get the flush tree
$tree = $this->buildFlushTree($this->conn->getTables());
// save all records
foreach($tree as $name) {
$table = $this->conn->getTable($name);
foreach($table->getRepository() as $record) {
$this->conn->save($record);
}
}
// save all associations
foreach($tree as $name) {
$table = $this->conn->getTable($name);
foreach($table->getRepository() as $record) {
$this->saveAssociations($record);
}
}
}
public function getIterator() { }
public function count() { }