Commit 47daaa9c authored by zYne's avatar zYne

Algorithm for creating the tables in correct order

parent b1a3eed4
...@@ -42,6 +42,10 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun ...@@ -42,6 +42,10 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
* keys representing Doctrine_Table component names and values as Doctrine_Table objects * keys representing Doctrine_Table component names and values as Doctrine_Table objects
*/ */
protected $tables = array(); protected $tables = array();
/**
* @var array $exported
*/
protected $exported = array();
/** /**
* @var string $driverName the name of this connection driver * @var string $driverName the name of this connection driver
*/ */
...@@ -751,15 +755,59 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun ...@@ -751,15 +755,59 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
$class = $name . 'Table'; $class = $name . 'Table';
if (class_exists($class) && in_array('Doctrine_Table', class_parents($class))) { if (class_exists($class) && in_array('Doctrine_Table', class_parents($class))) {
$table = new $class($name, $this, $allowExport); $table = new $class($name, $this);
} else { } else {
$table = new Doctrine_Table($name, $this, $allowExport); $table = new Doctrine_Table($name, $this);
} }
$this->tables[$name] = $table; $this->tables[$name] = $table;
if ($table->getAttribute(Doctrine::ATTR_EXPORT) & Doctrine::EXPORT_TABLES) { if ($allowExport) {
$table->export();
// the following is an algorithm for loading all
// the related tables for all loaded tables
$next = count($this->tables);
$prev = count($this->exported);
$stack = $this->exported;
while ($prev < $next) {
$prev = count($this->tables);
foreach($this->tables as $name => $tableObject) {
if (isset($stack[$name])) {
continue;
} else {
$stack[$name] = true;
}
$tableObject->getRelations();
//$this->getTable('RelationTestChild')->getRelation('Children');
}
$next = count($this->tables);
}
// when all the tables are loaded we build the array in which the order of the tables is
// relationally correct so that then those can be created in the given order)
$names = array_keys($this->tables);
$names = $this->unitOfWork->buildFlushTree($names);
foreach($names as $name) {
$tableObject = $this->tables[$name];
if (isset($this->exported[$name])) {
continue;
}
if ($tableObject->getAttribute(Doctrine::ATTR_EXPORT) & Doctrine::EXPORT_TABLES) {
$tableObject->export();
}
$this->exported[$name] = true;
}
} }
return $table; return $table;
...@@ -863,6 +911,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun ...@@ -863,6 +911,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
public function evictTables() public function evictTables()
{ {
$this->tables = array(); $this->tables = array();
$this->exported = array();
} }
/** /**
* close * close
......
...@@ -49,7 +49,7 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module implemen ...@@ -49,7 +49,7 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module implemen
foreach ($tables as $k => $table) { foreach ($tables as $k => $table) {
if ( ! ($table instanceof Doctrine_Table)) { if ( ! ($table instanceof Doctrine_Table)) {
$table = $this->conn->getTable($table); $table = $this->conn->getTable($table, false);
} }
$nm = $table->getComponentName(); $nm = $table->getComponentName();
......
...@@ -190,7 +190,7 @@ class Doctrine_Export_Sqlite extends Doctrine_Export ...@@ -190,7 +190,7 @@ class Doctrine_Export_Sqlite extends Doctrine_Export
foreach ($fk as $definition) { foreach ($fk as $definition) {
$query = 'CREATE TRIGGER doctrine_' . $name . '_cscd_delete ' $query = 'CREATE TRIGGER doctrine_' . $name . '_cscd_delete '
. 'AFTER DELETE ON ' . $name . ' FOR EACH STATEMENT ' . 'AFTER DELETE ON ' . $name . ' FOR EACH ROW '
. 'BEGIN ' . 'BEGIN '
. 'DELETE FROM ' . $definition['foreignTable'] . ' WHERE '; . 'DELETE FROM ' . $definition['foreignTable'] . ' WHERE ';
......
...@@ -148,7 +148,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -148,7 +148,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
} else { } else {
$class = get_class($this); $class = get_class($this);
// get the table of this class // get the table of this class
$this->_table = Doctrine_Manager::getInstance()->getConnectionForComponent($class)->getTable(get_class($this)); $this->_table = Doctrine_Manager::getInstance()
->getTable(get_class($this));
$exists = false; $exists = false;
} }
...@@ -322,12 +324,12 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -322,12 +324,12 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
*/ */
public function errorStack($stack = null) public function errorStack($stack = null)
{ {
if($stack !== null) { if($stack !== null) {
if( ! ($stack instanceof Doctrine_Validator_ErrorStack)) { if( ! ($stack instanceof Doctrine_Validator_ErrorStack)) {
throw new Doctrine_Record_Exception('Argument should be an instance of Doctrine_Validator_ErrorStack.'); throw new Doctrine_Record_Exception('Argument should be an instance of Doctrine_Validator_ErrorStack.');
} }
$this->_errorStack = $stack; $this->_errorStack = $stack;
} else { } else {
return $this->_errorStack; return $this->_errorStack;
} }
} }
...@@ -1548,7 +1550,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1548,7 +1550,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
*/ */
public function index($name, array $definition = array()) public function index($name, array $definition = array())
{ {
if ( ! $definition) { if ( ! $definition) {
return $this->_table->getIndex($name); return $this->_table->getIndex($name);
} else { } else {
return $this->_table->addIndex($name, $definition); return $this->_table->addIndex($name, $definition);
......
...@@ -172,7 +172,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable ...@@ -172,7 +172,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
* @throws Doctrine_Table_Exception if there is already an instance of this table * @throws Doctrine_Table_Exception if there is already an instance of this table
* @return void * @return void
*/ */
public function __construct($name, Doctrine_Connection $conn, $allowExport) public function __construct($name, Doctrine_Connection $conn)
{ {
$this->conn = $conn; $this->conn = $conn;
...@@ -684,7 +684,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable ...@@ -684,7 +684,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
public function getBound($name) public function getBound($name)
{ {
if ( ! isset($this->bound[$name])) { if ( ! isset($this->bound[$name])) {
throw new Doctrine_Table_Exception('Unknown bound '.$name); throw new Doctrine_Table_Exception('Unknown bound ' . $name);
} }
return $this->bound[$name]; return $this->bound[$name];
} }
...@@ -737,7 +737,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable ...@@ -737,7 +737,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
* @return void * @return void
*/ */
public function unbindAll() public function unbindAll()
{ { throw new Exception();
$this->bound = array(); $this->bound = array();
$this->relations = array(); $this->relations = array();
$this->boundAliases = array(); $this->boundAliases = array();
...@@ -850,6 +850,13 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable ...@@ -850,6 +850,13 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
if (isset($this->relations[$name])) { if (isset($this->relations[$name])) {
return $this->relations[$name]; return $this->relations[$name];
} }
if ( ! $this->conn->hasTable($this->options['name'])) {
$allowExport = true;
} else {
$allowExport = false;
}
if (isset($this->bound[$name])) { if (isset($this->bound[$name])) {
$definition = $this->bound[$name]; $definition = $this->bound[$name];
...@@ -857,7 +864,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable ...@@ -857,7 +864,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
list($component, $definition['foreign']) = explode('.', $definition['field']); list($component, $definition['foreign']) = explode('.', $definition['field']);
unset($definition['field']); unset($definition['field']);
$definition['table'] = $this->conn->getTable($definition['class'], false); $definition['table'] = $this->conn->getTable($definition['class'], $allowExport);
$definition['constraint'] = false; $definition['constraint'] = false;
if ($component == $this->options['name'] || in_array($component, $this->options['parents'])) { if ($component == $this->options['name'] || in_array($component, $this->options['parents'])) {
...@@ -930,7 +937,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable ...@@ -930,7 +937,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
if ($e2[0] != $component) { if ($e2[0] != $component) {
throw new Doctrine_Table_Exception($e2[0] . ' doesn\'t match ' . $component); throw new Doctrine_Table_Exception($e2[0] . ' doesn\'t match ' . $component);
} }
$associationTable = $this->conn->getTable($e2[0]); $associationTable = $this->conn->getTable($e2[0], $allowExport);
if (count($fields) > 1) { if (count($fields) > 1) {
// SELF-REFERENCING THROUGH JOIN TABLE // SELF-REFERENCING THROUGH JOIN TABLE
...@@ -984,6 +991,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable ...@@ -984,6 +991,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
return $this->relations[$name]; return $this->relations[$name];
} }
// load all relations // load all relations
$this->getRelations(); $this->getRelations();
...@@ -1002,7 +1010,6 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable ...@@ -1002,7 +1010,6 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
*/ */
final public function getRelations() final public function getRelations()
{ {
$a = array();
foreach ($this->bound as $k => $v) { foreach ($this->bound as $k => $v) {
$this->getRelation($k); $this->getRelation($k);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment