Varien_Data_Tree_Db Class Reference

Inheritance diagram for Varien_Data_Tree_Db:

Varien_Data_Tree

List of all members.

Public Member Functions

 __construct ($connection, $table, $fields)
 getDbSelect ()
 setDbSelect ($select)
 load ($parentNode=null, $recursionLevel=100)
 loadNode ($nodeId)
 appendChild ($data=array(), $parentNode, $prevNode=null)
 moveNodeTo ($node, $parentNode, $prevNode=null)
 removeNode ($node)

Public Attributes

const ID_FIELD = 'id'
const PARENT_FIELD = 'parent'
const LEVEL_FIELD = 'level'
const ORDER_FIELD = 'order'

Protected Member Functions

 _updateChildLevels ($parentId, $parentLevel)
 _loadFullTree ()

Protected Attributes

 $_conn
 $_table
 $_select
 $_idField
 $_parentField
 $_levelField
 $_orderField


Detailed Description

Definition at line 37 of file Db.php.


Constructor & Destructor Documentation

__construct ( connection,
table,
fields 
)

Db tree constructor

$fields = array( Varien_Data_Tree_Db.ID_FIELD => string, Varien_Data_Tree_Db.PARENT_FIELD => string, Varien_Data_Tree_Db.LEVEL_FIELD => string Varien_Data_Tree_Db.ORDER_FIELD => string )

Parameters:
Zend_Db_Adapter_Abstract $connection
string $table
array $fields

Definition at line 89 of file Db.php.

00090     {
00091         parent::__construct();
00092 
00093         if (!$connection) {
00094             throw new Exception('Wrong "$connection" parametr');
00095         }
00096 
00097         $this->_conn    = $connection;
00098         $this->_table   = $table;
00099 
00100         if (!isset($fields[self::ID_FIELD]) ||
00101             !isset($fields[self::PARENT_FIELD]) ||
00102             !isset($fields[self::LEVEL_FIELD]) ||
00103             !isset($fields[self::ORDER_FIELD])) {
00104 
00105             throw new Exception('"$fields" tree configuratin array');
00106         }
00107 
00108         $this->_idField     = $fields[self::ID_FIELD];
00109         $this->_parentField = $fields[self::PARENT_FIELD];
00110         $this->_levelField  = $fields[self::LEVEL_FIELD];
00111         $this->_orderField  = $fields[self::ORDER_FIELD];
00112 
00113         $this->_select  = $this->_conn->select();
00114         $this->_select->from($this->_table, array_values($fields));
00115     }


Member Function Documentation

_loadFullTree (  )  [protected]

Definition at line 264 of file Db.php.

00265     {
00266         $select = clone $this->_select;
00267         $select->order($this->_table . '.' . $this->_levelField)
00268             ->order($this->_table.'.'.$this->_orderField);
00269 
00270         $arrNodes = $this->_conn->fetchAll($select);
00271 
00272         foreach ($arrNodes as $nodeInfo) {
00273             $node = new Varien_Data_Tree_Node($nodeInfo, $this->_idField, $this);
00274             $parentNode = $this->getNodeById($nodeInfo[$this->_parentField]);
00275             $this->addNode($node, $parentNode);
00276         }
00277 
00278         return $this;
00279     }

_updateChildLevels ( parentId,
parentLevel 
) [protected]

Definition at line 246 of file Db.php.

00247     {
00248         $select = $this->_conn->select()
00249             ->from($this->_table, $this->_idField)
00250             ->where($this->_parentField.'=?', $parentId);
00251         $ids = $this->_conn->fetchCol($select);
00252 
00253         if (!empty($ids)) {
00254             $this->_conn->update($this->_table,
00255                 array($this->_levelField=>$parentLevel+1),
00256                 $this->_conn->quoteInto($this->_idField.' IN (?)', $ids));
00257             foreach ($ids as $id) {
00258                 $this->_updateChildLevels($id, $parentLevel+1);
00259             }
00260         }
00261         return $this;
00262     }

appendChild ( data = array(),
parentNode,
prevNode = null 
)

Enter description here...

Parameters:
array|Varien_Data_Tree_Node $data
Varien_Data_Tree_Node $parentNode
Varien_Data_Tree_Node $prevNode
Returns:
Varien_Data_Tree_Node

Reimplemented from Varien_Data_Tree.

Definition at line 177 of file Db.php.

00178     {
00179         $orderSelect = $this->_conn->select();
00180         $orderSelect->from($this->_table, new Zend_Db_Expr('MAX('.$this->_conn->quoteIdentifier($this->_orderField).')'))
00181             ->where($this->_conn->quoteIdentifier($this->_parentField).'='.$parentNode->getId());
00182 
00183         $order = $this->_conn->fetchOne($orderSelect);
00184         $data[$this->_parentField] = $parentNode->getId();
00185         $data[$this->_levelField]  = $parentNode->getData($this->_levelField)+1;
00186         $data[$this->_orderField]  = $order+1;
00187 
00188         $this->_conn->insert($this->_table, $data);
00189         $data[$this->_idField] = $this->_conn->lastInsertId();
00190 
00191         return parent::appendChild($data, $parentNode, $prevNode);
00192     }

getDbSelect (  ) 

Definition at line 117 of file Db.php.

00118     {
00119         return $this->_select;
00120     }

load ( parentNode = null,
recursionLevel = 100 
)

Load tree

Parameters:
int || Varien_Data_Tree_Node $parentNode
int $recursionLevel recursion level
Returns:
this

Definition at line 134 of file Db.php.

00135     {
00136         if (is_null($parentNode)) {
00137             $this->_loadFullTree();
00138             return $this;
00139         }
00140         elseif ($parentNode instanceof Varien_Data_Tree_Node) {
00141             $parentId = $parentNode->getId();
00142         }
00143         elseif (is_numeric($parentNode)) {
00144             $parentId = $parentNode;
00145             $parentNode = null;
00146         }
00147         else {
00148             throw new Exception('root node id is not defined');
00149         }
00150 
00151         $select = clone $this->_select;
00152         $select->order($this->_table.'.'.$this->_orderField . ' ASC');
00153         $condition = $this->_conn->quoteInto("$this->_table.$this->_parentField=?", $parentId);
00154         $select->where($condition);
00155         $arrNodes = $this->_conn->fetchAll($select);
00156         foreach ($arrNodes as $nodeInfo) {
00157             $node = new Varien_Data_Tree_Node($nodeInfo, $this->_idField, $this, $parentNode);
00158             $this->addNode($node, $parentNode);
00159 
00160             if ($recursionLevel) {
00161                 $node->loadChildren($recursionLevel-1);
00162             }
00163         }
00164         return $this;
00165     }

loadNode ( nodeId  ) 

Enter description here...

Parameters:
unknown_type $nodeId

Reimplemented from Varien_Data_Tree.

Definition at line 167 of file Db.php.

00168     {
00169         $select = clone $this->_select;
00170         $condition = $this->_conn->quoteInto("$this->_table.$this->_idField=?", $nodeId);
00171         $select->where($condition);
00172         $node = new Varien_Data_Tree_Node($this->_conn->fetchRow($select), $this->_idField, $this);
00173         $this->addNode($node);
00174         return $node;
00175     }

moveNodeTo ( node,
parentNode,
prevNode = null 
)

Move tree node

Parameters:
Varien_Data_Tree_Node $node
Varien_Data_Tree_Node $parentNode
Varien_Data_Tree_Node $prevNode

Reimplemented from Varien_Data_Tree.

Definition at line 201 of file Db.php.

00202     {
00203         $data = array();
00204         $data[$this->_parentField]  = $parentNode->getId();
00205         $data[$this->_levelField]   = $parentNode->getData($this->_levelField)+1;
00206         // New node order
00207         if (is_null($prevNode) || is_null($prevNode->getData($this->_orderField))) {
00208             $data[$this->_orderField] = 1;
00209         }
00210         else {
00211             $data[$this->_orderField] = $prevNode->getData($this->_orderField)+1;
00212         }
00213         $condition = $this->_conn->quoteInto("$this->_idField=?", $node->getId());
00214 
00215         // For reorder new node branch
00216         $dataReorderNew = array(
00217             $this->_orderField => new Zend_Db_Expr($this->_conn->quoteIdentifier($this->_orderField).'+1')
00218         );
00219         $conditionReorderNew = $this->_conn->quoteIdentifier($this->_parentField).'='.$parentNode->getId().
00220                             ' AND '.$this->_conn->quoteIdentifier($this->_orderField).'>='. $data[$this->_orderField];
00221 
00222         // For reorder old node branch
00223         $dataReorderOld = array(
00224             $this->_orderField => new Zend_Db_Expr($this->_conn->quoteIdentifier($this->_orderField).'-1')
00225         );
00226         $conditionReorderOld = $this->_conn->quoteIdentifier($this->_parentField).'='.$node->getData($this->_parentField).
00227                             ' AND '.$this->_conn->quoteIdentifier($this->_orderField).'>'.$node->getData($this->_orderField);
00228 
00229         $this->_conn->beginTransaction();
00230         try {
00231             // Prepare new node branch
00232             $this->_conn->update($this->_table, $dataReorderNew, $conditionReorderNew);
00233             // Move node
00234             $this->_conn->update($this->_table, $data, $condition);
00235             // Update old node branch
00236             $this->_conn->update($this->_table, $dataReorderOld, $conditionReorderOld);
00237             $this->_updateChildLevels($node->getId(), $data[$this->_levelField]);
00238             $this->_conn->commit();
00239         }
00240         catch (Exception $e){
00241             $this->_conn->rollBack();
00242             throw new Exception('Can\'t move tree node');
00243         }
00244     }

removeNode ( node  ) 

Enter description here...

Parameters:
Varien_Data_Tree_Node $node
Returns:
Varien_Data_Tree

Reimplemented from Varien_Data_Tree.

Definition at line 281 of file Db.php.

00282     {
00283         // For reorder old node branch
00284         $dataReorderOld = array(
00285             $this->_orderField => new Zend_Db_Expr($this->_conn->quoteIdentifier($this->_orderField).'-1')
00286         );
00287         $conditionReorderOld = $this->_conn->quoteIdentifier($this->_parentField).'='.$node->getData($this->_parentField).
00288                             ' AND '.$this->_conn->quoteIdentifier($this->_orderField).'>'.$node->getData($this->_orderField);
00289 
00290         $this->_conn->beginTransaction();
00291         try {
00292             $condition = $this->_conn->quoteInto("$this->_idField=?", $node->getId());
00293             $this->_conn->delete($this->_table, $condition);
00294             // Update old node branch
00295             $this->_conn->update($this->_table, $dataReorderOld, $conditionReorderOld);
00296             $this->_conn->commit();
00297         }
00298         catch (Exception $e){
00299             $this->_conn->rollBack();
00300             throw new Exception('Can\'t remove tree node');
00301         }
00302         parent::removeNode($node);
00303         return $this;
00304     }

setDbSelect ( select  ) 

Definition at line 122 of file Db.php.

00123     {
00124         $this->_select = $select;
00125     }


Member Data Documentation

$_conn [protected]

Definition at line 49 of file Db.php.

$_idField [protected]

Definition at line 70 of file Db.php.

$_levelField [protected]

Definition at line 72 of file Db.php.

$_orderField [protected]

Definition at line 73 of file Db.php.

$_parentField [protected]

Definition at line 71 of file Db.php.

$_select [protected]

Definition at line 63 of file Db.php.

$_table [protected]

Definition at line 56 of file Db.php.

const ID_FIELD = 'id'

Definition at line 39 of file Db.php.

const LEVEL_FIELD = 'level'

Definition at line 41 of file Db.php.

const ORDER_FIELD = 'order'

Definition at line 42 of file Db.php.

const PARENT_FIELD = 'parent'

Definition at line 40 of file Db.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:25:00 2009 for Magento by  doxygen 1.5.8