Commit 71338c3d authored by zYne's avatar zYne

drafting the new relation model, still a lot of work

parent b3b1f617
...@@ -79,7 +79,7 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module ...@@ -79,7 +79,7 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
// skip self-referenced relations // skip self-referenced relations
if ($name === $nm) { if ($name === $nm) {
continue; continue;
} }
if ($rel instanceof Doctrine_Relation_ForeignKey) { if ($rel instanceof Doctrine_Relation_ForeignKey) {
if ($index2 !== false) { if ($index2 !== false) {
...@@ -257,10 +257,11 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module ...@@ -257,10 +257,11 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
} }
} }
/** /**
* update
* updates the given record * updates the given record
* *
* @param Doctrine_Record $record * @param Doctrine_Record $record record to be updated
* @return boolean * @return boolean whether or not the update was successful
*/ */
public function update(Doctrine_Record $record) public function update(Doctrine_Record $record)
{ {
...@@ -301,7 +302,7 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module ...@@ -301,7 +302,7 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
. ' WHERE ' . implode(' = ? AND ', $record->getTable()->getPrimaryKeys()) . ' WHERE ' . implode(' = ? AND ', $record->getTable()->getPrimaryKeys())
. ' = ?'; . ' = ?';
$stmt = $this->conn->getDBH()->prepare($sql); $stmt = $this->conn->getDbh()->prepare($sql);
$stmt->execute($params); $stmt->execute($params);
$record->assignIdentifier(true); $record->assignIdentifier(true);
...@@ -335,7 +336,7 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module ...@@ -335,7 +336,7 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
$id = $this->conn->sequence->nextId($seq); $id = $this->conn->sequence->nextId($seq);
$name = $record->getTable()->getIdentifier(); $name = $record->getTable()->getIdentifier();
$array[$name] = $id; $array[$name] = $id;
$record->assignIdentifier($id); $record->assignIdentifier($id);
} }
......
...@@ -44,7 +44,7 @@ class Doctrine_Db_Statement implements Doctrine_Adapter_Statement_Interface ...@@ -44,7 +44,7 @@ class Doctrine_Db_Statement implements Doctrine_Adapter_Statement_Interface
$this->stmt = $stmt; $this->stmt = $stmt;
if ($stmt === false) { if ($stmt === false) {
throw new Doctrine_Db_Exception('Unknown statement object given.'); throw new Doctrine_Db_Exception('Unknown statement object given.');
} }
} }
/** /**
...@@ -205,7 +205,11 @@ class Doctrine_Db_Statement implements Doctrine_Adapter_Statement_Interface ...@@ -205,7 +205,11 @@ class Doctrine_Db_Statement implements Doctrine_Adapter_Statement_Interface
$skip = $this->adapter->getListener()->onPreExecute($event); $skip = $this->adapter->getListener()->onPreExecute($event);
if ( ! $skip) { if ( ! $skip) {
$this->stmt->execute($params); if (isset($params[0]) && is_array($params[0])) {
print_r($params);
throw new Exception();
}
$this->stmt->execute((array) $params);
$this->adapter->incrementQueryCount(); $this->adapter->incrementQueryCount();
} }
......
...@@ -66,6 +66,13 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -66,6 +66,13 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
* @var array $_enumParams an array containing the keys of the parameters that should be enumerated * @var array $_enumParams an array containing the keys of the parameters that should be enumerated
*/ */
protected $_enumParams = array(); protected $_enumParams = array();
/**
* @var array $_options an array of options
*/
protected $_options = array(
'fetchMode' => Doctrine::FETCH_RECORD,
'cacheMode' => null,
);
/** /**
* @var array $_dqlParts an array containing all DQL query parts * @var array $_dqlParts an array containing all DQL query parts
*/ */
...@@ -483,7 +490,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -483,7 +490,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
$q = 'UPDATE '; $q = 'UPDATE ';
break; break;
case self::SELECT: case self::SELECT:
$distinct = ($this->isDistinct()) ? 'DISTINCT ' : ''; $distinct = ($this->parts['distinct']) ? 'DISTINCT ' : '';
$q = 'SELECT ' . $distinct . implode(', ', $this->parts['select']) . ' FROM '; $q = 'SELECT ' . $distinct . implode(', ', $this->parts['select']) . ' FROM ';
break; break;
...@@ -565,11 +572,6 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -565,11 +572,6 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
// build the basic query // build the basic query
$str = '';
if ($this->isDistinct()) {
$str = 'DISTINCT ';
}
$q = $this->getQueryBase(); $q = $this->getQueryBase();
$q .= $this->buildFromPart(); $q .= $this->buildFromPart();
......
...@@ -901,8 +901,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -901,8 +901,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
} }
foreach ($saveLater as $fk) { foreach ($saveLater as $fk) {
$table = $fk->getTable(); $alias = $fk->getAlias();
$alias = $this->_table->getAlias($table->getComponentName());
if (isset($this->_references[$alias])) { if (isset($this->_references[$alias])) {
$obj = $this->_references[$alias]; $obj = $this->_references[$alias];
...@@ -1011,8 +1010,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1011,8 +1010,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$a[$v] = $this->_table->enumIndex($v,$this->_data[$v]); $a[$v] = $this->_table->enumIndex($v,$this->_data[$v]);
break; break;
default: default:
if ($this->_data[$v] instanceof Doctrine_Record) if ($this->_data[$v] instanceof Doctrine_Record) {
$this->_data[$v] = $this->_data[$v]->getIncremented(); $this->_data[$v] = $this->_data[$v]->getIncremented();
}
$a[$v] = $this->_data[$v]; $a[$v] = $this->_data[$v];
} }
...@@ -1273,48 +1273,61 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1273,48 +1273,61 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
} }
} }
/** /**
* ownsOne
* binds One-to-One composite relation * binds One-to-One composite relation
* *
* @param string $objTableName * @param string $componentName the name of the related component
* @param string $fkField * @param string $options relation options
* @return void * @see Doctrine_Relation::_$definition
* @return Doctrine_Record this object
*/ */
final public function ownsOne($componentName, $foreignKey, $options = null) public function ownsOne()
{ {
$this->_table->bind($componentName, $foreignKey, Doctrine_Relation::ONE_COMPOSITE, $options); $this->_table->bind(func_get_args(), Doctrine_Relation::ONE_COMPOSITE);
return $this;
} }
/** /**
* binds One-to-Many composite relation * ownsMany
* binds One-to-Many / Many-to-Many composite relation
* *
* @param string $objTableName * @param string $componentName the name of the related component
* @param string $fkField * @param string $options relation options
* @return void * @see Doctrine_Relation::_$definition
* @return Doctrine_Record this object
*/ */
final public function ownsMany($componentName, $foreignKey, $options = null) public function ownsMany()
{ {
$this->_table->bind($componentName, $foreignKey, Doctrine_Relation::MANY_COMPOSITE, $options); $this->_table->bind(func_get_args(), Doctrine_Relation::MANY_COMPOSITE);
return $this;
} }
/** /**
* hasOne
* binds One-to-One aggregate relation * binds One-to-One aggregate relation
* *
* @param string $objTableName * @param string $componentName the name of the related component
* @param string $fkField * @param string $options relation options
* @return void * @see Doctrine_Relation::_$definition
* @return Doctrine_Record this object
*/ */
final public function hasOne($componentName, $foreignKey, $options = null) public function hasOne()
{ {
$this->_table->bind($componentName, $foreignKey, Doctrine_Relation::ONE_AGGREGATE, $options); $this->_table->bind(func_get_args(), Doctrine_Relation::ONE_AGGREGATE);
return $this;
} }
/** /**
* binds One-to-Many aggregate relation * hasMany
* binds One-to-Many / Many-to-Many aggregate relation
* *
* @param string $objTableName * @param string $componentName the name of the related component
* @param string $fkField * @param string $options relation options
* @return void * @see Doctrine_Relation::_$definition
* @return Doctrine_Record this object
*/ */
final public function hasMany($componentName, $foreignKey, $options = null) public function hasMany()
{ {
$this->_table->bind($componentName, $foreignKey, Doctrine_Relation::MANY_AGGREGATE, $options); $this->_table->bind(func_get_args(), Doctrine_Relation::MANY_AGGREGATE);
return $this;
} }
/** /**
* hasColumn * hasColumn
......
...@@ -62,7 +62,7 @@ abstract class Doctrine_Relation ...@@ -62,7 +62,7 @@ abstract class Doctrine_Relation
'class' => true, 'class' => true,
'type' => true, 'type' => true,
'name' => false, 'name' => false,
'assocTable' => false, 'refTable' => false,
'onDelete' => false, 'onDelete' => false,
'onUpdate' => false, 'onUpdate' => false,
'deferred' => false, 'deferred' => false,
...@@ -80,7 +80,7 @@ abstract class Doctrine_Relation ...@@ -80,7 +80,7 @@ abstract class Doctrine_Relation
* *
* table the foreign table object * table the foreign table object
* *
* assocTable the association table object (if any) * refTable the reference table object (if any)
* *
* onDelete referential delete action * onDelete referential delete action
* *
......
...@@ -40,11 +40,11 @@ class Doctrine_Relation_Association extends Doctrine_Relation ...@@ -40,11 +40,11 @@ class Doctrine_Relation_Association extends Doctrine_Relation
*/ */
public function getAssociationFactory() public function getAssociationFactory()
{ {
return $this->definition['assocTable']; return $this->definition['refTable'];
} }
public function getAssociationTable() public function getAssociationTable()
{ {
return $this->definition['assocTable']; return $this->definition['refTable'];
} }
/** /**
* getRelationDql * getRelationDql
...@@ -54,7 +54,7 @@ class Doctrine_Relation_Association extends Doctrine_Relation ...@@ -54,7 +54,7 @@ class Doctrine_Relation_Association extends Doctrine_Relation
*/ */
public function getRelationDql($count, $context = 'record') public function getRelationDql($count, $context = 'record')
{ {
$component = $this->definition['assocTable']->getComponentName(); $component = $this->definition['refTable']->getComponentName();
switch ($context) { switch ($context) {
case "record": case "record":
$sub = 'SQL:SELECT ' . $this->definition['foreign']. $sub = 'SQL:SELECT ' . $this->definition['foreign'].
......
...@@ -43,17 +43,17 @@ class Doctrine_Relation_Association_Self extends Doctrine_Relation_Association ...@@ -43,17 +43,17 @@ class Doctrine_Relation_Association_Self extends Doctrine_Relation_Association
switch ($context) { switch ($context) {
case 'record': case 'record':
$sub = 'SELECT '.$this->definition['foreign'] $sub = 'SELECT '.$this->definition['foreign']
. ' FROM '.$this->definition['assocTable']->getTableName() . ' FROM '.$this->definition['refTable']->getTableName()
. ' WHERE '.$this->definition['local'] . ' WHERE '.$this->definition['local']
. ' = ?'; . ' = ?';
$sub2 = 'SELECT '.$this->definition['local'] $sub2 = 'SELECT '.$this->definition['local']
. ' FROM '.$this->definition['assocTable']->getTableName() . ' FROM '.$this->definition['refTable']->getTableName()
. ' WHERE '.$this->definition['foreign'] . ' WHERE '.$this->definition['foreign']
. ' = ?'; . ' = ?';
$dql = 'FROM ' . $this->definition['table']->getComponentName() $dql = 'FROM ' . $this->definition['table']->getComponentName()
. '.' . $this->definition['assocTable']->getComponentName() . '.' . $this->definition['refTable']->getComponentName()
. ' WHERE ' . $this->definition['table']->getComponentName() . ' WHERE ' . $this->definition['table']->getComponentName()
. '.' . $this->definition['table']->getIdentifier() . '.' . $this->definition['table']->getIdentifier()
. ' IN (' . $sub . ')' . ' IN (' . $sub . ')'
...@@ -63,9 +63,9 @@ class Doctrine_Relation_Association_Self extends Doctrine_Relation_Association ...@@ -63,9 +63,9 @@ class Doctrine_Relation_Association_Self extends Doctrine_Relation_Association
break; break;
case 'collection': case 'collection':
$sub = substr(str_repeat('?, ', $count),0,-2); $sub = substr(str_repeat('?, ', $count),0,-2);
$dql = 'FROM '.$this->definition['assocTable']->getComponentName() $dql = 'FROM '.$this->definition['refTable']->getComponentName()
. '.' . $this->definition['table']->getComponentName() . '.' . $this->definition['table']->getComponentName()
. ' WHERE '.$this->definition['assocTable']->getComponentName() . ' WHERE '.$this->definition['refTable']->getComponentName()
. '.' . $this->definition['local'] . ' IN (' . $sub . ')'; . '.' . $this->definition['local'] . ' IN (' . $sub . ')';
}; };
......
...@@ -45,7 +45,7 @@ class Doctrine_Relation_ForeignKey extends Doctrine_Relation ...@@ -45,7 +45,7 @@ class Doctrine_Relation_ForeignKey extends Doctrine_Relation
{ {
$id = array(); $id = array();
foreach ((array) $this->definition['local'] as $local) { foreach ((array) $this->definition['local'] as $local) {
$id = $record->get($local); $id[] = $record->get($local);
} }
if ($this->isOneToOne()) { if ($this->isOneToOne()) {
if (empty($id)) { if (empty($id)) {
...@@ -54,7 +54,7 @@ class Doctrine_Relation_ForeignKey extends Doctrine_Relation ...@@ -54,7 +54,7 @@ class Doctrine_Relation_ForeignKey extends Doctrine_Relation
$dql = 'FROM ' . $this->getTable()->getComponentName() $dql = 'FROM ' . $this->getTable()->getComponentName()
. ' WHERE ' . $this->getCondition(); . ' WHERE ' . $this->getCondition();
$coll = $this->getTable()->getConnection()->query($dql, array($id)); $coll = $this->getTable()->getConnection()->query($dql, $id);
$related = $coll[0]; $related = $coll[0];
} }
...@@ -66,7 +66,7 @@ class Doctrine_Relation_ForeignKey extends Doctrine_Relation ...@@ -66,7 +66,7 @@ class Doctrine_Relation_ForeignKey extends Doctrine_Relation
$related = new Doctrine_Collection($this->getTable()); $related = new Doctrine_Collection($this->getTable());
} else { } else {
$query = $this->getRelationDql(1); $query = $this->getRelationDql(1);
$related = $this->getTable()->getConnection()->query($query, array($id)); $related = $this->getTable()->getConnection()->query($query, $id);
} }
$related->setReference($record, $this); $related->setReference($record, $this);
} }
......
...@@ -126,6 +126,8 @@ class Doctrine_Relation_Parser ...@@ -126,6 +126,8 @@ class Doctrine_Relation_Parser
if (isset($this->_pending[$alias])) { if (isset($this->_pending[$alias])) {
$def = $this->_pending[$alias]; $def = $this->_pending[$alias];
// check if reference class name exists
// if it does we are dealing with association relation
if (isset($def['refClass'])) { if (isset($def['refClass'])) {
$def = $this->completeAssocDefinition($def); $def = $this->completeAssocDefinition($def);
$localClasses = array_merge($this->_table->getOption('parents'), array($this->_table->getComponentName())); $localClasses = array_merge($this->_table->getOption('parents'), array($this->_table->getComponentName()));
...@@ -134,7 +136,10 @@ class Doctrine_Relation_Parser ...@@ -134,7 +136,10 @@ class Doctrine_Relation_Parser
! isset($this->_relations[$def['refClass']])) { ! isset($this->_relations[$def['refClass']])) {
$def['refTable']->getRelationParser()->bind($this->_table->getComponentName(), $def['refTable']->getRelationParser()->bind($this->_table->getComponentName(),
array('type' => Doctrine_Relation::ONE)); array('type' => Doctrine_Relation::ONE,
'local' => $def['local'],
'foreign' => $this->_table->getIdentifier(),
));
$this->bind($def['refClass'], array('type' => Doctrine_Relation::MANY, $this->bind($def['refClass'], array('type' => Doctrine_Relation::MANY,
'foreign' => $def['local'])); 'foreign' => $def['local']));
...@@ -145,15 +150,20 @@ class Doctrine_Relation_Parser ...@@ -145,15 +150,20 @@ class Doctrine_Relation_Parser
$rel = new Doctrine_Relation_Association($def); $rel = new Doctrine_Relation_Association($def);
} }
} else { } else {
// simple foreign key relation
$def = $this->completeDefinition($def); $def = $this->completeDefinition($def);
if ( ! isset($def['foreign'])) {
Doctrine::dump($def); if (isset($def['localKey'])) {
} $rel = new Doctrine_Relation_LocalKey($def);
$rel = new Doctrine_Relation_ForeignKey($def); } else {
$rel = new Doctrine_Relation_ForeignKey($def);
}
} }
if (isset($rel)) { if (isset($rel)) {
unset($this->_pending[$name]); // unset pending relation
unset($this->_pending[$alias]);
$this->_relations[$alias] = $rel;
return $rel; return $rel;
} }
} }
...@@ -288,6 +298,7 @@ class Doctrine_Relation_Parser ...@@ -288,6 +298,7 @@ class Doctrine_Relation_Parser
// the foreign field is likely to be the // the foreign field is likely to be the
// identifier of the foreign class // identifier of the foreign class
$def['foreign'] = $def['table']->getIdentifier(); $def['foreign'] = $def['table']->getIdentifier();
$def['localKey'] = true;
} }
} }
} else { } else {
...@@ -295,6 +306,7 @@ class Doctrine_Relation_Parser ...@@ -295,6 +306,7 @@ class Doctrine_Relation_Parser
// local key not set, but foreign key is set // local key not set, but foreign key is set
// try to guess the local key // try to guess the local key
if ($def['foreign'] === $def['table']->getIdentifier()) { if ($def['foreign'] === $def['table']->getIdentifier()) {
$def['localKey'] = true;
$def['local'] = $this->guessColumns($foreignClasses, $this->_table); $def['local'] = $this->guessColumns($foreignClasses, $this->_table);
} else { } else {
$def['local'] = $this->_table->getIdentifier(); $def['local'] = $this->_table->getIdentifier();
...@@ -316,6 +328,7 @@ class Doctrine_Relation_Parser ...@@ -316,6 +328,7 @@ class Doctrine_Relation_Parser
if ($table2->hasColumn($column)) { if ($table2->hasColumn($column)) {
$def['foreign'] = $column; $def['foreign'] = $column;
$def['local'] = $table->getIdentifier(); $def['local'] = $table->getIdentifier();
$def['localKey'] = true;
return $def; return $def;
} }
} }
...@@ -334,8 +347,8 @@ class Doctrine_Relation_Parser ...@@ -334,8 +347,8 @@ class Doctrine_Relation_Parser
return $def; return $def;
} }
} }
} Doctrine::dump($this->_table->getComponentName()); }
Doctrine::dump($def); Doctrine::dump($def);
throw new Doctrine_Relation_Parser_Exception("Couldn't complete relation definition."); throw new Doctrine_Relation_Parser_Exception("Couldn't complete relation definition.");
} }
} }
......
This diff is collapsed.
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