Commit 81160cb9 authored by doctrine's avatar doctrine

Fixed one-to-one relation bug, with foreignkey [worked only with localkey]

Fixed session flushing bug
Faster session flushing algorithm
Fixed couple of require_once errors
parent 88dc397e
<?php <?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."iCache.class.php");
/** /**
* Doctrine_CacheFile * Doctrine_CacheFile
* @author Konsta Vesterinen * @author Konsta Vesterinen
......
<?php <?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Cache.class.php");
class Doctrine_Cache_Sqlite { class Doctrine_Cache_Sqlite {
/** /**
* STATS_FILE constant * STATS_FILE constant
......
...@@ -802,12 +802,15 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -802,12 +802,15 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$local = $fk->getLocal(); $local = $fk->getLocal();
$foreign = $fk->getForeign(); $foreign = $fk->getForeign();
$graph = $table->getDQLParser(); $graph = $table->getDQLParser();
$type = $fk->getType();
switch($this->getState()): switch($this->getState()):
case Doctrine_Record::STATE_TDIRTY: case Doctrine_Record::STATE_TDIRTY:
case Doctrine_Record::STATE_TCLEAN: case Doctrine_Record::STATE_TCLEAN:
if($fk->getType() == Doctrine_Table::ONE_COMPOSITE || $fk->getType() == Doctrine_Table::ONE_AGGREGATE) { if($type == Doctrine_Table::ONE_COMPOSITE ||
$type == Doctrine_Table::ONE_AGGREGATE) {
// ONE-TO-ONE // ONE-TO-ONE
$this->references[$name] = $table->create(); $this->references[$name] = $table->create();
...@@ -848,7 +851,14 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -848,7 +851,14 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
} }
} elseif ($fk instanceof Doctrine_ForeignKey) { } elseif ($fk instanceof Doctrine_ForeignKey) {
if(empty($id)) {
$this->references[$name] = $table->create();
$this->references[$name]->set($fk->getForeign(), $this);
} else {
$coll = $graph->query("FROM ".$name." WHERE ".$name.".".$fk->getForeign()." = ?", array($id));
$this->references[$name] = $coll[0];
$this->references[$name]->set($fk->getForeign(), $this);
}
} }
break; break;
default: default:
......
...@@ -58,7 +58,9 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab ...@@ -58,7 +58,9 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
* @var integer $transaction_level the nesting level of transactions, used by transaction methods * @var integer $transaction_level the nesting level of transactions, used by transaction methods
*/ */
private $transaction_level = 0; private $transaction_level = 0;
/**
* @var Doctrine_Validator $validator transaction validator
*/
private $validator; private $validator;
/** /**
* @var PDO $cacheHandler * @var PDO $cacheHandler
...@@ -223,60 +225,49 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab ...@@ -223,60 +225,49 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
* @return array * @return array
*/ */
public function buildFlushTree() { public function buildFlushTree() {
$tables = $this->tables;
$tree = array(); $tree = array();
foreach($this->tables as $k => $table) { foreach($tables as $table) {
$tmp = array(); $name = $table->getComponentName();
$tmp[] = $table->getComponentName(); $index = array_search($name,$tree);
$pos = 0; if($index === false)
foreach($table->getForeignKeys() as $fk) { $tree[] = $name;
if($fk instanceof Doctrine_ForeignKey ||
$fk instanceof Doctrine_LocalKey) {
$name = $fk->getTable()->getComponentName();
$index = array_search($name,$tree);
if(isset($locked[$name])) {
$pos = $index;
continue;
}
foreach($table->getForeignKeys() as $rel) {
$name = $rel->getTable()->getComponentName();
$index = array_search($name,$tree);
if($rel instanceof Doctrine_ForeignKey) {
if($index !== false) if($index !== false)
unset($tree[$index]); unset($tree[$index]);
switch($fk->getType()): $tree[] = $name;
case Doctrine_Table::ONE_COMPOSITE: } elseif($rel instanceof Doctrine_LocalKey) {
case Doctrine_Table::ONE_AGGREGATE: if($index !== false)
array_unshift($tmp,$name); unset($tree[$index]);
break;
case Doctrine_Table::MANY_COMPOSITE: array_unshift($tree, $name);
case Doctrine_Table::MANY_AGGREGATE: } elseif($rel instanceof Doctrine_Association) {
$tmp[] = $name; $t = $rel->getAssociationFactory();
break; $n = $t->getComponentName();
endswitch; $index = array_search($n,$tree);
$locked[$name] = true;
} if($index !== false)
} unset($tree[$index]);
$index = array_search($k,$tree);
$tree[] = $n;
if($index === false) {
if($pos != 0) {
$first = array_splice($tree,0,$pos);
$tree = array_merge($first, $tmp, $tree);
} else {
$tree = array_merge($tree,$tmp);
} }
} else {
$first = array_splice($tree,0,$index);
array_splice($tree, 0, ($index + 1));
$tree = array_merge($first, $tmp, $tree);
} }
} }
return $tree; return $tree;
} }
/** /**
* flush saves all the records from all tables * flush
* this operation is isolated using a transaction * saves all the records from all tables
* this operation is isolated using a transaction
*
* @return void * @return void
*/ */
public function flush() { public function flush() {
...@@ -285,7 +276,8 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab ...@@ -285,7 +276,8 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
$this->commit(); $this->commit();
} }
/** /**
* saveAll save all the records from all tables * saveAll
* saves all the records from all tables
*/ */
private function saveAll() { private function saveAll() {
$tree = $this->buildFlushTree(); $tree = $this->buildFlushTree();
......
...@@ -503,9 +503,9 @@ class Doctrine_Table extends Doctrine_Configurable { ...@@ -503,9 +503,9 @@ class Doctrine_Table extends Doctrine_Configurable {
* findBySql * findBySql
* @return Doctrine_Collection a collection of data access objects * @return Doctrine_Collection a collection of data access objects
*/ */
public function findBySql($sql) { public function findBySql($sql, array $params = array()) {
$graph = new Doctrine_DQL_Parser($this->session); $graph = new Doctrine_DQL_Parser($this->session);
$users = $graph->query("FROM ".$this->name." WHERE ".$sql); $users = $graph->query("FROM ".$this->name." WHERE ".$sql, $params);
return $users; return $users;
} }
/** /**
......
...@@ -3,6 +3,60 @@ require_once("UnitTestCase.class.php"); ...@@ -3,6 +3,60 @@ require_once("UnitTestCase.class.php");
class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
public function testOne2OneForeign() {
$user = new User();
$user->name = "Richard Linklater";
$account = $user->Account;
$account->amount = 1000;
$this->assertTrue($account instanceof Account);
$this->assertEqual($account->getState(), Doctrine_Record::STATE_TDIRTY);
$this->assertEqual($account->entity_id, $user);
$this->assertEqual($account->amount, 1000);
$this->assertEqual($user->name, "Richard Linklater");
$user->save();
$user->refresh();
$account = $user->Account;
$this->assertTrue($account instanceof Account);
$this->assertEqual($account->getState(), Doctrine_Record::STATE_CLEAN);
$this->assertEqual($account->entity_id, $user->getID());
$this->assertEqual($account->amount, 1000);
$this->assertEqual($user->name, "Richard Linklater");
$user = new User();
$user->name = "John Rambo";
$account = $user->Account;
$account->amount = 2000;
$this->assertEqual($account->getTable()->getColumnNames(), array("id","entity_id","amount"));
$this->session->flush();
$this->assertEqual($user->getState(), Doctrine_Record::STATE_CLEAN);
$this->assertTrue($account instanceof Account);
$this->assertEqual($account->getTable()->getColumnNames(), array("id","entity_id","amount"));
$this->assertEqual($account->entity_id, $user->getID());
$this->assertEqual($account->amount, 2000);
$user = $user->getTable()->find($user->getID());
$this->assertEqual($user->getState(), Doctrine_Record::STATE_CLEAN);
$account = $user->Account;
$this->assertTrue($account instanceof Account);
$this->assertEqual($account->getState(), Doctrine_Record::STATE_CLEAN);
$this->assertEqual($account->getTable()->getColumnNames(), array("id","entity_id","amount"));
$this->assertEqual($account->entity_id, $user->getID());
$this->assertEqual($account->amount, 2000);
$this->assertEqual($user->name, "John Rambo");
}
public function testGet() { public function testGet() {
$user = new User(); $user = new User();
$user->name = "Jack Daniels"; $user->name = "Jack Daniels";
...@@ -28,7 +82,6 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { ...@@ -28,7 +82,6 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$this->assertTrue($user->getState() == Doctrine_Record::STATE_CLEAN); $this->assertTrue($user->getState() == Doctrine_Record::STATE_CLEAN);
$this->assertTrue($user->name,"John Locke"); $this->assertTrue($user->name,"John Locke");
} }
public function testTreeStructure() { public function testTreeStructure() {
$e = new Element(); $e = new Element();
$e->name = "parent"; $e->name = "parent";
...@@ -203,7 +256,7 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { ...@@ -203,7 +256,7 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$pf = $this->session->getTable("Phonenumber"); $pf = $this->session->getTable("Phonenumber");
$this->assertTrue($user->Phonenumber instanceof Doctrine_Collection); $this->assertTrue($user->Phonenumber instanceof Doctrine_Collection);
$this->assertTrue($user->Phonenumber->count() == 3); $this->assertEqual($user->Phonenumber->count(), 3);
$coll = new Doctrine_Collection($pf); $coll = new Doctrine_Collection($pf);
...@@ -443,5 +496,6 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { ...@@ -443,5 +496,6 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
public function testGetIterator() { public function testGetIterator() {
$this->assertTrue($this->old->getIterator() instanceof ArrayIterator); $this->assertTrue($this->old->getIterator() instanceof ArrayIterator);
} }
} }
?> ?>
<?php <?php
require_once("UnitTestCase.class.php"); require_once("UnitTestCase.class.php");
class Doctrine_SessionTestCase extends Doctrine_UnitTestCase { class Doctrine_SessionTestCase extends Doctrine_UnitTestCase {
public function testGetFactory() { public function testBuildFlushTree() {
$objTable = $this->session->getTable("User"); $tree = $this->session->buildFlushTree();
$this->assertTrue($objTable instanceOf Doctrine_Table);
//print_r($tree);
} }
public function testFlush() { public function testFlush() {
$this->assertTrue(is_numeric($this->old->Phonenumber[0]->entity_id)); $this->assertTrue(is_numeric($this->old->Phonenumber[0]->entity_id));
......
...@@ -50,7 +50,7 @@ class Doctrine_UnitTestCase extends UnitTestCase { ...@@ -50,7 +50,7 @@ class Doctrine_UnitTestCase extends UnitTestCase {
$this->manager->setAttribute(Doctrine::ATTR_LISTENER, $this->listener); $this->manager->setAttribute(Doctrine::ATTR_LISTENER, $this->listener);
} }
$this->tables = array("entity","email","phonenumber","groupuser","album","song","element","error","description","address"); $this->tables = array("entity","email","phonenumber","groupuser","album","song","element","error","description","address","account");
$tables = $this->tables; $tables = $this->tables;
foreach($tables as $name) { foreach($tables as $name) {
$this->dbh->query("DROP TABLE IF EXISTS $name"); $this->dbh->query("DROP TABLE IF EXISTS $name");
......
...@@ -3,8 +3,8 @@ class Entity extends Doctrine_Record { ...@@ -3,8 +3,8 @@ class Entity extends Doctrine_Record {
public function setUp() { public function setUp() {
$this->ownsOne("Email","Entity.email_id"); $this->ownsOne("Email","Entity.email_id");
$this->ownsMany("Phonenumber","Phonenumber.entity_id"); $this->ownsMany("Phonenumber","Phonenumber.entity_id");
$this->ownsOne("Account","Account.entity_id");
} }
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("name","string",50); $this->hasColumn("name","string",50);
$this->hasColumn("loginname","string",20,"unique"); $this->hasColumn("loginname","string",20,"unique");
...@@ -15,12 +15,21 @@ class Entity extends Doctrine_Record { ...@@ -15,12 +15,21 @@ class Entity extends Doctrine_Record {
$this->hasColumn("email_id","integer"); $this->hasColumn("email_id","integer");
} }
} }
class Account extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn("entity_id","integer");
$this->hasColumn("amount","integer");
}
}
class EntityAddress extends Doctrine_Record { class EntityAddress extends Doctrine_Record {
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("entity_id","integer"); $this->hasColumn("entity_id","integer");
$this->hasColumn("address_id","integer"); $this->hasColumn("address_id","integer");
} }
} }
class Address extends Doctrine_Record { class Address extends Doctrine_Record {
public function setUp() { public function setUp() {
$this->hasMany("User","Entityaddress.entity_id"); $this->hasMany("User","Entityaddress.entity_id");
...@@ -29,6 +38,7 @@ class Address extends Doctrine_Record { ...@@ -29,6 +38,7 @@ class Address extends Doctrine_Record {
$this->hasColumn("address","string",200); $this->hasColumn("address","string",200);
} }
} }
// grouptable doesn't extend Doctrine_Table -> Doctrine_Session // grouptable doesn't extend Doctrine_Table -> Doctrine_Session
// won't initialize grouptable when Doctrine_Session->getTable("Group") is called // won't initialize grouptable when Doctrine_Session->getTable("Group") is called
......
...@@ -24,12 +24,11 @@ error_reporting(E_ALL); ...@@ -24,12 +24,11 @@ error_reporting(E_ALL);
$test = new GroupTest("Doctrine Framework Unit Tests"); $test = new GroupTest("Doctrine Framework Unit Tests");
$test->addTestCase(new Doctrine_SessionTestCase());
$test->addTestCase(new Doctrine_RecordTestCase());
/**
$test->addTestCase(new Doctrine_RecordTestCase());
$test->addTestCase(new Doctrine_SessionTestCase());
$test->addTestCase(new Doctrine_ValidatorTestCase()); $test->addTestCase(new Doctrine_ValidatorTestCase());
$test->addTestCase(new Doctrine_ManagerTestCase()); $test->addTestCase(new Doctrine_ManagerTestCase());
...@@ -54,9 +53,9 @@ $test->addTestCase(new Doctrine_CollectionTestCase()); ...@@ -54,9 +53,9 @@ $test->addTestCase(new Doctrine_CollectionTestCase());
*/
$test->addTestCase(new Doctrine_Collection_OffsetTestCase()); $test->addTestCase(new Doctrine_Collection_OffsetTestCase());
//$test->addTestCase(new Sensei_UnitTestCase()); $test->addTestCase(new Sensei_UnitTestCase());
//$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_FileTestCase());
......
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