HosipLan

Untitled

Aug 17th, 2011
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.60 KB | None | 0 0
  1. <?php
  2.  
  3. class DibiExtensions extends Nette\Object {
  4.  
  5.     public static function register()
  6.     {
  7.         \DibiFluent::extensionMethod('fetchTree', callback(__CLASS__, 'fetchTree'));
  8.         \DibiResult::extensionMethod('fetchTree', callback(__CLASS__, 'fetchTree'));
  9.     }
  10.    
  11.  
  12.     public static function fetchTree(DibiObject $obj, $id, $parent_id) {
  13.         if ($obj instanceof DibiFluent) {
  14.                 $result = $obj->execute();
  15.  
  16.         } elseif ($obj instanceof DibiResult) {
  17.                 $result = $obj;
  18.  
  19.         } else {
  20.                 throw new Nette\MemberAccessException("fetchTree cant be called on " . get_class($obj));
  21.         }
  22.  
  23.         // nahradit $obj za $result
  24.  
  25.         $result->seek(0);
  26.         $row = $result->fetch(FALSE);
  27.         if (!$row)
  28.             return array();  // empty resultset
  29.  
  30.             $refs = array();
  31.         $data = NULL;
  32.  
  33.         if (!array_key_exists($id, $row) ||
  34.                 !array_key_exists($parent_id, $row)) {
  35.             throw new \Nette\InvalidArgumentException("Column '$id' or '$parent_id' is not in record");
  36.         }
  37.  
  38.         do {
  39.             $ref = &$refs[$row[$id]];
  40.  
  41.             $ref[$parent_id] = $row[$parent_id];
  42.             $ref = array_merge((array) $ref, (array) $row);
  43.  
  44.             if ($row[$parent_id] == 0 || $row[$parent_id] == NULL) {
  45.                 $data[$row[$id]] = &$ref;
  46.             } else {
  47.                 $refs[$row[$parent_id]]['children'][$row[$id]] = &$ref;
  48.             }
  49.         } while ($row = $result->fetch(FALSE));
  50.  
  51.         unset($ref);
  52.         unset($refs);
  53.         return $data;
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment