HosipLan

Untitled

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