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