Code Monkey home page Code Monkey logo

Comments (3)

Surt avatar Surt commented on August 26, 2024

I think I have it.

new Collection.php inside baum.

<?php
namespace Baum;

use Illuminate\Database\Eloquent\Collection as Eloquent_Collection;

class Collection extends Eloquent_Collection {

    private function nestedArray(&$result, $right = 'rgh', $left = 'lft', $path = '', $level = -1) {
        $new = array();
        if(is_array($result)) {
            while(list($n, $sub) = each($result)) {
                $subId = $sub['id'];
                $new[$subId] = $sub;
                $alias = (isset($sub['data']))?$sub['data']['alias']:'';
                $new[$subId]['_tree_path'] = $path.'/'.$alias;
                $new[$subId]['_tree_level'] = $level + 1;

                if($sub[$right] - $sub[$left] != 1) {
                    // recurse ($result is manipulated by reference!)
                    $new[$subId]['children'] = $this->nestedArray($result, $right, $left, $new[$subId]['_tree_path'], $new[$subId]['_tree_level']);
                }

                $next_id = key($result);
                if($next_id && $result[$next_id]['parent_id'] != $sub['parent_id']) {
                    return $new;
                }
            }
        }
        return $new;
    }

    public function toHierarchical(){
        $tree = $this->toArray();
        return $this->nestedArray($tree);
    }
}

and, at Baum\Node.php add a new method

    public function newCollection(array $models = array())
    {
        return new Baum\Collection($models);
    }

It needs some tweaks, using getQualifiedLeftColumName, etc... and adding the changes to make path as option, etc...
I will try to make the changes when I get some time. (If nobody makes it before)

from baum.

Surt avatar Surt commented on August 26, 2024

Done...
The "toHierarchical" method returns a Eloquent\Collection of models, each element with its children in a hierarchical way.

Now is possible to do a $collection->toHierarchical()->toArray(); if needed.

<?php
namespace Baum;

use Illuminate\Database\Eloquent\Collection as Eloquent_Collection;

class Collection extends Eloquent_Collection {

    public function toHierarchical(){
        $tree = $this->items;
        return new Eloquent_Collection($this->hierarchical($tree));
    }


    private function hierarchical(&$result) {
        $new = array();
        if(is_array($result)) {
            while(list($n, $sub) = each($result)) {
                $new[$sub->getKey()] = $sub;
                if($sub->getRight() - $sub->getLeft() != 1) {
                    $new[$sub->getKey()]->children = $this->hierarchical($result);
                }
                $next_id = key($result);
                if($next_id && $result[$next_id]->getParentId() != $sub->getParentId()) {
                    return $new;
                }
            }
        }
        return $new;
    }
}

from baum.

etrepat avatar etrepat commented on August 26, 2024

As we are using #5 to discuss and you've provided the implementation, I'm closing this issue...

from baum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.