• Eyelash / php jquery image management
  • Zend Framework hierarchical Zend_Db_Table extension

    After a few hours looking for a good implementation of recursive data with Zend Framework, I came across Hector Virgen’s table abstraction for nested recursive tables, which uses the efficiency of the Modified Preorder Tree Traversal method that retrieves descendents and ancestors faster that with the old parent_id reference. In fact parent_id is not needed any more but in the example below we will keep the parent_id implementation. My aim here is to implement a draggable tree with data from this Zend_DB_Table abstraction. Setting up ext js with Zend Framework is not that hard, especially when many tutorials have been posted around for extjs tree with other frameworks such as Cake PHP .

    The Hierarchical zend_db_table extension is the first part of the problem : we have simplified Virgen’s db_table extension to let it work with just parent_id. Here you will find basic functions that allow you to count children of a particular record and list them as rowset rows.

    Usage :

    1. include("Hierarchical_Db_Table.php");
    2. class Theme2 extends Hierarchical_Db_Table  {    protected  $_parentKey = "parent_id";}
    3. $theme=new Theme2;
    4. if ($node=="0" || $node=="") $thRowSet = $theme->fetchTop();
    5. else $thRowSet=$theme->fetchChildren($node);

    File : Hierarchical_Db_Table.php

    1. <?php
    2. /**
    3.  * Enhancements to Zend_Db_Table
    4.  * @author webmasterbulletin.net
    5.  *
    6.  */
    7. require_once ‘Zend/Db/Table/Abstract.php’;
    8.  
    9. class Hierarchical_Db_Table extends Zend_Db_Table_Abstract
    10. {
    11. public function primaryKey()
    12.         {
    13.                 $info = $this->info();
    14.                 return $info[primary][1];
    15.         }
    16. public function fetchTop(  Zend_Db_Select $select = null)
    17.         {
    18.                 if (null === $select) {
    19.             $select = $this->select();
    20.         }
    21.        
    22.         $select->where($this->_parentKey . ‘ is NULL’ );    
    23.                 return $this->fetchAll($select);
    24.         }
    25. public function CountChildren($row, Zend_Db_Select $select = null)
    26.         {
    27.                  if ($row instanceof Zend_Db_Table_Row_Abstract) {
    28.             $_row = $row;
    29.         } else if (is_string($row) OR is_numeric($row)) {
    30.             $_row = $this->find($row)->current();
    31.             if (null === $_row) {
    32.                 require_once ‘Zend/Db/Exception.php’;
    33.                 throw new Zend_Db_Exception("Cannot find row ‘" . $this->_traversal[‘column’] . "’ = {$row}");
    34.             }
    35.         } else {
    36.             require_once ‘Zend/Db/Exception.php’;
    37.             throw new Zend_Db_Exception("Expecting instance of Zend_Db_Table_Row_Abstract, a string, or numeric");
    38.         }
    39.                
    40.                  if (null === $select) {
    41.             $select = $this->select();
    42.         }
    43.                  $parent_id = $_row->{$this->primaryKey()};
    44.         $select->where($this->_parentKey . ‘ = ?’, (int) $parent_id)  ;
    45.                
    46.                 $select->from($this, array("count(" . $this->primaryKey() . ") as c"));
    47.                 $row =          $this->fetchAll($select)->current();
    48.                 //print_r($select->toArray());
    49.                 //Zend_Debug::dump( $this->_parentKey .  $parent_id);
    50.                 return $row->c;
    51.         }
    52. public function fetchChildren($row, Zend_Db_Select $select = null)
    53.         {
    54.                  if ($row instanceof Zend_Db_Table_Row_Abstract) {
    55.             $_row = $row;
    56.         } else if (is_string($row) OR is_numeric($row)) {
    57.              $_row = $this->find($row)->current();
    58.             if (null === $_row) {
    59.                 require_once ‘Zend/Db/Exception.php’;
    60.                 throw new Zend_Db_Exception("Cannot find row ‘" . $this->primaryKey(). "’ = {$row}");
    61.             }
    62.         } else {
    63.             require_once ‘Zend/Db/Exception.php’;
    64.             throw new Zend_Db_Exception("Expecting instance of Zend_Db_Table_Row_Abstract, a string, or numeric");
    65.         }
    66.                
    67.                  if (null === $select) {
    68.             $select = $this->select();
    69.         }
    70.          
    71.          $parent_id = $_row->{$this->primaryKey()};
    72.                 //print_r($_row);
    73.         $select->where($this->_parentKey . ‘ = ?’, (int) $parent_id)        ;
    74.         return $this->fetchAll($select);
    75.         }
    76. }
    1. One Response to “Zend Framework hierarchical Zend_Db_Table extension”

    2. June 11, 2009 - George John

      This was useful for my classifieds adcertisement section

    Quick contact form