예제 #1
0
파일: Tree.php 프로젝트: ElBiniou/superbok
 public function loadChildren($all = false)
 {
     $fieldNames = array_keys($this->values);
     $selectedFields = array();
     foreach ($fieldNames as $name) {
         $selectedFields[] = 'node.' . $name;
     }
     if ($all) {
         $query = "\n                SELECT\n                  " . implode(',', $selectedFields) . "\n                FROM " . static::getTableName() . " root\n                    JOIN " . static::getTableName() . " node\n                        ON root." . $this->getLeftBoundFieldName() . "<node." . $this->getLeftBoundFieldName() . "\n                        AND root." . $this->getRightBoundFieldName() . ">node." . $this->getRightBoundFieldName() . "\n                    WHERE root." . $this->getPrimaryKeyFieldName() . "=" . $this->values[$this->getPrimaryKeyFieldName()] . "\n            ";
     } else {
         $query = "\n                SELECT\n                  " . implode(',', $selectedFields) . "\n                FROM " . static::getTableName() . " node\n                WHERE " . $this->getParentIdFieldName() . "=" . $this->values[$this->getPrimaryKeyFieldName()] . "\n            ";
     }
     $rows = $this->queryAndFetch($query);
     $nodes = array();
     foreach ($rows as $row) {
         $node = new Static($this->getSource());
         $node->setValues($row);
         $nodes[$row[$this->getPrimaryKeyFieldName()]] = $node;
     }
     foreach ($nodes as $node) {
         if ($node->getValue($this->getParentIdFieldName()) == $this->getValue($this->getPrimaryKeyFieldName())) {
             $this->addChild($node);
         } else {
             if (isset($nodes[$node->getValue($this->getParentIdFieldName())])) {
                 $nodes[$node->getValue($this->getParentIdFieldName())]->addChild($node);
             }
         }
     }
     if ($all) {
         $this->allChildren = array_values($nodes);
     }
 }