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
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."iCache.class.php");
/**
* Doctrine_CacheFile
* @author Konsta Vesterinen
......
<?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Cache.class.php");
class Doctrine_Cache_Sqlite {
/**
* STATS_FILE constant
......
......@@ -802,12 +802,15 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$local = $fk->getLocal();
$foreign = $fk->getForeign();
$graph = $table->getDQLParser();
$type = $fk->getType();
switch($this->getState()):
case Doctrine_Record::STATE_TDIRTY:
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
$this->references[$name] = $table->create();
......@@ -848,7 +851,14 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
}
} 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;
default:
......
......@@ -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
*/
private $transaction_level = 0;
/**
* @var Doctrine_Validator $validator transaction validator
*/
private $validator;
/**
* @var PDO $cacheHandler
......@@ -223,60 +225,49 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
* @return array
*/
public function buildFlushTree() {
$tables = $this->tables;
$tree = array();
foreach($this->tables as $k => $table) {
$tmp = array();
$tmp[] = $table->getComponentName();
$pos = 0;
foreach($table->getForeignKeys() as $fk) {
if($fk instanceof Doctrine_ForeignKey ||
$fk instanceof Doctrine_LocalKey) {
foreach($tables as $table) {
$name = $table->getComponentName();
$index = array_search($name,$tree);
if($index === false)
$tree[] = $name;
$name = $fk->getTable()->getComponentName();
foreach($table->getForeignKeys() as $rel) {
$name = $rel->getTable()->getComponentName();
$index = array_search($name,$tree);
if(isset($locked[$name])) {
$pos = $index;
continue;
}
if($rel instanceof Doctrine_ForeignKey) {
if($index !== false)
unset($tree[$index]);
$tree[] = $name;
} elseif($rel instanceof Doctrine_LocalKey) {
if($index !== false)
unset($tree[$index]);
switch($fk->getType()):
case Doctrine_Table::ONE_COMPOSITE:
case Doctrine_Table::ONE_AGGREGATE:
array_unshift($tmp,$name);
break;
case Doctrine_Table::MANY_COMPOSITE:
case Doctrine_Table::MANY_AGGREGATE:
$tmp[] = $name;
break;
endswitch;
$locked[$name] = true;
}
}
$index = array_search($k,$tree);
array_unshift($tree, $name);
} elseif($rel instanceof Doctrine_Association) {
$t = $rel->getAssociationFactory();
$n = $t->getComponentName();
$index = array_search($n,$tree);
if($index === false) {
if($pos != 0) {
$first = array_splice($tree,0,$pos);
$tree = array_merge($first, $tmp, $tree);
} else {
$tree = array_merge($tree,$tmp);
if($index !== false)
unset($tree[$index]);
$tree[] = $n;
}
} else {
$first = array_splice($tree,0,$index);
array_splice($tree, 0, ($index + 1));
$tree = array_merge($first, $tmp, $tree);
}
}
return $tree;
}
/**
* flush saves all the records from all tables
* flush
* saves all the records from all tables
* this operation is isolated using a transaction
*
* @return void
*/
public function flush() {
......@@ -285,7 +276,8 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
$this->commit();
}
/**
* saveAll save all the records from all tables
* saveAll
* saves all the records from all tables
*/
private function saveAll() {
$tree = $this->buildFlushTree();
......
......@@ -503,9 +503,9 @@ class Doctrine_Table extends Doctrine_Configurable {
* findBySql
* @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);
$users = $graph->query("FROM ".$this->name." WHERE ".$sql);
$users = $graph->query("FROM ".$this->name." WHERE ".$sql, $params);
return $users;
}
/**
......
......@@ -3,6 +3,60 @@ require_once("UnitTestCase.class.php");
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() {
$user = new User();
$user->name = "Jack Daniels";
......@@ -28,7 +82,6 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$this->assertTrue($user->getState() == Doctrine_Record::STATE_CLEAN);
$this->assertTrue($user->name,"John Locke");
}
public function testTreeStructure() {
$e = new Element();
$e->name = "parent";
......@@ -203,7 +256,7 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$pf = $this->session->getTable("Phonenumber");
$this->assertTrue($user->Phonenumber instanceof Doctrine_Collection);
$this->assertTrue($user->Phonenumber->count() == 3);
$this->assertEqual($user->Phonenumber->count(), 3);
$coll = new Doctrine_Collection($pf);
......@@ -443,5 +496,6 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
public function testGetIterator() {
$this->assertTrue($this->old->getIterator() instanceof ArrayIterator);
}
}
?>
<?php
require_once("UnitTestCase.class.php");
class Doctrine_SessionTestCase extends Doctrine_UnitTestCase {
public function testGetFactory() {
$objTable = $this->session->getTable("User");
$this->assertTrue($objTable instanceOf Doctrine_Table);
public function testBuildFlushTree() {
$tree = $this->session->buildFlushTree();
//print_r($tree);
}
public function testFlush() {
$this->assertTrue(is_numeric($this->old->Phonenumber[0]->entity_id));
......
......@@ -50,7 +50,7 @@ class Doctrine_UnitTestCase extends UnitTestCase {
$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;
foreach($tables as $name) {
$this->dbh->query("DROP TABLE IF EXISTS $name");
......
......@@ -3,7 +3,7 @@ class Entity extends Doctrine_Record {
public function setUp() {
$this->ownsOne("Email","Entity.email_id");
$this->ownsMany("Phonenumber","Phonenumber.entity_id");
$this->ownsOne("Account","Account.entity_id");
}
public function setTableDefinition() {
$this->hasColumn("name","string",50);
......@@ -15,12 +15,21 @@ class Entity extends Doctrine_Record {
$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 {
public function setTableDefinition() {
$this->hasColumn("entity_id","integer");
$this->hasColumn("address_id","integer");
}
}
class Address extends Doctrine_Record {
public function setUp() {
$this->hasMany("User","Entityaddress.entity_id");
......@@ -29,6 +38,7 @@ class Address extends Doctrine_Record {
$this->hasColumn("address","string",200);
}
}
// grouptable doesn't extend Doctrine_Table -> Doctrine_Session
// won't initialize grouptable when Doctrine_Session->getTable("Group") is called
......
......@@ -24,12 +24,11 @@ error_reporting(E_ALL);
$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_ManagerTestCase());
......@@ -54,9 +53,9 @@ $test->addTestCase(new Doctrine_CollectionTestCase());
*/
$test->addTestCase(new Doctrine_Collection_OffsetTestCase());
//$test->addTestCase(new Sensei_UnitTestCase());
$test->addTestCase(new Sensei_UnitTestCase());
//$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