Commit 02a283f5 authored by doctrine's avatar doctrine

--no commit message

--no commit message
parent bf5deba9
......@@ -240,6 +240,7 @@ final class Doctrine {
case "Table":
case "Validator":
case "Exception":
case "EventListener":
case "Session":
case "DQL":
case "Sensei":
......@@ -273,6 +274,7 @@ final class Doctrine {
if(! self::$path)
self::$path = dirname(__FILE__);
$class = self::$path.DIRECTORY_SEPARATOR.str_replace("_",DIRECTORY_SEPARATOR,$classname).".php";
if( ! file_exists($class))
......
<?php
Doctrine::autoload("EventListener");
class Doctrine_DebugMessage {
private $code;
private $object;
public function __construct($object, $code) {
$this->object = $object;
$this->code = $code;
}
final public function getCode() {
return $this->code;
}
final public function getObject() {
return $this->object;
}
}
class Doctrine_EventListener_Debugger extends Doctrine_EventListener {
const EVENT_LOAD = 1;
const EVENT_PRELOAD = 2;
const EVENT_SLEEP = 3;
const EVENT_WAKEUP = 4;
const EVENT_UPDATE = 5;
const EVENT_PREUPDATE = 6;
const EVENT_CREATE = 7;
const EVENT_PRECREATE = 8;
const EVENT_SAVE = 9;
const EVENT_PRESAVE = 10;
const EVENT_INSERT = 11;
const EVENT_PREINSERT = 12;
const EVENT_DELETE = 13;
const EVENT_PREDELETE = 14;
const EVENT_EVICT = 15;
const EVENT_PREEVICT = 16;
const EVENT_CLOSE = 17;
const EVENT_PRECLOSE = 18;
const EVENT_OPEN = 19;
const EVENT_COMMIT = 20;
const EVENT_PRECOMMIT = 21;
const EVENT_ROLLBACK = 22;
const EVENT_PREROLLBACK = 23;
const EVENT_BEGIN = 24;
const EVENT_PREBEGIN = 25;
const EVENT_COLLDELETE = 26;
const EVENT_PRECOLLDELETE = 27;
private $debug;
public function getMessages() {
return $this->debug;
}
public function onLoad(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_LOAD);
}
public function onPreLoad(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_PRELOAD);
}
public function onSleep(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_SLEEP);
}
public function onWakeUp(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_WAKEUP);
}
public function onUpdate(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_UPDATE);
}
public function onPreUpdate(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_PREUPDATE);
}
public function onCreate(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_CREATE);
}
public function onPreCreate(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_PRECREATE);
}
public function onSave(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_SAVE);
}
public function onPreSave(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_PRESAVE);
}
public function onInsert(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_INSERT);
}
public function onPreInsert(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_PREINSERT);
}
public function onDelete(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_DELETE);
}
public function onPreDelete(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_PREDELETE);
}
public function onEvict(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_EVICT);
}
public function onPreEvict(Doctrine_Record $record) {
$this->debug[] = new Doctrine_DebugMessage($record,self::EVENT_PREEVICT);
}
public function onClose(Doctrine_Session $session) {
$this->debug[] = new Doctrine_DebugMessage($session,self::EVENT_CLOSE);
}
public function onPreClose(Doctrine_Session $session) {
$this->debug[] = new Doctrine_DebugMessage($session,self::EVENT_PRECLOSE);
}
public function onOpen(Doctrine_Session $session) {
$this->debug[] = new Doctrine_DebugMessage($session,self::EVENT_OPEN);
}
public function onTransactionCommit(Doctrine_Session $session) {
$this->debug[] = new Doctrine_DebugMessage($session,self::EVENT_COMMIT);
}
public function onPreTransactionCommit(Doctrine_Session $session) {
$this->debug[] = new Doctrine_DebugMessage($session,self::EVENT_PRECOMMIT);
}
public function onTransactionRollback(Doctrine_Session $session) {
$this->debug[] = new Doctrine_DebugMessage($session,self::EVENT_ROLLBACK);
}
public function onPreTransactionRollback(Doctrine_Session $session) {
$this->debug[] = new Doctrine_DebugMessage($session,self::EVENT_PREROLLBACK);
}
public function onTransactionBegin(Doctrine_Session $session) {
$this->debug[] = new Doctrine_DebugMessage($session,self::EVENT_BEGIN);
}
public function onPreTransactionBegin(Doctrine_Session $session) {
$this->debug[] = new Doctrine_DebugMessage($session,self::EVENT_PREBEGIN);
}
public function onCollectionDelete(Doctrine_Collection $collection) {
$this->debug[] = new Doctrine_DebugMessage($collection,self::EVENT_COLLDELETE);
}
public function onPreCollectionDelete(Doctrine_Collection $collection) {
$this->debug[] = new Doctrine_DebugMessage($collection,self::EVENT_PRECOLLDELETE);
}
}
?>
......@@ -42,9 +42,24 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
const STATE_DELETED = 6;
/**
* FETCHMODE CONSTANTS
* CALLBACK CONSTANTS
*/
/**
* RAW CALLBACK
*
* when using a raw callback and the property if a record is changed using this callback the
* record state remains untouched
*/
const CALLBACK_RAW = 1;
/**
* STATE-WISE CALLBACK
*
* state-wise callback means that when callback is used and the property is changed the
* record state is also updated
*/
const CALLBACK_STATEWISE = 2;
/**
* @var object Doctrine_Table $table the factory that created this data access object
*/
......@@ -207,7 +222,15 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
if( ! isset($tmp[$name])) {
$this->data[$name] = self::$null;
} else {
switch($this->table->getTypeOf($name)):
case "array":
case "object":
if($tmp[$name] !== self::$null)
$this->data[$name] = unserialize($tmp[$name]);
break;
default:
$this->data[$name] = $tmp[$name];
endswitch;
}
}
}
......@@ -220,14 +243,16 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
switch($this->table->getIdentifierType()):
case Doctrine_Identifier::AUTO_INCREMENT:
case Doctrine_Identifier::SEQUENCE:
if($exists) {
$name = $this->table->getIdentifier();
if($exists) {
if(isset($this->data[$name]))
$this->id = $this->data[$name];
}
unset($this->data[$name]);
}
break;
case Doctrine_Identifier::COMPOSITE:
$names = $this->table->getIdentifier();
......@@ -250,7 +275,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
public function __sleep() {
$this->table->getAttribute(Doctrine::ATTR_LISTENER)->onSleep($this);
$this->table = $this->table->getComponentName();
// unset all vars that won't need to be serialized
unset($this->associations);
......@@ -259,12 +284,23 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
unset($this->originals);
unset($this->oid);
foreach($this->data as $k=>$v) {
foreach($this->data as $k => $v) {
if($v instanceof Doctrine_Record)
$this->data[$k] = array();
elseif($v === self::$null)
elseif($v === self::$null) {
unset($this->data[$k]);
} else {
switch($this->table->getTypeOf($k)):
case "array":
case "object":
$this->data[$k] = serialize($this->data[$k]);
break;
endswitch;
}
}
$this->table = $this->table->getComponentName();
return array_keys(get_object_vars($this));
}
......@@ -281,6 +317,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$sess = $manager->getCurrentSession();
$this->oid = self::$index;
self::$index++;
$this->table = $sess->getTable($name);
......@@ -289,10 +326,15 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$this->cleanData();
//unset($this->data['id']);
$exists = true;
$this->table->getAttribute(Doctrine::ATTR_LISTENER)->onWakeUp($this);
if($this->state == Doctrine_Record::STATE_TDIRTY ||
$this->state == Doctrine_Record::STATE_TCLEAN)
$exists = false;
$this->prepareIdentifiers($exists);
$this->table->getAttribute(Doctrine::ATTR_LISTENER)->onWakeUp($this);
}
/**
* addCollection
......@@ -452,8 +494,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
return $this->data[$name];
}
if($name == $this->table->getIdentifier())
if($name == $this->table->getIdentifier()) {
return $this->id;
}
if( ! isset($this->references[$name]))
$this->loadReference($name);
......@@ -656,9 +699,18 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
}
foreach($this->modified as $k => $v) {
if($this->data[$v] instanceof Doctrine_Record) {
$this->data[$v] = $this->data[$v]->getID();
$type = $this->table->getTypeOf($v);
if($type == 'array' ||
$type == 'object') {
$a[$v] = serialize($this->data[$v]);
continue;
}
if($this->data[$v] instanceof Doctrine_Record)
$this->data[$v] = $this->data[$v]->getID();
$a[$v] = $this->data[$v];
}
......@@ -829,7 +881,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* @return boolean true on success, false on failure
*/
public function delete() {
$this->table->getSession()->delete($this);
return $this->table->getSession()->delete($this);
}
/**
* returns a copy of this object
......@@ -986,7 +1038,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
try {
$this->references[$name] = $table->find($id);
} catch(Doctrine_Find_Exception $e) {
$this->references[$name] = $table->create();
//$this->set($fk->getLocal(),$this->references[$name]);
}
}
......@@ -1131,7 +1184,18 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
if(isset($a[0])) {
$column = $a[0];
$a[0] = $this->get($column);
$this->data[$column] = call_user_func_array($m, $a);
$newvalue = call_user_func_array($m, $a);
/**
if( ! isset($a[1]) || $a[1] == Doctrine_Record::CALLBACK_RAW) {
*/
$this->data[$column] = $newvalue;
/**
} elseif($a[1] == Doctrine_Record::CALLBACK_STATEWISE) {
$this->set($column, call_user_func_array($m, $a));
}
*/
}
return $this;
}
......
......@@ -640,9 +640,12 @@ class Doctrine_Table extends Doctrine_Configurable {
$query = $this->query." WHERE ".implode(" = ? AND ",$this->primaryKeys)." = ?";
$query = $this->applyInheritance($query);
$params = array_merge($id, array_values($this->inheritanceMap));
$this->data = $this->session->execute($query,$params)->fetch(PDO::FETCH_ASSOC);
$stmt = $this->session->execute($query,$params);
$this->data = $stmt->fetch(PDO::FETCH_ASSOC);
if($this->data === false)
throw new Doctrine_Find_Exception();
......@@ -819,6 +822,13 @@ class Doctrine_Table extends Doctrine_Configurable {
public function getColumnNames() {
return array_keys($this->columns);
}
/**
* getTypeOf
*/
public function getTypeOf($column) {
if(isset($this->columns[$column]))
return $this->columns[$column][0];
}
/**
* setData
* doctrine uses this function internally
......
......@@ -22,8 +22,8 @@ class Doctrine_ConfigurableTestCase extends Doctrine_UnitTestCase {
$this->manager->setAttribute(Doctrine::ATTR_BATCH_SIZE, 5);
$this->assertEqual($this->manager->getAttribute(Doctrine::ATTR_BATCH_SIZE),5);
$this->manager->setAttribute(Doctrine::ATTR_LISTENER, new Doctrine_Debugger());
$this->assertTrue($this->manager->getAttribute(Doctrine::ATTR_LISTENER) instanceof Doctrine_Debugger);
$this->manager->setAttribute(Doctrine::ATTR_LISTENER, new Doctrine_EventListener_Debugger());
$this->assertTrue($this->manager->getAttribute(Doctrine::ATTR_LISTENER) instanceof Doctrine_EventListener_Debugger);
$this->manager->setAttribute(Doctrine::ATTR_PK_COLUMNS, array("id"));
$this->assertEqual($this->manager->getAttribute(Doctrine::ATTR_PK_COLUMNS), array("id"));
......
......@@ -7,7 +7,7 @@ class Doctrine_EventListenerTestCase extends Doctrine_UnitTestCase {
$debug = $this->listener->getMessages();
$last = end($debug);
$this->assertTrue($last->getObject() instanceof Doctrine_Session);
$this->assertTrue($last->getCode() == Doctrine_Debugger::EVENT_OPEN);
$this->assertTrue($last->getCode() == Doctrine_EventListener_Debugger::EVENT_OPEN);
}
public function prepareData() { }
public function prepareTables() { }
......
......@@ -5,8 +5,108 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$this->tables[] = "Forum_Entry";
$this->tables[] = "Forum_Board";
$this->tables[] = "Forum_Thread";
$this->tables[] = "ORM_TestEntry";
$this->tables[] = "ORM_TestItem";
$this->tables[] = "Log_Status";
$this->tables[] = "Log_Entry";
$this->dbh->query("DROP TABLE IF EXISTS test_items");
$this->dbh->query("DROP TABLE IF EXISTS test_entries");
parent::prepareTables();
}
public function testOneToOneRelationFetching2() {
$status = new Log_Status();
$status->name = 'success';
$entries[0] = new Log_Entry();
$entries[0]->stamp = '2006-06-06';
$entries[0]->Log_Status = $status;
$this->assertTrue($entries[0]->Log_Status instanceof Log_Status);
$entries[1] = new Log_Entry();
$entries[1]->stamp = '2006-06-06';
$entries[1]->Log_Status = $status;
$this->session->flush();
// clear the identity maps
$entries[0]->Log_Status->getTable()->clear();
$entries[0]->getTable()->clear();
$entries = $this->session->query("FROM Log_Entry-I.Log_Status-i");
$this->assertEqual($entries->count(), 2);
$this->assertTrue($entries[0]->Log_Status instanceof Log_Status);
$this->assertEqual($entries[0]->Log_Status->name, 'success');
// the second Log_Status is fetched from identityMap
$this->assertTrue($entries[1]->Log_Status instanceof Log_Status);
$this->assertEqual($entries[1]->Log_Status->name, 'success');
// the following line is possible since doctrine uses identityMap
$this->assertEqual($entries[0]->Log_Status, $entries[1]->Log_Status);
$entries[0]->Log_Status->delete();
$this->assertEqual($entries[0]->Log_Status, $entries[1]->Log_Status);
$this->assertEqual($entries[0]->Log_Status->getState(), Doctrine_Record::STATE_TCLEAN);
// clear the identity maps
$entries[0]->Log_Status->getTable()->clear();
$entries[0]->getTable()->clear();
$entries = $this->session->query("FROM Log_Entry-I.Log_Status-i");
$this->assertEqual($entries->count(), 2);
$this->assertEqual($entries[0]->Log_Status->name, null);
$this->assertEqual($entries[1]->Log_Status->name, null);
}
public function testOneToOneRelationFetchingWithCustomTableNames() {
$entry = new ORM_TestEntry();
$entry->name = 'entry 1';
$entry->amount = '123.123';
$entry->ORM_TestItem->name = 'item 1';
$entry = new ORM_TestEntry();
$entry->name = 'entry 2';
$entry->amount = '123.123';
$entry->ORM_TestItem->name = 'item 2';
$this->session->flush();
$count = $this->dbh->count();
$entries = $this->session->query("FROM ORM_TestEntry-i.ORM_TestItem-i");
$this->assertEqual($entries->count(), 2);
$this->assertTrue($entries[0] instanceof ORM_TestEntry);
$this->assertTrue($entries[0]->getState(), Doctrine_Record::STATE_CLEAN);
$this->assertEqual($entries[0]->name, 'entry 1');
$this->assertTrue($entries[1] instanceof ORM_TestEntry);
$this->assertTrue($entries[1]->getState(), Doctrine_Record::STATE_CLEAN);
$this->assertEqual($entries[1]->name, 'entry 2');
$this->assertEqual(($count + 1), $this->dbh->count());
$this->assertTrue($entries[0]->ORM_TestItem instanceof ORM_TestItem);
$this->assertEqual($entries[0]->ORM_TestItem->getState(), Doctrine_Record::STATE_CLEAN);
$this->assertEqual($entries[0]->ORM_TestItem->name, 'item 1');
$this->assertTrue($entries[1]->ORM_TestItem instanceof ORM_TestItem);
$this->assertEqual($entries[1]->ORM_TestItem->getState(), Doctrine_Record::STATE_CLEAN);
$this->assertEqual($entries[1]->ORM_TestItem->name, 'item 2');
$this->assertEqual(($count + 1), $this->dbh->count());
}
public function testImmediateFetching() {
$count = $this->dbh->count();
$this->session->getTable('User')->clear();
......
......@@ -2,6 +2,13 @@
require_once("UnitTestCase.class.php");
class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
public function testSerialize() {
//$user = $this->session->getTable("User")->find(4);
//$str = serialize($user);
//$user2 = unserialize($str);
//$this->assertEqual($user2->getID(),$user->getID());
}
public function testCallback() {
$user = new User();
$user->name = " zYne ";
......@@ -10,6 +17,7 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$user->substr('name',0,1);
$this->assertEqual($user->name, 'z');
}
public function testJoinTableSelfReferencing() {
$e = new Entity();
$e->name = "Entity test";
......@@ -281,6 +289,8 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$this->session->flush();
$elements = $this->session->query("FROM Element-l");
$this->assertEqual($elements->count(), 5);
$e = $e->getTable()->find(1);
$this->assertEqual($e->name,"parent");
......@@ -389,7 +399,7 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$debug = $this->listener->getMessages();
$p = array_pop($debug);
$this->assertTrue($p->getObject() instanceof Doctrine_Session);
$this->assertTrue($p->getCode() == Doctrine_Debugger::EVENT_COMMIT);
$this->assertTrue($p->getCode() == Doctrine_EventListener_Debugger::EVENT_COMMIT);
$user->delete();
$this->assertTrue($user->getState() == Doctrine_Record::STATE_TCLEAN);
......@@ -409,15 +419,15 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$debug = $this->listener->getMessages();
$p = array_pop($debug);
$this->assertTrue($p->getObject() instanceof Doctrine_Session);
$this->assertTrue($p->getCode() == Doctrine_Debugger::EVENT_COMMIT);
$this->assertTrue($p->getCode() == Doctrine_EventListener_Debugger::EVENT_COMMIT);
$p = array_pop($debug);
$this->assertTrue($p->getObject() instanceof Doctrine_Record);
$this->assertTrue($p->getCode() == Doctrine_Debugger::EVENT_SAVE);
$this->assertTrue($p->getCode() == Doctrine_EventListener_Debugger::EVENT_SAVE);
$p = array_pop($debug);
$this->assertTrue($p->getObject() instanceof Doctrine_Record);
$this->assertTrue($p->getCode() == Doctrine_Debugger::EVENT_UPDATE);
$this->assertTrue($p->getCode() == Doctrine_EventListener_Debugger::EVENT_UPDATE);
}
......@@ -668,18 +678,9 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$this->assertTrue($user->Phonenumber->count() == 1);
}
public function testSerialize() {
$user = $this->session->getTable("User")->find(4);
$str = serialize($user);
$this->assertEqual(unserialize($str)->getID(),$user->getID());
}
public function testGetIterator() {
$user = $this->session->getTable("User")->find(4);
$this->assertTrue($user->getIterator() instanceof ArrayIterator);
}
}
?>
......@@ -47,7 +47,7 @@ class Doctrine_UnitTestCase extends UnitTestCase {
} else {
$this->dbh = Doctrine_DB::getConnection();
$this->session = $this->manager->openSession($this->dbh);
$this->listener = new Doctrine_Debugger();
$this->listener = new Doctrine_EventListener_Debugger();
$this->manager->setAttribute(Doctrine::ATTR_LISTENER, $this->listener);
}
......
......@@ -245,15 +245,46 @@ class App_Category extends Doctrine_Record {
}
}
/**
$apps = $con->query("FROM App.Category");
class ORM_TestEntry extends Doctrine_Record {
public function setTableDefinition() {
$this->setTableName('test_entries');
$this->hasColumn("id", "integer", 11, "autoincrement|primary");
$this->hasColumn("name", "string", 255);
$this->hasColumn("stamp", "timestamp");
$this->hasColumn("amount", "float");
$this->hasColumn("itemID", "integer");
}
public function setUp() {
$this->hasOne("ORM_TestItem", "ORM_TestEntry.itemID");
}
}
class ORM_TestItem extends Doctrine_Record {
public function setTableDefinition() {
$this->setTableName('test_items');
$this->hasColumn("id", "integer", 11, "autoincrement|primary");
$this->hasColumn("name", "string", 255);
}
public function setUp() {
$this->hasOne("ORM_TestEntry", "ORM_TestEntry.itemID");
}
}
class Log_Entry extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn("stamp", "timestamp");
$this->hasColumn("status_id", "integer");
}
public function setUp() {
$this->hasOne("Log_Status", "Log_Entry.status_id");
}
}
if (!empty($apps))
{
foreach ($apps as $app)
{
print '<p>' . $app->Category[0]->name . ' => ' . $app->name . '</p>';
class Log_Status extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn("name", "string", 255);
}
}
*/
?>
......@@ -24,6 +24,7 @@ $test = new GroupTest("Doctrine Framework Unit Tests");
//$test->addTestCase(new Sensei_UnitTestCase());
$test->addTestCase(new Doctrine_RecordTestCase());
$test->addTestCase(new Doctrine_SessionTestCase());
......
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