Commit 00cbc5d1 authored by zYne's avatar zYne

Fixed many-to-many handling when join table component is accessed before the...

Fixed many-to-many handling when join table component is accessed before the associated component, fixes #136, #84
parent bbc3d3f6
......@@ -585,10 +585,12 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
return false;
}
/**
* getRelation
*
* @param string $name component name of which a foreign key object is bound
* @return Doctrine_Relation
*/
final public function getRelation($name) {
final public function getRelation($name, $recursive = true) {
$original = $name;
if(isset($this->relations[$name]))
......@@ -672,10 +674,13 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
$this->relations[$alias] = $relation;
return $this->relations[$alias];
}
try {
throw new Doctrine_Table_Exception($this->name . " doesn't have a relation to " . $original);
} catch(Exception $e) {
print $e;
// load all relations
$this->getRelations();
if($recursive) {
return $this->getRelation($original, false);
} else {
throw new Doctrine_Table_Exception($this->name . " doesn't have a relation to " . $original);
}
}
/**
......
<?php
class Transaction_TestLogger implements Doctrine_Overloadable {
private $messages = array();
public function __call($m, $a) {
$this->messages[] = $m;
}
public function pop() {
return array_pop($this->messages);
}
public function clear() {
$this->messages = array();
}
public function getAll() {
return $this->messages;
}
}
class Doctrine_Connection_Transaction_TestCase extends Doctrine_UnitTestCase {
public function prepareData() { }
public function testInsert() {
$count = count($this->dbh);
$listener = new Transaction_TestLogger();
$user = new User();
$user->getTable()->getConnection()->setListener($listener);
$this->connection->beginTransaction();
$user->name = 'John';
$user->save();
$this->assertEqual($listener->pop(), 'onSave');
$this->assertEqual($listener->pop(), 'onInsert');
$this->assertEqual($listener->pop(), 'onPreInsert');
$this->assertEqual($listener->pop(), 'onPreSave');
$this->assertEqual($listener->pop(), 'onSetProperty');
$this->assertEqual($listener->pop(), 'onTransactionBegin');
$this->assertEqual($listener->pop(), 'onPreTransactionBegin');
$this->assertEqual($user->id, 1);
$this->assertTrue($count < count($this->dbh));
$this->connection->commit();
$this->assertEqual($listener->pop(), 'onTransactionCommit');
$this->assertEqual($listener->pop(), 'onPreTransactionCommit');
}
public function testInsertMultiple() {
$count = count($this->dbh);
$listener = new Transaction_TestLogger();
$users = new Doctrine_Collection('User');
$users->getTable()->getConnection()->setListener($listener);
$this->connection->beginTransaction();
$users[0]->name = 'Arnold';
$users[1]->name = 'Vincent';
$users[0]->save();
$users[1]->save();
$this->assertEqual($listener->pop(), 'onSave');
$this->assertEqual($listener->pop(), 'onInsert');
$this->assertEqual($listener->pop(), 'onPreInsert');
$this->assertEqual($listener->pop(), 'onPreSave');
$this->assertEqual($listener->pop(), 'onSave');
$this->assertEqual($listener->pop(), 'onInsert');
$this->assertEqual($listener->pop(), 'onPreInsert');
$this->assertEqual($listener->pop(), 'onPreSave');
$this->assertEqual($users[0]->id, 2);
$this->assertEqual($users[1]->id, 3);
$this->assertTrue($count < count($this->dbh));
$this->connection->commit();
$this->assertEqual($listener->pop(), 'onTransactionCommit');
$this->assertEqual($listener->pop(), 'onPreTransactionCommit');
}
public function testUpdate() {
$count = count($this->dbh);
$user = $this->connection->getTable('User')->find(1);
$listener = new Transaction_TestLogger();
$user->getTable()->getConnection()->setListener($listener);
$this->connection->beginTransaction();
$user->name = 'Jack';
$user->save();
$this->assertEqual($listener->pop(), 'onSave');
$this->assertEqual($listener->pop(), 'onUpdate');
$this->assertEqual($listener->pop(), 'onPreUpdate');
$this->assertEqual($listener->pop(), 'onPreSave');
$this->assertEqual($listener->pop(), 'onSetProperty');
$this->assertEqual($listener->pop(), 'onTransactionBegin');
$this->assertEqual($listener->pop(), 'onPreTransactionBegin');
$this->assertEqual($user->id, 1);
$this->assertTrue($count < count($this->dbh));
$this->connection->commit();
$this->assertEqual($listener->pop(), 'onTransactionCommit');
$this->assertEqual($listener->pop(), 'onPreTransactionCommit');
}
public function testUpdateMultiple() {
$count = count($this->dbh);
$listener = new Transaction_TestLogger();
$users = $this->connection->query('FROM User');
$users->getTable()->getConnection()->setListener($listener);
$this->connection->beginTransaction();
$users[1]->name = 'Arnold';
$users[2]->name = 'Vincent';
$users[1]->save();
$users[2]->save();
$this->assertEqual($listener->pop(), 'onSave');
$this->assertEqual($listener->pop(), 'onUpdate');
$this->assertEqual($listener->pop(), 'onPreUpdate');
$this->assertEqual($listener->pop(), 'onPreSave');
$this->assertEqual($listener->pop(), 'onSave');
$this->assertEqual($listener->pop(), 'onUpdate');
$this->assertEqual($listener->pop(), 'onPreUpdate');
$this->assertEqual($listener->pop(), 'onPreSave');
$this->assertEqual($users[1]->id, 2);
$this->assertEqual($users[2]->id, 3);
$this->assertTrue($count < count($this->dbh));
$this->connection->commit();
$this->assertEqual($listener->pop(), 'onTransactionCommit');
$this->assertEqual($listener->pop(), 'onPreTransactionCommit');
}
public function testDelete() {
$count = count($this->dbh);
$listener = new Transaction_TestLogger();
$listener->clear();
$users = $this->connection->query('FROM User');
$users->getTable()->getConnection()->setListener($listener);
$this->connection->beginTransaction();
$users->delete();
$this->assertEqual($listener->pop(), 'onPreDelete');
$this->assertTrue($count, count($this->dbh));
$this->connection->commit();
$this->assertTrue(($count + 1), count($this->dbh));
$this->assertEqual($listener->pop(), 'onTransactionCommit');
$this->assertEqual($listener->pop(), 'onPreTransactionCommit');
}
}
?>
<?php
class Doctrine_Relation_TestCase extends Doctrine_UnitTestCase {
public function prepareData() { }
public function prepareTables() {
$this->tables = array();
}
public function testManyToManyRelation() {
$user = new User();
// test that join table relations can be initialized even before the association have been initialized
try {
$user->Groupuser;
$this->pass();
} catch(Doctrine_Table_Exception $e) {
$this->fail();
}
$this->assertTrue($user->getTable()->getRelation('Groupuser') instanceof Doctrine_Relation_ForeignKey);
$this->assertTrue($user->getTable()->getRelation('Group') instanceof Doctrine_Relation_Association);
}
public function testOneToOneLocalKeyRelation() {
$user = new User();
$this->assertTrue($user->getTable()->getRelation('Email') instanceof Doctrine_Relation_LocalKey);
}
public function testOneToOneForeignKeyRelation() {
$user = new User();
$this->assertTrue($user->getTable()->getRelation('Account') instanceof Doctrine_Relation_ForeignKey);
}
public function testOneToManyForeignKeyRelation() {
$user = new User();
$this->assertTrue($user->getTable()->getRelation('Phonenumber') instanceof Doctrine_Relation_ForeignKey);
}
}
......@@ -33,12 +33,15 @@ require_once("ImportTestCase.php");
require_once("BooleanTestCase.php");
require_once("EnumTestCase.php");
require_once("RelationAccessTestCase.php");
require_once("RelationTestCase.php");
require_once("DataDictSqliteTestCase.php");
error_reporting(E_ALL);
$test = new GroupTest("Doctrine Framework Unit Tests");
$test->addTestCase(new Doctrine_Relation_TestCase());
/**
$test->addTestCase(new Doctrine_EventListenerTestCase());
$test->addTestCase(new Doctrine_RecordTestCase());
......@@ -98,7 +101,7 @@ $test->addTestCase(new Doctrine_BooleanTestCase());
$test->addTestCase(new Doctrine_QueryTestCase());
$test->addTestCase(new Doctrine_EventListener_Chain_TestCase());
*/
//$test->addTestCase(new Doctrine_Cache_FileTestCase());
//$test->addTestCase(new Doctrine_Cache_SqliteTestCase());
......
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