Record refactoring + new method Doctrine_Manager::install
This commit is contained in:
parent
bb1a39683c
commit
4df1a9d1ca
@ -105,7 +105,24 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
|
||||
|
||||
return $instance;
|
||||
}
|
||||
/**
|
||||
* install
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
final public function install() {
|
||||
$parent = new ReflectionClass('Doctrine_Record');
|
||||
$old = $this->getAttribute(Doctrine::ATTR_CREATE_TABLES);
|
||||
|
||||
$this->attributes[Doctrine::ATTR_CREATE_TABLES] = true;
|
||||
foreach(get_declared_classes() as $name) {
|
||||
$class = new ReflectionClass($name);
|
||||
|
||||
if($class->isSubclassOf($parent))
|
||||
$obj = new $class();
|
||||
}
|
||||
$this->attributes[Doctrine::ATTR_CREATE_TABLES] = $old;
|
||||
}
|
||||
/**
|
||||
* openSession
|
||||
* opens a new session and saves it to Doctrine_Manager->sessions
|
||||
|
@ -812,14 +812,16 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
|
||||
$this->loadReference($alias);
|
||||
}
|
||||
|
||||
$r = $this->getRelationOperations($alias,$new);
|
||||
$r = Doctrine_Relation::getDeleteOperations($this->originals[$alias],$new);
|
||||
|
||||
foreach($r["delete"] as $record) {
|
||||
foreach($r as $record) {
|
||||
$query = "DELETE FROM ".$asf->getTableName()." WHERE ".$fk->getForeign()." = ?"
|
||||
." AND ".$fk->getLocal()." = ?";
|
||||
$this->table->getSession()->execute($query, array($record->getIncremented(),$this->getIncremented()));
|
||||
}
|
||||
foreach($r["add"] as $record) {
|
||||
|
||||
$r = Doctrine_Relation::getInsertOperations($this->originals[$alias],$new);
|
||||
foreach($r as $record) {
|
||||
$reldao = $asf->create();
|
||||
$reldao->set($fk->getForeign(),$record);
|
||||
$reldao->set($fk->getLocal(),$this);
|
||||
@ -846,9 +848,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
|
||||
if( ! isset($this->originals[$alias]))
|
||||
$this->loadReference($alias);
|
||||
|
||||
$r = $this->getRelationOperations($alias,$new);
|
||||
$r = Doctrine_Relation::getDeleteOperations($this->originals[$alias], $new);
|
||||
|
||||
foreach($r["delete"] as $record) {
|
||||
foreach($r as $record) {
|
||||
$record->delete();
|
||||
}
|
||||
|
||||
@ -859,68 +861,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
|
||||
}
|
||||
endforeach;
|
||||
}
|
||||
/**
|
||||
* get the records that need to be added
|
||||
* and/or deleted in order to change the old collection
|
||||
* to the new one
|
||||
*
|
||||
* The algorithm here is very simple and definitely not
|
||||
* the fastest one, since we have to iterate through the collections twice.
|
||||
* the complexity of this algorithm is O(n^2)
|
||||
*
|
||||
* First we iterate through the new collection and get the
|
||||
* records that do not exist in the old collection (Doctrine_Records that need to be added).
|
||||
*
|
||||
* Then we iterate through the old collection and get the records
|
||||
* that do not exists in the new collection (Doctrine_Records that need to be deleted).
|
||||
*/
|
||||
final public function getRelationOperations($name, Doctrine_Collection $new) {
|
||||
$r["add"] = array();
|
||||
$r["delete"] = array();
|
||||
|
||||
|
||||
|
||||
foreach($new as $k=>$record) {
|
||||
|
||||
$found = false;
|
||||
|
||||
$id = $record->getIncremented();
|
||||
if( ! empty($id)) {
|
||||
foreach($this->originals[$name] as $k2 => $record2) {
|
||||
if($record2->getIncremented() === $record->getIncremented()) {
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if( ! $found) {
|
||||
$this->originals[$name][] = $record;
|
||||
$r["add"][] = $record;
|
||||
}
|
||||
}
|
||||
|
||||
foreach($this->originals[$name] as $k => $record) {
|
||||
$id = $record->getIncremented();
|
||||
|
||||
if(empty($id))
|
||||
continue;
|
||||
|
||||
$found = false;
|
||||
foreach($new as $k2 => $record2) {
|
||||
if($record2->getIncremented() === $record->getIncremented()) {
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( ! $found) {
|
||||
$r["delete"][] = $record;
|
||||
unset($this->originals[$name][$k]);
|
||||
}
|
||||
}
|
||||
|
||||
return $r;
|
||||
}
|
||||
/**
|
||||
* getOriginals
|
||||
*/
|
||||
|
@ -95,6 +95,80 @@ class Doctrine_Relation {
|
||||
public function getForeign() {
|
||||
return $this->foreign;
|
||||
}
|
||||
/**
|
||||
* getDeleteOperations
|
||||
*
|
||||
* get the records that need to be deleted in order to change the old collection
|
||||
* to the new one
|
||||
*
|
||||
* The algorithm here is very simple and definitely not
|
||||
* the fastest one, since we have to iterate through the collections twice.
|
||||
* the complexity of this algorithm is O(n^2)
|
||||
*
|
||||
* We iterate through the old collection and get the records
|
||||
* that do not exists in the new collection (Doctrine_Records that need to be deleted).
|
||||
*/
|
||||
final public static function getDeleteOperations(Doctrine_Collection $old, Doctrine_Collection $new) {
|
||||
$r = array();
|
||||
|
||||
foreach($old as $k => $record) {
|
||||
$id = $record->getIncremented();
|
||||
|
||||
if(empty($id))
|
||||
continue;
|
||||
|
||||
$found = false;
|
||||
foreach($new as $k2 => $record2) {
|
||||
if($record2->getIncremented() === $record->getIncremented()) {
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( ! $found) {
|
||||
$r[] = $record;
|
||||
unset($old[$k]);
|
||||
}
|
||||
}
|
||||
|
||||
return $r;
|
||||
}
|
||||
/**
|
||||
* getInsertOperations
|
||||
*
|
||||
* get the records that need to be added in order to change the old collection
|
||||
* to the new one
|
||||
*
|
||||
* The algorithm here is very simple and definitely not
|
||||
* the fastest one, since we have to iterate through the collections twice.
|
||||
* the complexity of this algorithm is O(n^2)
|
||||
*
|
||||
* We iterate through the old collection and get the records
|
||||
* that exists only in the new collection (Doctrine_Records that need to be added).
|
||||
*/
|
||||
final public static function getInsertOperations(Doctrine_Collection $old, Doctrine_Collection $new) {
|
||||
$r = array();
|
||||
|
||||
foreach($new as $k => $record) {
|
||||
$found = false;
|
||||
|
||||
$id = $record->getIncremented();
|
||||
if( ! empty($id)) {
|
||||
foreach($old as $k2 => $record2) {
|
||||
if($record2->getIncremented() === $record->getIncremented()) {
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if( ! $found) {
|
||||
$old[] = $record;
|
||||
$r[] = $record;
|
||||
}
|
||||
}
|
||||
|
||||
return $r;
|
||||
}
|
||||
/**
|
||||
* __toString
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user