1
0
mirror of synced 2024-12-14 07:06:04 +03:00

refactoring for tree implementation

This commit is contained in:
joesimms 2007-02-08 12:53:32 +00:00
parent 8eabed57d6
commit 02efd7f73b
9 changed files with 58 additions and 34 deletions

View File

@ -102,6 +102,7 @@ class Doctrine_Locking_Manager_Pessimistic
$key = $record->obtainIdentifier();
$gotLock = false;
$time = time();
if (is_array($key)) {
// Composite key
@ -118,7 +119,7 @@ class Doctrine_Locking_Manager_Pessimistic
$stmt->bindParam(':object_type', $objectType);
$stmt->bindParam(':object_key', $key);
$stmt->bindParam(':user_ident', $userIdent);
$stmt->bindParam(':ts_obtained', time());
$stmt->bindParam(':ts_obtained', $time);
try {
$stmt->execute();
@ -138,7 +139,7 @@ class Doctrine_Locking_Manager_Pessimistic
WHERE object_type = :object_type AND
object_key = :object_key AND
user_ident = :user_ident");
$stmt->bindParam(':ts', time());
$stmt->bindParam(':ts', $time);
$stmt->bindParam(':object_type', $objectType);
$stmt->bindParam(':object_key', $key);
$stmt->bindParam(':user_ident', $lockingUserIdent);

View File

@ -129,7 +129,7 @@ class Doctrine_Node implements IteratorAggregate
$options = (isset($this->iteratorOptions) ? $this->iteratorOptions : array());
}
$implName = $this->record->getTable()->getTreeImplName();
$implName = $this->record->getTable()->getOption('treeImpl');
$iteratorClass = 'Doctrine_Node_' . $implName . '_' . ucfirst(strtolower($type)) . 'OrderIterator';
return new $iteratorClass($this->record, $options);

View File

@ -187,8 +187,9 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
{
$q = $this->record->getTable()->createQuery();
$parent = $q->where('lft < ? AND rgt > ?', array($this->getLeftValue(), $this->getRightValue()))
->orderBy('rgt asc')
$componentName = $this->record->getTable()->getComponentName();
$parent = $q->where("$componentName.lft < ? AND $componentName.rgt > ?", array($this->getLeftValue(), $this->getRightValue()))
->orderBy("$componentName.rgt asc")
->execute()
->getFirst();
@ -207,8 +208,9 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
{
$q = $this->record->getTable()->createQuery();
$ancestors = $q->where('lft < ? AND rgt > ?', array($this->getLeftValue(), $this->getRightValue()))
->orderBy('lft asc')
$componentName = $this->record->getTable()->getComponentName();
$ancestors = $q->where("$componentName.lft < ? AND $componentName.rgt > ?", array($this->getLeftValue(), $this->getRightValue()))
->orderBy("$componentName.lft asc")
->execute();
return $ancestors;

View File

@ -68,9 +68,9 @@ class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator
$params = array($record->get('lft'), $record->get('rgt'));
if (isset($opts['include_record']) && $opts['include_record']) {
$query = $q->where("$componentName.lft >= ? AND $componentName.rgt <= ?", $params)->orderBy('lft asc');
$query = $q->where("$componentName.lft >= ? AND $componentName.rgt <= ?", $params)->orderBy("$componentName.lft asc");
} else {
$query = $q->where("$componentName.lft > ? AND $componentName.rgt < ?", $params)->orderBy('lft asc');
$query = $q->where("$componentName.lft > ? AND $componentName.rgt < ?", $params)->orderBy("$componentName.lft asc");
}
$query = $record->getTable()->getTree()->returnQueryWithRootId($query, $record->getNode()->getRootValue());

View File

@ -47,7 +47,8 @@ class Doctrine_Query_Set extends Doctrine_Query_Part
$alias = $this->query->getTableAlias($reference);
$result[] = $alias . '.' . $field . ' = ' . $set[1];
$fieldname = $alias ? $alias . '.' . $field : $field;
$result[] = $fieldname . ' = ' . $set[1];
}
return implode(', ', $result);

View File

@ -171,8 +171,9 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition
$value = $enumIndex;
}
default:
$where = $alias . '.' . $field . ' '
. $operator . ' ' . $value;
$fieldname = $alias ? $alias . '.' . $field : $field;
$where = $fieldname . ' '
. $operator . ' ' . $value;
}
}
}

View File

@ -78,6 +78,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* @var object Doctrine_Table $_table the factory that created this data access object
*/
protected $_table;
/**
* @var Doctrine_Node_<TreeImpl> node object
*/
protected $_node;
/**
* @var integer $_id the primary keys of this object
*/
@ -1532,10 +1536,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
public function hasIndex($name )
{
}
public function actsAsTree($treeImplName, $args)
{
}
/**
* addListener
@ -1591,6 +1591,27 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
}
return $this;
}
/**
* getter for node assciated with this record
*
* @return mixed if tree returns Doctrine_Node otherwise returns false
*/
public function getNode() {
if(!$this->_table->isTree())
return false;
if(!isset($this->_node))
$this->_node = Doctrine_Node::factory($this, $this->getTable()->getOption('treeImpl'), $this->getTable()->getOption('treeOptions'));
return $this->_node;
}
/**
* used to delete node from tree - MUST BE USE TO DELETE RECORD IF TABLE ACTS AS TREE
*
*/
public function deleteNode() {
$this->getNode()->delete();
}
/**
* returns a string representation of this object
*/

View File

@ -206,6 +206,10 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
if (method_exists($record, 'setTableDefinition')) {
$record->setTableDefinition();
// set the table definition for the given tree implementation
if($this->isTree())
$this->getTree()->setTableDefinition();
$this->columnCount = count($this->columns);
if (isset($this->columns)) {
@ -298,6 +302,10 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
$record->setUp();
// if tree, set up tree
if($this->isTree())
$this->getTree()->setUp();
// save parents
array_pop($names);
$this->parents = $names;
@ -1377,15 +1385,6 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
{
return $this->data;
}
/**
* returns a string representation of this object
*
* @return string
*/
public function setTree($implName, $options) {
$this->setOption('treeImpl', $implName);
$this->setOption('treeOptions', $options);
}
/**
* getter for associated tree
*
@ -1398,9 +1397,6 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
$this->options['treeImpl'],
$this->options['treeOptions']
);
// set the table definition for the given tree implementation
$this->tree->setTableDefinition();
}
return $this->tree;
}
@ -1409,7 +1405,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
/**
* determine if table acts as tree
*
* @return mixed if tree return instance of Doctrine_Tree, otherwise returns false
* @return mixed if tree return true, otherwise returns false
*/
public function isTree() {
return ( ! is_null($this->options['treeImpl'])) ? true : false;

View File

@ -40,8 +40,9 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int
public function __construct(Doctrine_Table $table, $options)
{
// set default many root attributes
$options['has_many_roots'] = isset($options['has_many_roots']) ? $options['has_many_roots'] : false;
$options['root_column_name'] = isset($options['root_column_name']) ? $options['root_column_name'] : 'root_id';
$options['hasManyRoots'] = isset($options['hasManyRoots']) ? $options['hasManyRoots'] : false;
if($options['hasManyRoots'])
$options['rootColumnName'] = isset($options['rootColumnName']) ? $options['rootColumnName'] : 'root_id';
parent::__construct($table, $options);
}
@ -73,7 +74,7 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int
}
// if tree is many roots, then get next root id
if($root = $this->getAttribute('root_column_name')) {
if($root = $this->getAttribute('hasManyRoots')) {
$record->getNode()->setRootValue($this->getNextRootId());
}
@ -121,9 +122,10 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int
{
// fetch tree
$q = $this->table->createQuery();
$componentName = $this->table->getComponentName();
$q = $q->where('lft >= ?', 1)
->orderBy('lft asc');
$q = $q->where("$componentName.lft >= ?", 1)
->orderBy("$componentName.lft asc");
// if tree has many roots, then specify root id
$rootId = isset($options['root_id']) ? $options['root_id'] : '1';