Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DibiExtensions extends Nette\Object {
- public static function register()
- {
- \DibiFluent::extensionMethod('fetchTree', callback(__CLASS__, 'fetchTree'));
- \DibiResult::extensionMethod('fetchTree', callback(__CLASS__, 'fetchTree'));
- }
- public static function fetchTree(DibiObject $obj, $id, $parent_id) {
- if ($obj instanceof DibiFluent) {
- $result = $obj->execute();
- } elseif ($obj instanceof DibiResult) {
- $result = $obj;
- } else {
- throw new Nette\MemberAccessException("fetchTree cant be called on " . get_class($obj));
- }
- // nahradit $obj za $result
- $result->seek(0);
- $row = $result->fetch(FALSE);
- if (!$row)
- return array(); // empty resultset
- $refs = array();
- $data = NULL;
- if (!array_key_exists($id, $row) ||
- !array_key_exists($parent_id, $row)) {
- throw new \Nette\InvalidArgumentException("Column '$id' or '$parent_id' is not in record");
- }
- do {
- $ref = &$refs[$row[$id]];
- $ref[$parent_id] = $row[$parent_id];
- $ref = array_merge((array) $ref, (array) $row);
- if ($row[$parent_id] == 0 || $row[$parent_id] == NULL) {
- $data[$row[$id]] = &$ref;
- } else {
- $refs[$row[$parent_id]]['children'][$row[$id]] = &$ref;
- }
- } while ($row = $result->fetch(FALSE));
- unset($ref);
- unset($refs);
- return $data;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment