Refactored Doctrine_Connection
This commit is contained in:
parent
c5f7572cf2
commit
c6bd1147fe
@ -223,16 +223,38 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
|
|||||||
$query = 'DELETE FROM '. $table . ' WHERE ' . implode(' AND ', $condition);
|
$query = 'DELETE FROM '. $table . ' WHERE ' . implode(' AND ', $condition);
|
||||||
$affectedRows = $this->dbh->exec($query);
|
$affectedRows = $this->dbh->exec($query);
|
||||||
|
|
||||||
$insert = implode(', ', array_keys($values));
|
$this->insert($table, $values);
|
||||||
$query = 'INSERT INTO ' . $table . ' (' . $insert . ') VALUES (' . substr(str_repeat('?, ', count($values)), 0, -2) . ')';
|
|
||||||
$stmt = $this->dbh->prepare($query);
|
|
||||||
$stmt->execute($values);
|
|
||||||
|
|
||||||
$affectedRows++;
|
$affectedRows++;
|
||||||
|
|
||||||
|
|
||||||
return $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
|
* 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
|
* saves all the records from all tables
|
||||||
* this operation is isolated using a transaction
|
* this operation is isolated using a transaction
|
||||||
*
|
*
|
||||||
|
* @throws PDOException if something went wrong at database level
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function flush() {
|
public function flush() {
|
||||||
$this->beginTransaction();
|
$this->beginTransaction();
|
||||||
$this->saveAll();
|
$this->unitOfWork->saveAll();
|
||||||
$this->commit();
|
$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
|
* clear
|
||||||
* clears all repositories
|
* clears all repositories
|
||||||
@ -503,6 +503,10 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
|
|||||||
/**
|
/**
|
||||||
* beginTransaction
|
* beginTransaction
|
||||||
* starts a new transaction
|
* starts a new transaction
|
||||||
|
*
|
||||||
|
* this method can be listened by onPreBeginTransaction and onBeginTransaction
|
||||||
|
* listener methods
|
||||||
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function beginTransaction() {
|
public function beginTransaction() {
|
||||||
@ -579,6 +583,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
|
|||||||
$record->getTable()->getListener()->onDelete($record);
|
$record->getTable()->getListener()->onDelete($record);
|
||||||
|
|
||||||
$this->commit();
|
$this->commit();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
@ -282,7 +282,7 @@ class Doctrine_Connection_Transaction implements Countable, IteratorAggregate {
|
|||||||
/**
|
/**
|
||||||
* inserts a record into database
|
* inserts a record into database
|
||||||
*
|
*
|
||||||
* @param Doctrine_Record $record
|
* @param Doctrine_Record $record record to be inserted
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function insert(Doctrine_Record $record) {
|
public function insert(Doctrine_Record $record) {
|
||||||
@ -298,24 +298,15 @@ class Doctrine_Connection_Transaction implements Countable, IteratorAggregate {
|
|||||||
$keys = $table->getPrimaryKeys();
|
$keys = $table->getPrimaryKeys();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$seq = $record->getTable()->getSequenceName();
|
$seq = $record->getTable()->getSequenceName();
|
||||||
|
|
||||||
if( ! empty($seq)) {
|
if( ! empty($seq)) {
|
||||||
$id = $this->getNextID($seq);
|
$id = $this->nextId($seq);
|
||||||
$name = $record->getTable()->getIdentifier();
|
$name = $record->getTable()->getIdentifier();
|
||||||
$array[$name] = $id;
|
$array[$name] = $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
$strfields = join(", ", array_keys($array));
|
$this->conn->insert($table->getTableName(), $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));
|
|
||||||
|
|
||||||
|
|
||||||
if(count($keys) == 1 && $keys[0] == $table->getIdentifier()) {
|
if(count($keys) == 1 && $keys[0] == $table->getIdentifier()) {
|
||||||
$id = $this->conn->getDBH()->lastInsertID();
|
$id = $this->conn->getDBH()->lastInsertID();
|
||||||
|
@ -142,6 +142,7 @@ class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable {
|
|||||||
* saveRelated
|
* saveRelated
|
||||||
* saves all related records to $record
|
* saves all related records to $record
|
||||||
*
|
*
|
||||||
|
* @throws PDOException if something went wrong at database level
|
||||||
* @param Doctrine_Record $record
|
* @param Doctrine_Record $record
|
||||||
*/
|
*/
|
||||||
public function saveRelated(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
|
* 3, 4 and 5, this method would first destroy the associations to 1 and 2 and then
|
||||||
* save new associations to 4 and 5
|
* save new associations to 4 and 5
|
||||||
*
|
*
|
||||||
|
* @throws PDOException if something went wrong at database level
|
||||||
* @param Doctrine_Record $record
|
* @param Doctrine_Record $record
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
@ -200,6 +202,7 @@ class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable {
|
|||||||
* deletes all related composites
|
* deletes all related composites
|
||||||
* this method is always called internally when a record is deleted
|
* this method is always called internally when a record is deleted
|
||||||
*
|
*
|
||||||
|
* @throws PDOException if something went wrong at database level
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function deleteComposites(Doctrine_Record $record) {
|
public function deleteComposites(Doctrine_Record $record) {
|
||||||
@ -213,6 +216,35 @@ class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable {
|
|||||||
endswitch;
|
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 getIterator() { }
|
||||||
|
|
||||||
public function count() { }
|
public function count() { }
|
||||||
|
Loading…
Reference in New Issue
Block a user