Commit 7327d3b6 authored by doctrine's avatar doctrine

Custom primary key column support

parent 1cc0c764
...@@ -59,9 +59,17 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -59,9 +59,17 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
public function getTable() { public function getTable() {
return $this->table; return $this->table;
} }
/**
* whether or not an offset batch has been expanded
* @return boolean
*/
public function isExpanded($offset) { public function isExpanded($offset) {
return isset($this->expanded[$offset]); return isset($this->expanded[$offset]);
} }
/**
* whether or not this collection is expandable
* @return boolean
*/
public function isExpandable() { public function isExpandable() {
return $this->expandable; return $this->expandable;
} }
...@@ -175,7 +183,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -175,7 +183,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
$ids = $this->getPrimaryKeys(); $ids = $this->getPrimaryKeys();
if( ! empty($ids)) { if( ! empty($ids)) {
$where[] = "id NOT IN (".substr(str_repeat("?, ",count($ids)),0,-2).")"; $where[] = $this->table->getIdentifier()." NOT IN (".substr(str_repeat("?, ",count($ids)),0,-2).")";
$params = array_merge($params,$ids); $params = array_merge($params,$ids);
} }
...@@ -191,7 +199,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -191,7 +199,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
$table = $fk->getTable(); $table = $fk->getTable();
$graph = new Doctrine_DQL_Parser($table->getSession()); $graph = new Doctrine_DQL_Parser($table->getSession());
$q = "FROM ".$table->getComponentName()." WHERE ".$table->getComponentName().".id IN ($query)"; $q = "FROM ".$table->getComponentName()." WHERE ".$table->getComponentName().".".$table->getIdentifier()." IN ($query)";
} }
} }
...@@ -291,9 +299,11 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -291,9 +299,11 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
*/ */
public function getPrimaryKeys() { public function getPrimaryKeys() {
$list = array(); $list = array();
$name = $this->table->getIdentifier();
foreach($this->data as $record): foreach($this->data as $record):
if(is_array($record) && isset($record['id'])) { if(is_array($record) && isset($record[$name])) {
$list[] = $record['id']; $list[] = $record[$name];
} else { } else {
$list[] = $record->getID(); $list[] = $record->getID();
} }
......
...@@ -53,13 +53,14 @@ class Doctrine_Collection_Batch extends Doctrine_Collection { ...@@ -53,13 +53,14 @@ class Doctrine_Collection_Batch extends Doctrine_Collection {
return false; return false;
$id = $record->getID(); $id = $record->getID();
$identifier = $this->table->getIdentifier();
foreach($this->data as $key => $v) { foreach($this->data as $key => $v) {
if(is_object($v)) { if(is_object($v)) {
if($v->getID() == $id) if($v->getID() == $id)
break; break;
} elseif(is_array($v["id"])) { } elseif(is_array($v[$identifier])) {
if($v["id"] == $id) if($v[$identifier] == $id)
break; break;
} }
} }
...@@ -77,7 +78,7 @@ class Doctrine_Collection_Batch extends Doctrine_Collection { ...@@ -77,7 +78,7 @@ class Doctrine_Collection_Batch extends Doctrine_Collection {
if(is_object($this->data[$i])) if(is_object($this->data[$i]))
$id = $this->data[$i]->getID(); $id = $this->data[$i]->getID();
elseif(is_array($this->data[$i])) elseif(is_array($this->data[$i]))
$id = $this->data[$i]["id"]; $id = $this->data[$i][$identifier];
$a[$i] = $id; $a[$i] = $id;
...@@ -87,11 +88,10 @@ class Doctrine_Collection_Batch extends Doctrine_Collection { ...@@ -87,11 +88,10 @@ class Doctrine_Collection_Batch extends Doctrine_Collection {
$pk = $this->table->getPrimaryKeys(); $pk = $this->table->getPrimaryKeys();
$query = $this->table->getQuery()." WHERE "; $query = $this->table->getQuery()." WHERE ";
$query .= ($c > 1)?$pk[0]." IN (":$pk[0]." = "; $query .= ($c > 1)?$identifier." IN (":$pk[0]." = ";
$query .= substr(str_repeat("?, ",count($a)),0,-2); $query .= substr(str_repeat("?, ",count($a)),0,-2);
$query .= ($c > 1)?") ORDER BY ".$pk[0]." ASC":""; $query .= ($c > 1)?") ORDER BY ".$pk[0]." ASC":"";
$stmt = $this->table->getSession()->execute($query,array_values($a)); $stmt = $this->table->getSession()->execute($query,array_values($a));
foreach($a as $k => $id) { foreach($a as $k => $id) {
......
...@@ -348,7 +348,7 @@ class Doctrine_DQL_Parser { ...@@ -348,7 +348,7 @@ class Doctrine_DQL_Parser {
* remove duplicated data rows and map data into objects * remove duplicated data rows and map data into objects
*/ */
foreach($data as $key => $row): foreach($data as $key => $row):
if(empty($row) || empty($row['id'])) if(empty($row))
continue; continue;
$key = ucwords($key); $key = ucwords($key);
...@@ -725,23 +725,24 @@ class Doctrine_DQL_Parser { ...@@ -725,23 +725,24 @@ class Doctrine_DQL_Parser {
$reference = implode(".",$a); $reference = implode(".",$a);
$objTable = $this->session->getTable(end($a)); $objTable = $this->session->getTable(end($a));
$where = $objTable->getTableName().".".$field." ".$operator." ".$value; $where = $objTable->getTableName().".".$field." ".$operator." ".$value;
if(count($a) > 1 && isset($a[1])) { if(count($a) > 1 && isset($a[1])) {
$root = $a[0]; $root = $a[0];
$fk = $this->tnames[$root]->getForeignKey($a[1]); $fk = $this->tnames[$root]->getForeignKey($a[1]);
if($fk instanceof Doctrine_Association) { if($fk instanceof Doctrine_Association) {
$asf = $fk->getAssociationFactory(); $asf = $fk->getAssociationFactory();
switch($fk->getType()): switch($fk->getType()):
case Doctrine_Table::ONE_AGGREGATE: case Doctrine_Relation::ONE_AGGREGATE:
case Doctrine_Table::ONE_COMPOSITE: case Doctrine_Relation::ONE_COMPOSITE:
break; break;
case Doctrine_Table::MANY_AGGREGATE: case Doctrine_Relation::MANY_AGGREGATE:
case Doctrine_Table::MANY_COMPOSITE: case Doctrine_Relation::MANY_COMPOSITE:
$b = array_shift($a); $b = array_shift($a);
$b = array_shift($a); $b = array_shift($a);
$graph = new Doctrine_DQL_Parser($this->session); $graph = new Doctrine_DQL_Parser($this->session);
$graph->parseQuery("FROM $b-l WHERE $where"); $graph->parseQuery("FROM $b-l WHERE $where");
$where = $this->tnames[$root]->getTableName().".id IN (SELECT ".$fk->getLocal()." FROM ".$asf->getTableName()." WHERE ".$fk->getForeign()." IN (".$graph->getQuery()."))"; $where = $this->tnames[$root]->getTableName().".".$this->tnames[$root]->getIdentifier()." IN (SELECT ".$fk->getLocal()." FROM ".$asf->getTableName()." WHERE ".$fk->getForeign()." IN (".$graph->getQuery()."))";
break; break;
endswitch; endswitch;
} else } else
...@@ -782,15 +783,15 @@ class Doctrine_DQL_Parser { ...@@ -782,15 +783,15 @@ class Doctrine_DQL_Parser {
if($fk instanceof Doctrine_ForeignKey || if($fk instanceof Doctrine_ForeignKey ||
$fk instanceof Doctrine_LocalKey) { $fk instanceof Doctrine_LocalKey) {
switch($fk->getType()): switch($fk->getType()):
case Doctrine_Table::ONE_AGGREGATE: case Doctrine_Relation::ONE_AGGREGATE:
case Doctrine_Table::ONE_COMPOSITE: case Doctrine_Relation::ONE_COMPOSITE:
$this->where[] = "(".$tname.".".$fk->getLocal()." = ".$tname2.".".$fk->getForeign().")"; $this->where[] = "(".$tname.".".$fk->getLocal()." = ".$tname2.".".$fk->getForeign().")";
$this->from[$tname] = true; $this->from[$tname] = true;
$this->from[$tname2] = true; $this->from[$tname2] = true;
break; break;
case Doctrine_Table::MANY_AGGREGATE: case Doctrine_Relation::MANY_AGGREGATE:
case Doctrine_Table::MANY_COMPOSITE: case Doctrine_Relation::MANY_COMPOSITE:
$this->join[$tname] = "LEFT JOIN ".$tname2." ON ".$tname.".".$fk->getLocal()." = ".$tname2.".".$fk->getForeign(); $this->join[$tname] = "LEFT JOIN ".$tname2." ON ".$tname.".".$fk->getLocal()." = ".$tname2.".".$fk->getForeign();
$this->joined[] = $tname2; $this->joined[] = $tname2;
$this->from[$tname] = true; $this->from[$tname] = true;
...@@ -800,12 +801,12 @@ class Doctrine_DQL_Parser { ...@@ -800,12 +801,12 @@ class Doctrine_DQL_Parser {
$asf = $fk->getAssociationFactory(); $asf = $fk->getAssociationFactory();
switch($fk->getType()): switch($fk->getType()):
case Doctrine_Table::ONE_AGGREGATE: case Doctrine_Relation::ONE_AGGREGATE:
case Doctrine_Table::ONE_COMPOSITE: case Doctrine_Relation::ONE_COMPOSITE:
break; break;
case Doctrine_Table::MANY_AGGREGATE: case Doctrine_Relation::MANY_AGGREGATE:
case Doctrine_Table::MANY_COMPOSITE: case Doctrine_Relation::MANY_COMPOSITE:
//$this->addWhere("SELECT ".$fk->getLocal()." FROM ".$asf->getTableName()." WHERE ".$fk->getForeign()." IN (SELECT ".$fk->getTable()->getComponentName().")"); //$this->addWhere("SELECT ".$fk->getLocal()." FROM ".$asf->getTableName()." WHERE ".$fk->getForeign()." IN (SELECT ".$fk->getTable()->getComponentName().")");
$this->from[$tname] = true; $this->from[$tname] = true;
......
...@@ -16,7 +16,7 @@ class Doctrine_DataDict { ...@@ -16,7 +16,7 @@ class Doctrine_DataDict {
public function createTable($tablename, $columns) { public function createTable($tablename, $columns) {
foreach($columns as $name => $args) { foreach($columns as $name => $args) {
$r[] = $name." ".$this->getADOType($args[0],$args[1])." ".$args[2]; $r[] = $name." ".$this->getADOType($args[0],$args[1])." ".str_replace("|"," ",$args[2]);
} }
......
<?php
class Doctrine_Identifier {
/**
* constant for unique identifier
*/
const UNIQUE = 0;
/**
* constant for auto_increment identifier
*/
const AUTO_INCREMENT = 1;
/**
* constant for sequence identifier
*/
const SEQUENCE = 2;
/**
* constant for normal identifier
*/
const NORMAL = 3;
}
?>
...@@ -6,7 +6,9 @@ require_once("EventListener.class.php"); ...@@ -6,7 +6,9 @@ require_once("EventListener.class.php");
* @package Doctrine ORM * @package Doctrine ORM
* @url www.phpdoctrine.com * @url www.phpdoctrine.com
* @license LGPL * @license LGPL
* @version 1.0 alpha *
* Doctrine_Manager is the base component of all doctrine based projects.
* It opens and keeps track of all sessions (database connections).
*/ */
class Doctrine_Manager extends Doctrine_Configurable implements Countable, IteratorAggregate { class Doctrine_Manager extends Doctrine_Configurable implements Countable, IteratorAggregate {
/** /**
...@@ -26,7 +28,6 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera ...@@ -26,7 +28,6 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
*/ */
private $root; private $root;
/** /**
* constructor * constructor
*/ */
...@@ -63,13 +64,16 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera ...@@ -63,13 +64,16 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
} }
} }
/** /**
* @return string the root directory of Doctrine * returns the root directory of Doctrine
* @return string
*/ */
final public function getRoot() { final public function getRoot() {
return $this->root; return $this->root;
} }
/** /**
* getInstance this class uses the singleton pattern * getInstance
* this class uses the singleton pattern
* @return Doctrine_Manager
*/ */
final public static function getInstance() { final public static function getInstance() {
static $instance; static $instance;
...@@ -80,10 +84,12 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera ...@@ -80,10 +84,12 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
} }
/** /**
* openSession open a new session and save it to Doctrine_Manager->sessions * openSession
* opens a new session and saves it to Doctrine_Manager->sessions
*
* @param PDO $pdo PDO database driver * @param PDO $pdo PDO database driver
* @param string $name name of the session, if empty numeric key is used * @param string $name name of the session, if empty numeric key is used
* @return Doctrine_Session the opened session object * @return Doctrine_Session
*/ */
final public function openSession(PDO $pdo, $name = null) { final public function openSession(PDO $pdo, $name = null) {
// initialize the default attributes // initialize the default attributes
...@@ -178,6 +184,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera ...@@ -178,6 +184,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
} }
/** /**
* getIterator * getIterator
* returns an ArrayIterator that iterates through open sessions
* @return ArrayIterator * @return ArrayIterator
*/ */
public function getIterator() { public function getIterator() {
...@@ -185,7 +192,9 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera ...@@ -185,7 +192,9 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
} }
/** /**
* getCurrentSession * getCurrentSession
* @return object Doctrine_Session * returns the current session
* @throws Doctrine_Session_Exception if there are no open sessions
* @return Doctrine_Session
*/ */
final public function getCurrentSession() { final public function getCurrentSession() {
$i = $this->currIndex; $i = $this->currIndex;
...@@ -196,6 +205,8 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera ...@@ -196,6 +205,8 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
} }
/** /**
* __toString * __toString
* returns a string representation of this object
* @return string
*/ */
public function __toString() { public function __toString() {
$r[] = "<pre>"; $r[] = "<pre>";
......
This diff is collapsed.
...@@ -10,6 +10,28 @@ ...@@ -10,6 +10,28 @@
* *
*/ */
class Doctrine_Relation { class Doctrine_Relation {
/**
* RELATION CONSTANTS
*/
/**
* constant for ONE_TO_ONE and MANY_TO_ONE aggregate relationships
*/
const ONE_AGGREGATE = 0;
/**
* constant for ONE_TO_ONE and MANY_TO_ONE composite relationships
*/
const ONE_COMPOSITE = 1;
/**
* constant for MANY_TO_MANY and ONE_TO_MANY aggregate relationships
*/
const MANY_AGGREGATE = 2;
/**
* constant for MANY_TO_MANY and ONE_TO_MANY composite relationships
*/
const MANY_COMPOSITE = 3;
/** /**
* @var Doctrine_Table $table foreign factory * @var Doctrine_Table $table foreign factory
*/ */
...@@ -39,7 +61,6 @@ class Doctrine_Relation { ...@@ -39,7 +61,6 @@ class Doctrine_Relation {
$this->type = $type; $this->type = $type;
} }
/** /**
* @see Doctrine_Table::BIND_ONE, Doctrine_Table::BIND_MANY
* @return integer bind type 1 or 0 * @return integer bind type 1 or 0
*/ */
public function getType() { public function getType() {
......
...@@ -299,7 +299,8 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab ...@@ -299,7 +299,8 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
} }
} }
/** /**
* clear -- clears the whole registry * clear
* clears the whole registry
* @return void * @return void
*/ */
public function clear() { public function clear() {
...@@ -427,11 +428,11 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab ...@@ -427,11 +428,11 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
$increment = false; $increment = false;
$id = null; $id = null;
$keys = $table->getPrimaryKeys(); $keys = $table->getPrimaryKeys();
if(count($keys) == 1 && $keys[0] == "id") { if(count($keys) == 1 && $keys[0] == $table->getIdentifier()) {
// record uses auto_increment column // record uses auto_increment column
$sql = "SELECT MAX(id) FROM ".$record->getTable()->getTableName(); $sql = "SELECT MAX(".$table->getIdentifier().") FROM ".$record->getTable()->getTableName();
$stmt = $this->dbh->query($sql); $stmt = $this->dbh->query($sql);
$data = $stmt->fetch(PDO::FETCH_NUM); $data = $stmt->fetch(PDO::FETCH_NUM);
$id = $data[0]; $id = $data[0];
...@@ -474,10 +475,10 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab ...@@ -474,10 +475,10 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
$keys = $table->getPrimaryKeys(); $keys = $table->getPrimaryKeys();
$tablename = $table->getTableName(); $tablename = $table->getTableName();
if(count($keys) == 1 && $keys[0] == "id") { if(count($keys) == 1 && $keys[0] == $table->getIdentifier()) {
// record uses auto_increment column // record uses auto_increment column
$sql = "SELECT MAX(id) FROM ".$tablename; $sql = "SELECT MAX(".$table->getIdentifier().") FROM ".$tablename;
$stmt = $this->dbh->query($sql); $stmt = $this->dbh->query($sql);
$data = $stmt->fetch(PDO::FETCH_NUM); $data = $stmt->fetch(PDO::FETCH_NUM);
$values[$tablename] = $data[0]; $values[$tablename] = $data[0];
...@@ -527,8 +528,10 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab ...@@ -527,8 +528,10 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
$record->setID(null); $record->setID(null);
} }
if($record instanceof Doctrine_Record) { if($record instanceof Doctrine_Record) {
$table = $record->getTable();
$params = substr(str_repeat("?, ",count($ids)),0,-2); $params = substr(str_repeat("?, ",count($ids)),0,-2);
$query = "DELETE FROM ".$record->getTable()->getTableName()." WHERE id IN(".$params.")"; $query = "DELETE FROM ".$record->getTable()->getTableName()." WHERE ".$table->getIdentifier()." IN(".$params.")";
$this->execute($query,$ids); $this->execute($query,$ids);
$record->getTable()->getCache()->deleteMultiple($ids); $record->getTable()->getCache()->deleteMultiple($ids);
...@@ -592,8 +595,8 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab ...@@ -592,8 +595,8 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
if($fk instanceof Doctrine_ForeignKey || if($fk instanceof Doctrine_ForeignKey ||
$fk instanceof Doctrine_LocalKey) { $fk instanceof Doctrine_LocalKey) {
switch($fk->getType()): switch($fk->getType()):
case Doctrine_Table::ONE_COMPOSITE: case Doctrine_Relation::ONE_COMPOSITE:
case Doctrine_Table::MANY_COMPOSITE: case Doctrine_Relation::MANY_COMPOSITE:
$local = $fk->getLocal(); $local = $fk->getLocal();
$foreign = $fk->getForeign(); $foreign = $fk->getForeign();
...@@ -630,11 +633,10 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab ...@@ -630,11 +633,10 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
*/ */
private function update(Doctrine_Record $record) { private function update(Doctrine_Record $record) {
$array = $record->getModified(); $array = $record->getModified();
if(empty($array)) if(empty($array))
return false; return false;
$set = array(); $set = array();
foreach($array as $name => $value): foreach($array as $name => $value):
$set[] = $name." = ?"; $set[] = $name." = ?";
...@@ -668,26 +670,16 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab ...@@ -668,26 +670,16 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
* @return boolean * @return boolean
*/ */
private function insert(Doctrine_Record $record,$id = null) { private function insert(Doctrine_Record $record,$id = null) {
$array = $record->getModified(); $array = $record->getPrepared();
if(empty($array)) if(empty($array))
return false; return false;
foreach($record->getTable()->getInheritanceMap() as $k=>$v):
$array[$k] = $v;
endforeach;
$seq = $record->getTable()->getSequenceName(); $seq = $record->getTable()->getSequenceName();
if( ! empty($seq)) { if( ! empty($seq)) {
$id = $this->getNextID($seq); $id = $this->getNextID($seq);
$array["id"] = $id; $name = $record->getTable()->getIdentifier();
} $array[$name] = $id;
foreach($array as $k => $value) {
if($value instanceof Doctrine_Record) {
$array[$k] = $value->getID();
$record->set($k,$value->getID());
}
} }
if(isset($this->validator)) { if(isset($this->validator)) {
...@@ -717,8 +709,8 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab ...@@ -717,8 +709,8 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
final public function deleteComposites(Doctrine_Record $record) { final public function deleteComposites(Doctrine_Record $record) {
foreach($record->getTable()->getForeignKeys() as $fk) { foreach($record->getTable()->getForeignKeys() as $fk) {
switch($fk->getType()): switch($fk->getType()):
case Doctrine_Table::ONE_COMPOSITE: case Doctrine_Relation::ONE_COMPOSITE:
case Doctrine_Table::MANY_COMPOSITE: case Doctrine_Relation::MANY_COMPOSITE:
$obj = $record->get($fk->getTable()->getComponentName()); $obj = $record->get($fk->getTable()->getComponentName());
$obj->delete(); $obj->delete();
break; break;
......
...@@ -48,10 +48,10 @@ class Doctrine_Session_Mysql extends Doctrine_Session_Common { ...@@ -48,10 +48,10 @@ class Doctrine_Session_Mysql extends Doctrine_Session_Common {
$keys = $table->getPrimaryKeys(); $keys = $table->getPrimaryKeys();
$tablename = $table->getTableName(); $tablename = $table->getTableName();
if(count($keys) == 1 && $keys[0] == "id") { if(count($keys) == 1 && $keys[0] == $table->getIdentifier()) {
// record uses auto_increment column // record uses auto_increment column
$sql[] = "SELECT MAX(".$tablename.".id) as $tablename FROM ".$tablename; $sql[] = "SELECT MAX(".$tablename.".".$table->getIdentifier().") as $tablename FROM ".$tablename;
$values[$tablename] = 0; $values[$tablename] = 0;
$array[] = $tablename; $array[] = $tablename;
} }
...@@ -85,11 +85,11 @@ class Doctrine_Session_Mysql extends Doctrine_Session_Common { ...@@ -85,11 +85,11 @@ class Doctrine_Session_Mysql extends Doctrine_Session_Common {
$increment = false; $increment = false;
$id = null; $id = null;
$keys = $table->getPrimaryKeys(); $keys = $table->getPrimaryKeys();
if(count($keys) == 1 && $keys[0] == "id") { if(count($keys) == 1 && $keys[0] == $table->getIdentifier()) {
// record uses auto_increment column // record uses auto_increment column
$sql = "SELECT MAX(id) FROM ".$record->getTable()->getTableName(); $sql = "SELECT MAX(".$table->getIdentifier().") FROM ".$record->getTable()->getTableName();
$stmt = $this->getDBH()->query($sql); $stmt = $this->getDBH()->query($sql);
$data = $stmt->fetch(PDO::FETCH_NUM); $data = $stmt->fetch(PDO::FETCH_NUM);
$id = $data[0]; $id = $data[0];
...@@ -111,19 +111,7 @@ class Doctrine_Session_Mysql extends Doctrine_Session_Common { ...@@ -111,19 +111,7 @@ class Doctrine_Session_Mysql extends Doctrine_Session_Common {
$id++; $id++;
} }
$array = $record->getPrepared();
$array = $record->getModified();
foreach($record->getTable()->getInheritanceMap() as $k=>$v):
$array[$k] = $v;
endforeach;
foreach($array as $k => $value) {
if($value instanceof Doctrine_Record) {
$array[$k] = $value->getID();
$record->set($k,$value->getID());
}
}
if(isset($this->validator)) { if(isset($this->validator)) {
if( ! $this->validator->validateRecord($record)) { if( ! $this->validator->validateRecord($record)) {
......
...@@ -12,23 +12,6 @@ require_once("Configurable.class.php"); ...@@ -12,23 +12,6 @@ require_once("Configurable.class.php");
* @version 1.0 alpha * @version 1.0 alpha
*/ */
class Doctrine_Table extends Doctrine_Configurable { class Doctrine_Table extends Doctrine_Configurable {
/**
* constant for ONE_TO_ONE and MANY_TO_ONE aggregate relationships
*/
const ONE_AGGREGATE = 0;
/**
* constant for ONE_TO_ONE and MANY_TO_ONE composite relationships
*/
const ONE_COMPOSITE = 1;
/**
* constant for MANY_TO_MANY and ONE_TO_MANY aggregate relationships
*/
const MANY_AGGREGATE = 2;
/**
* constant for MANY_TO_MANY and ONE_TO_MANY composite relationships
*/
const MANY_COMPOSITE = 3;
/** /**
* @var boolean $isNewEntry whether ot not this table created a new record or not, used only internally * @var boolean $isNewEntry whether ot not this table created a new record or not, used only internally
*/ */
...@@ -46,9 +29,13 @@ class Doctrine_Table extends Doctrine_Configurable { ...@@ -46,9 +29,13 @@ class Doctrine_Table extends Doctrine_Configurable {
*/ */
private $primaryKeys = array(); private $primaryKeys = array();
/** /**
* @var integer $primaryType * @var mixed $identifier
*/
private $identifier;
/**
* @var integer $identifierType
*/ */
private $primaryType; private $identifierType;
/** /**
* @var string $query cached simple query * @var string $query cached simple query
*/ */
...@@ -117,7 +104,7 @@ class Doctrine_Table extends Doctrine_Configurable { ...@@ -117,7 +104,7 @@ class Doctrine_Table extends Doctrine_Configurable {
throw new Doctrine_Exception("Couldn't find class $name"); throw new Doctrine_Exception("Couldn't find class $name");
$record = new $name($this); $record = new $name($this);
$record->setUp();
$names = array(); $names = array();
...@@ -151,14 +138,44 @@ class Doctrine_Table extends Doctrine_Configurable { ...@@ -151,14 +138,44 @@ class Doctrine_Table extends Doctrine_Configurable {
switch(count($this->primaryKeys)): switch(count($this->primaryKeys)):
case 0: case 0:
$this->columns = array_merge(array("id" => array("integer",11,"AUTOINCREMENT PRIMARY")), $this->columns); $this->columns = array_merge(array("id" => array("integer",11,"autoincrement|primary")), $this->columns);
$this->primaryKeys[] = "id"; $this->primaryKeys[] = "id";
$this->identifier = "id";
$this->identifierType = Doctrine_Identifier::AUTO_INCREMENT;
break; break;
case 1: default:
foreach($this->primaryKeys as $pk) {
$o = $this->columns[$pk][2];
$e = explode("|",$o);
$found = false;
foreach($e as $option) {
if($found)
break; break;
default:
$e2 = explode(":",$option);
switch(strtolower($e2[0])):
case "unique":
$this->identifierType = Doctrine_Identifier::UNIQUE;
$found = true;
break;
case "autoincrement":
$this->identifierType = Doctrine_Identifier::AUTO_INCREMENT;
$found = true;
break;
case "seq":
$this->identifierType = Doctrine_Identifier::SEQUENCE;
$found = true;
break;
endswitch;
}
if( ! isset($this->identifierType))
$this->identifierType = Doctrine_Identifier::NORMAL;
$this->identifier = $pk;
}
endswitch; endswitch;
if($this->getAttribute(Doctrine::ATTR_CREATE_TABLES)) { if($this->getAttribute(Doctrine::ATTR_CREATE_TABLES)) {
...@@ -171,6 +188,8 @@ class Doctrine_Table extends Doctrine_Configurable { ...@@ -171,6 +188,8 @@ class Doctrine_Table extends Doctrine_Configurable {
throw new Doctrine_Exception("Class '$name' has no table definition."); throw new Doctrine_Exception("Class '$name' has no table definition.");
} }
$record->setUp();
// save parents // save parents
array_pop($names); array_pop($names);
$this->parents = $names; $this->parents = $names;
...@@ -216,6 +235,18 @@ class Doctrine_Table extends Doctrine_Configurable { ...@@ -216,6 +235,18 @@ class Doctrine_Table extends Doctrine_Configurable {
$this->primaryKeys[] = $name; $this->primaryKeys[] = $name;
} }
} }
/**
* @return mixed
*/
final public function getIdentifier() {
return $this->identifier;
}
/**
* @return integer
*/
final public function getIdentifierType() {
return $this->identifierType;
}
/** /**
* hasColumn * hasColumn
* @return boolean * @return boolean
...@@ -274,8 +305,8 @@ class Doctrine_Table extends Doctrine_Configurable { ...@@ -274,8 +305,8 @@ class Doctrine_Table extends Doctrine_Configurable {
try { try {
$fk = $this->getForeignKey($k); $fk = $this->getForeignKey($k);
switch($fk->getType()): switch($fk->getType()):
case Doctrine_Table::ONE_COMPOSITE: case Doctrine_Relation::ONE_COMPOSITE:
case Doctrine_Table::MANY_COMPOSITE: case Doctrine_Relation::MANY_COMPOSITE:
$n = $fk->getTable()->getComponentName(); $n = $fk->getTable()->getComponentName();
$array[] = $name.".".$n; $array[] = $name.".".$n;
$e = $fk->getTable()->getCompositePaths(); $e = $fk->getTable()->getCompositePaths();
...@@ -323,7 +354,9 @@ class Doctrine_Table extends Doctrine_Configurable { ...@@ -323,7 +354,9 @@ class Doctrine_Table extends Doctrine_Configurable {
// is reference table used? // is reference table used?
if($e[0] != $name && $e[0] == $this->name) if($e[0] != $name && $e[0] == $this->name)
$this->bound[$name] = array($field,Doctrine_Table::MANY_COMPOSITE); $this->bound[$name] = array($field,Doctrine_Relation::MANY_COMPOSITE);
$this->bound[$name] = array($field,$type,$localKey); $this->bound[$name] = array($field,$type,$localKey);
} }
...@@ -361,31 +394,42 @@ class Doctrine_Table extends Doctrine_Configurable { ...@@ -361,31 +394,42 @@ class Doctrine_Table extends Doctrine_Configurable {
return $this->foreignKeys[$name]; return $this->foreignKeys[$name];
if(isset($this->bound[$name])) { if(isset($this->bound[$name])) {
$type = $this->bound[$name][1];
$field = $this->bound[$name][0]; $field = $this->bound[$name][0];
$type = $this->bound[$name][1];
$local = $this->bound[$name][2];
$e = explode(".",$field); $e = explode(".",$field);
$objTable = $this->session->getTable($name); $objTable = $this->session->getTable($name);
switch($e[0]): switch($e[0]):
case $name: case $name:
if( ! isset($local))
$local = $this->identifier;
// ONE-TO-MANY or ONE-TO-ONE // ONE-TO-MANY or ONE-TO-ONE
$foreignKey = new Doctrine_ForeignKey($objTable,$this->bound[$name][2],$e[1],$type); $foreignKey = new Doctrine_ForeignKey($objTable,$local,$e[1],$type);
break; break;
case $this->name: case $this->name:
// ONE-TO-ONE // ONE-TO-ONE
if($type <= Doctrine_Table::ONE_COMPOSITE) if($type <= Doctrine_Relation::ONE_COMPOSITE) {
$foreignKey = new Doctrine_LocalKey($objTable,$e[1],$this->bound[$name][2],$type); if( ! isset($local))
else $local = $objTable->getIdentifier();
$foreignKey = new Doctrine_LocalKey($objTable,$e[1],$local,$type);
} else
throw new Doctrine_Mapping_Exception(); throw new Doctrine_Mapping_Exception();
break; break;
default: default:
if(in_array($e[0], $this->parents)) { if(in_array($e[0], $this->parents)) {
// ONE-TO-ONE // ONE-TO-ONE
if($type <= Doctrine_Table::ONE_COMPOSITE) if($type <= Doctrine_Relation::ONE_COMPOSITE) {
$foreignKey = new Doctrine_LocalKey($objTable,$e[1],$this->bound[$name][2],$type); if( ! isset($local))
else $local = $objTable->getIdentifier();
$foreignKey = new Doctrine_LocalKey($objTable,$e[1],$local,$type);
} else
throw new Doctrine_Mapping_Exception(); throw new Doctrine_Mapping_Exception();
} else { } else {
// POSSIBLY MANY-TO-MANY // POSSIBLY MANY-TO-MANY
...@@ -400,7 +444,8 @@ class Doctrine_Table extends Doctrine_Configurable { ...@@ -400,7 +444,8 @@ class Doctrine_Table extends Doctrine_Configurable {
} }
} }
if( ! isset($local))
$local = $this->identifier;
$e2 = explode(".",$bound[0]); $e2 = explode(".",$bound[0]);
...@@ -409,7 +454,7 @@ class Doctrine_Table extends Doctrine_Configurable { ...@@ -409,7 +454,7 @@ class Doctrine_Table extends Doctrine_Configurable {
$associationTable = $this->session->getTable($e2[0]); $associationTable = $this->session->getTable($e2[0]);
$this->foreignKeys[$e2[0]] = new Doctrine_ForeignKey($associationTable,$this->bound[$name][2],$e2[1],Doctrine_Table::MANY_COMPOSITE); $this->foreignKeys[$e2[0]] = new Doctrine_ForeignKey($associationTable,$local,$e2[1],Doctrine_Relation::MANY_COMPOSITE);
$foreignKey = new Doctrine_Association($objTable,$associationTable,$e2[1],$e[1],$type); $foreignKey = new Doctrine_Association($objTable,$associationTable,$e2[1],$e[1],$type);
} }
......
<?php <?php
/**
* Doctrine_Validator
*/
class Doctrine_Validator { class Doctrine_Validator {
/**
* ERROR CONSTANTS
*/
/**
* constant for length error
*/
const ERR_LENGTH = 0; const ERR_LENGTH = 0;
/**
* constant for type error
*/
const ERR_TYPE = 1; const ERR_TYPE = 1;
/**
* constant for general validation error
*/
const ERR_VALID = 2; const ERR_VALID = 2;
/**
* constant for unique validator error
*/
const ERR_UNIQUE = 3; const ERR_UNIQUE = 3;
/** /**
...@@ -81,6 +100,7 @@ class Doctrine_Validator { ...@@ -81,6 +100,7 @@ class Doctrine_Validator {
return true; return true;
} }
/** /**
* whether or not this validator has errors
* @return boolean * @return boolean
*/ */
public function hasErrors() { public function hasErrors() {
...@@ -93,6 +113,7 @@ class Doctrine_Validator { ...@@ -93,6 +113,7 @@ class Doctrine_Validator {
return $this->stack; return $this->stack;
} }
/** /**
* returns the type of loosely typed variable
* @param mixed $var * @param mixed $var
*/ */
public static function gettype($var) { public static function gettype($var) {
......
...@@ -40,7 +40,7 @@ class Doctrine_Collection_OffsetTestCase extends Doctrine_UnitTestCase { ...@@ -40,7 +40,7 @@ class Doctrine_Collection_OffsetTestCase extends Doctrine_UnitTestCase {
$this->session->setAttribute(Doctrine::ATTR_COLL_LIMIT, 1); $this->session->setAttribute(Doctrine::ATTR_COLL_LIMIT, 1);
$users = $this->session->query("FROM User-b, User.Phonenumber-o WHERE User.id = 5"); $users = $this->session->query("FROM User-b, User.Phonenumber-o WHERE User.".$this->objTable->getIdentifier()." = 5");
$this->assertEqual(count($users), 1); $this->assertEqual(count($users), 1);
......
...@@ -37,7 +37,8 @@ class Doctrine_CollectionTestCase extends Doctrine_UnitTestCase { ...@@ -37,7 +37,8 @@ class Doctrine_CollectionTestCase extends Doctrine_UnitTestCase {
$this->assertTrue($coll[2]->getState() == Doctrine_Record::STATE_PROXY); $this->assertTrue($coll[2]->getState() == Doctrine_Record::STATE_PROXY);
$generator = new Doctrine_IndexGenerator("id");
$generator = new Doctrine_IndexGenerator($this->objTable->getIdentifier());
$coll->setGenerator($generator); $coll->setGenerator($generator);
$generator = $coll->getGenerator(); $generator = $coll->getGenerator();
$this->assertEqual($generator->getIndex($this->old), 4); $this->assertEqual($generator->getIndex($this->old), 4);
......
...@@ -168,7 +168,7 @@ class Doctrine_DQL_ParserTestCase extends Doctrine_UnitTestCase { ...@@ -168,7 +168,7 @@ class Doctrine_DQL_ParserTestCase extends Doctrine_UnitTestCase {
$this->assertEqual($users->count(),8); $this->assertEqual($users->count(),8);
$users = $graph->query("FROM User-b WHERE User.name LIKE '%Jack%'"); $users = $graph->query("FROM User-b WHERE User.name LIKE '%Jack%'");
$this->assertTrue($graph->getQuery() == "SELECT entity.id AS User__id FROM entity WHERE (entity.name LIKE '%Jack%') AND (entity.type = 0)"); $this->assertEqual($graph->getQuery(), "SELECT entity.id AS User__id FROM entity WHERE (entity.name LIKE '%Jack%') AND (entity.type = 0)");
$this->assertEqual($users->count(),0); $this->assertEqual($users->count(),0);
......
<?php
class Doctrine_IdentifierTestCase extends Doctrine_UnitTestCase {
}
?>
...@@ -129,7 +129,6 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { ...@@ -129,7 +129,6 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$e->code = 1; $e->code = 1;
// ADDING NEW RECORD // ADDING NEW RECORD
$this->assertEqual($e->code,1); $this->assertEqual($e->code,1);
$this->assertEqual($e->file_md5, md5(0)); $this->assertEqual($e->file_md5, md5(0));
$this->assertEqual($e->message, "user error"); $this->assertEqual($e->message, "user error");
......
<?php <?php
require_once("UnitTestCase.class.php"); require_once("UnitTestCase.class.php");
class Doctrine_TableTestCase extends Doctrine_UnitTestCase { class Doctrine_TableTestCase extends Doctrine_UnitTestCase {
public function testGetIdentifier() {
$table = $this->session->getTable("User");
}
public function testGetForeignKey() { public function testGetForeignKey() {
$fk = $this->objTable->getForeignKey("Group"); $fk = $this->objTable->getForeignKey("Group");
$this->assertTrue($fk instanceof Doctrine_Association); $this->assertTrue($fk instanceof Doctrine_Association);
$this->assertTrue($fk->getTable() instanceof Doctrine_Table); $this->assertTrue($fk->getTable() instanceof Doctrine_Table);
$this->assertTrue($fk->getType() == Doctrine_Table::MANY_AGGREGATE); $this->assertTrue($fk->getType() == Doctrine_Relation::MANY_AGGREGATE);
$this->assertTrue($fk->getLocal() == "user_id"); $this->assertTrue($fk->getLocal() == "user_id");
$this->assertTrue($fk->getForeign() == "group_id"); $this->assertTrue($fk->getForeign() == "group_id");
$fk = $this->objTable->getForeignKey("Email"); $fk = $this->objTable->getForeignKey("Email");
$this->assertTrue($fk instanceof Doctrine_LocalKey); $this->assertTrue($fk instanceof Doctrine_LocalKey);
$this->assertTrue($fk->getTable() instanceof Doctrine_Table); $this->assertTrue($fk->getTable() instanceof Doctrine_Table);
$this->assertTrue($fk->getType() == Doctrine_Table::ONE_COMPOSITE); $this->assertTrue($fk->getType() == Doctrine_Relation::ONE_COMPOSITE);
$this->assertTrue($fk->getLocal() == "email_id"); $this->assertTrue($fk->getLocal() == "email_id");
$this->assertTrue($fk->getForeign() == "id"); $this->assertTrue($fk->getForeign() == $fk->getTable()->getIdentifier());
$fk = $this->objTable->getForeignKey("Phonenumber"); $fk = $this->objTable->getForeignKey("Phonenumber");
$this->assertTrue($fk instanceof Doctrine_ForeignKey); $this->assertTrue($fk instanceof Doctrine_ForeignKey);
$this->assertTrue($fk->getTable() instanceof Doctrine_Table); $this->assertTrue($fk->getTable() instanceof Doctrine_Table);
$this->assertTrue($fk->getType() == Doctrine_Table::MANY_COMPOSITE); $this->assertTrue($fk->getType() == Doctrine_Relation::MANY_COMPOSITE);
$this->assertTrue($fk->getLocal() == "id"); $this->assertTrue($fk->getLocal() == $this->objTable->getIdentifier());
$this->assertTrue($fk->getForeign() == "entity_id"); $this->assertTrue($fk->getForeign() == "entity_id");
} }
public function testGetComponentName() { public function testGetComponentName() {
......
<?php <?php
require_once("../classes/Doctrine.class.php"); require_once("../classes/Doctrine.class.php");
/**
function __autoload($class) {
Doctrine::autoload($class);
}
*/
Doctrine::loadAll(); Doctrine::loadAll();
require_once("classes.php"); require_once("classes.php");
...@@ -38,11 +31,18 @@ class Doctrine_UnitTestCase extends UnitTestCase { ...@@ -38,11 +31,18 @@ class Doctrine_UnitTestCase extends UnitTestCase {
$this->manager = Doctrine_Manager::getInstance(); $this->manager = Doctrine_Manager::getInstance();
$this->manager->setAttribute(Doctrine::ATTR_CACHE, Doctrine::CACHE_NONE); $this->manager->setAttribute(Doctrine::ATTR_CACHE, Doctrine::CACHE_NONE);
$this->manager->setAttribute(Doctrine::ATTR_FETCHMODE, Doctrine::FETCH_IMMEDIATE); $this->manager->setAttribute(Doctrine::ATTR_FETCHMODE, Doctrine::FETCH_IMMEDIATE);
$this->tables = array("entity","email","phonenumber","groupuser","album","song","element","error","description","address","account");
$tables = $this->tables;
if($this->manager->count() > 0) { if($this->manager->count() > 0) {
$this->session = $this->manager->getSession(0); $this->session = $this->manager->getSession(0);
$this->session->clear(); $this->session->clear();
$this->dbh = $this->session->getDBH(); $this->dbh = $this->session->getDBH();
$this->listener = $this->manager->getAttribute(Doctrine::ATTR_LISTENER); $this->listener = $this->manager->getAttribute(Doctrine::ATTR_LISTENER);
} else { } else {
$this->dbh = Doctrine_DB::getConnection(); $this->dbh = Doctrine_DB::getConnection();
$this->session = $this->manager->openSession($this->dbh); $this->session = $this->manager->openSession($this->dbh);
...@@ -50,8 +50,6 @@ class Doctrine_UnitTestCase extends UnitTestCase { ...@@ -50,8 +50,6 @@ 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","account");
$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");
} }
...@@ -62,6 +60,7 @@ class Doctrine_UnitTestCase extends UnitTestCase { ...@@ -62,6 +60,7 @@ class Doctrine_UnitTestCase extends UnitTestCase {
} }
$this->objTable = $this->session->getTable("User"); $this->objTable = $this->session->getTable("User");
$this->repository = $this->objTable->getRepository(); $this->repository = $this->objTable->getRepository();
//$this->cache = $this->objTable->getCache(); //$this->cache = $this->objTable->getCache();
......
...@@ -6,6 +6,7 @@ class Entity extends Doctrine_Record { ...@@ -6,6 +6,7 @@ class Entity extends Doctrine_Record {
$this->ownsOne("Account","Account.entity_id"); $this->ownsOne("Account","Account.entity_id");
} }
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("id","integer",20,"autoincrement|primary");
$this->hasColumn("name","string",50); $this->hasColumn("name","string",50);
$this->hasColumn("loginname","string",20,"unique"); $this->hasColumn("loginname","string",20,"unique");
$this->hasColumn("password","string",16); $this->hasColumn("password","string",16);
...@@ -84,7 +85,6 @@ class Groupuser extends Doctrine_Record { ...@@ -84,7 +85,6 @@ class Groupuser extends Doctrine_Record {
$this->hasColumn("user_id","integer"); $this->hasColumn("user_id","integer");
} }
} }
class Phonenumber extends Doctrine_Record { class Phonenumber extends Doctrine_Record {
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("phonenumber","string",20); $this->hasColumn("phonenumber","string",20);
......
...@@ -23,18 +23,19 @@ error_reporting(E_ALL); ...@@ -23,18 +23,19 @@ 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_TableTestCase());
$test->addTestCase(new Doctrine_RecordTestCase());
$test->addTestCase(new Doctrine_SessionTestCase());
$test->addTestCase(new Doctrine_RecordTestCase());
$test->addTestCase(new Doctrine_ValidatorTestCase()); $test->addTestCase(new Doctrine_ValidatorTestCase());
$test->addTestCase(new Doctrine_ManagerTestCase()); $test->addTestCase(new Doctrine_ManagerTestCase());
$test->addTestCase(new Doctrine_TableTestCase());
$test->addTestCase(new Doctrine_AccessTestCase()); $test->addTestCase(new Doctrine_AccessTestCase());
...@@ -57,7 +58,6 @@ $test->addTestCase(new Doctrine_CollectionTestCase()); ...@@ -57,7 +58,6 @@ $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());
//$test->addTestCase(new Doctrine_Cache_SqliteTestCase()); //$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