From 02efd7f73b1393156f25ce4f9eddfc03be429a3b Mon Sep 17 00:00:00 2001 From: joesimms Date: Thu, 8 Feb 2007 12:53:32 +0000 Subject: [PATCH] refactoring for tree implementation --- lib/Doctrine/Locking/Manager/Pessimistic.php | 5 ++-- lib/Doctrine/Node.php | 2 +- lib/Doctrine/Node/NestedSet.php | 10 ++++--- .../Node/NestedSet/PreOrderIterator.php | 4 +-- lib/Doctrine/Query/Set.php | 3 +- lib/Doctrine/Query/Where.php | 5 ++-- lib/Doctrine/Record.php | 29 ++++++++++++++++--- lib/Doctrine/Table.php | 22 ++++++-------- lib/Doctrine/Tree/NestedSet.php | 12 ++++---- 9 files changed, 58 insertions(+), 34 deletions(-) diff --git a/lib/Doctrine/Locking/Manager/Pessimistic.php b/lib/Doctrine/Locking/Manager/Pessimistic.php index 9f33bb9c9..b57079ab4 100644 --- a/lib/Doctrine/Locking/Manager/Pessimistic.php +++ b/lib/Doctrine/Locking/Manager/Pessimistic.php @@ -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); diff --git a/lib/Doctrine/Node.php b/lib/Doctrine/Node.php index 4ff072bf4..e92f844f7 100644 --- a/lib/Doctrine/Node.php +++ b/lib/Doctrine/Node.php @@ -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); diff --git a/lib/Doctrine/Node/NestedSet.php b/lib/Doctrine/Node/NestedSet.php index fb0e4e05c..4045d8916 100644 --- a/lib/Doctrine/Node/NestedSet.php +++ b/lib/Doctrine/Node/NestedSet.php @@ -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; diff --git a/lib/Doctrine/Node/NestedSet/PreOrderIterator.php b/lib/Doctrine/Node/NestedSet/PreOrderIterator.php index bc2e87d5a..1075281b9 100644 --- a/lib/Doctrine/Node/NestedSet/PreOrderIterator.php +++ b/lib/Doctrine/Node/NestedSet/PreOrderIterator.php @@ -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()); diff --git a/lib/Doctrine/Query/Set.php b/lib/Doctrine/Query/Set.php index b4ce0a3fa..5d98bb293 100644 --- a/lib/Doctrine/Query/Set.php +++ b/lib/Doctrine/Query/Set.php @@ -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); diff --git a/lib/Doctrine/Query/Where.php b/lib/Doctrine/Query/Where.php index 02c1fa932..1f63dfc52 100644 --- a/lib/Doctrine/Query/Where.php +++ b/lib/Doctrine/Query/Where.php @@ -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; } } } diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index 63da66f86..247cd486d 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -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_ 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 */ diff --git a/lib/Doctrine/Table.php b/lib/Doctrine/Table.php index 96900d8cf..54b837487 100644 --- a/lib/Doctrine/Table.php +++ b/lib/Doctrine/Table.php @@ -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; diff --git a/lib/Doctrine/Tree/NestedSet.php b/lib/Doctrine/Tree/NestedSet.php index 29286765a..d0aa24e23 100644 --- a/lib/Doctrine/Tree/NestedSet.php +++ b/lib/Doctrine/Tree/NestedSet.php @@ -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';