Example #1
0
 /**
  * Creates a {@link Blueprint} instance from an {@link ActiveRecord\Query}.
  *
  * @param Query $query
  *
  * @return Blueprint
  */
 public static function from(Query $query)
 {
     $query->mode(\PDO::FETCH_CLASS, __NAMESPACE__ . '\\BluePrintNode');
     $relation = array();
     $children = array();
     $index = array();
     foreach ($query as $row) {
         $row->parent = null;
         $row->depth = null;
         $row->children = array();
         $nid = $row->nid;
         $parent_id = $row->parentid;
         $index[$nid] = $row;
         $relation[$nid] = $parent_id;
         $children[$parent_id][$nid] = $nid;
     }
     $tree = array();
     foreach ($index as $nid => $page) {
         if (!$page->parentid || empty($index[$page->parentid])) {
             $tree[$nid] = $page;
             continue;
         }
         $page->parent = $index[$page->parentid];
         $page->parent->children[$nid] = $page;
     }
     self::set_depth($tree);
     return new static($query->model, $relation, $children, $index, $tree);
 }