Code Monkey home page Code Monkey logo

Comments (5)

franzose avatar franzose commented on June 4, 2024

What kind of filter do you use? It would be nice, if you showed it here. Give me an example, please, and show how you modified the tree method. If you just want to get certain columns, you can modify current tree to something like this:

public static function tree(array $columns = array('*'))
{
    $instance = new static;
    $closureColumns = array(
        "closure1.".static::ANCESTOR,
        "closure1.".static::DESCENDANT,
        "closure1.".static::DEPTH
    );

    $columns = array_merge($columns, $closureColumns);
    // ...

Your response (and a pull request after discussion) is appreciated!

from closuretable.

vjandrea avatar vjandrea commented on June 4, 2024

This is the code i would add:

    /**
     * Retrive from the database a tree filtered using a where clause.
     * 
     * @param mixed|null $column
     * @param mixed|null $operator
     * @param mixed|null $value
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public static function filteredTree($column = null, $operator = null, $value = null)
    {
        $instance = new static;
        $columns = array(
            $instance->getTable().".*",
            "closure1.".static::ANCESTOR,
            "closure1.".static::DESCENDANT,
            "closure1.".static::DEPTH
        );

        $key = $instance->getQualifiedKeyName();

        if($column != null && $operator != null)
        {
            return static::select($columns)
                ->distinct()
                ->join($instance->getClosure().' as closure1', $key, '=', 'closure1.'.static::ANCESTOR)
                ->join($instance->getClosure().' as closure2', $key, '=', 'closure2.'.static::DESCENDANT)
                ->whereRaw('closure1.'.static::ANCESTOR.' = closure1.'.static::DESCENDANT)
                ->where($column, $operator, $value)
                ->get()
                ->toTree();
        }
        else
        {
            return static::select($columns)
                ->distinct()
                ->join($instance->getClosure().' as closure1', $key, '=', 'closure1.'.static::ANCESTOR)
                ->join($instance->getClosure().' as closure2', $key, '=', 'closure2.'.static::DESCENDANT)
                ->whereRaw('closure1.'.static::ANCESTOR.' = closure1.'.static::DESCENDANT)
                ->get()
                ->toTree();
        }
    }

The else clause is just a fallback to avoid an exception

from closuretable.

franzose avatar franzose commented on June 4, 2024

I would slightly improve your suggestion.

    /**
     * Retrive a whole tree from the database.
     *
     * @param array $columns
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public static function tree(array $columns = array('*'))
    {
        return static::buildTreeQuery(null, null, null, $columns)->get()->toTree();
    }

    /**
     * Retrive from the database a tree filtered using a where clause.
     *
     * @param string|Closure|null $column
     * @param string|null $operator
     * @param string|Closure|null $value
     * @param array $columns
     * @return \Illuminate\Database\Query\Builder
     */
    public static function filteredTree($column = null, $operator = null, $value = null, array $columns = array('*'))
    {
        return static::buildTreeQuery($column, $operator, $value, $columns)->get()->toTree();
    }

    /**
     * Builds query for retrieving a whole tree.
     *
     * @param string|Closure|null $column
     * @param string|null $operator
     * @param string|Closure|null $value
     * @param array $columns
     * @return \Illuminate\Database\Query\Builder
     */
    protected static function buildTreeQuery($column = null, $operator = null, $value = null, array $columns = array('*'))
    {
        $instance = new static;

        $closureColumns = array(
            "closure1.".static::ANCESTOR,
            "closure1.".static::DESCENDANT,
            "closure1.".static::DEPTH
        );

        // prevent from selecting all columns from all tables
        // as we have complex join query
        if (count($columns) == 1 && $columns[0] == '*')
        {
            $columns[0] = $instance->getTable().'.*';
        }

        $columns = array_merge($columns, $closureColumns);
        $key = $instance->getQualifiedKeyName();
        $closure = $instance->getClosure();
        $query = static::select($columns)
            ->distinct()
            ->join($closure.' as closure1', $key, '=', 'closure1.'.static::ANCESTOR)
            ->join($closure.' as closure2', $key, '=', 'closure2.'.static::DESCENDANT)
            ->whereRaw('closure1.'.static::ANCESTOR.' = closure1.'.static::DESCENDANT);

        if($column != null && $operator != null)
        {
            $query->where($column, $operator, $value);
        }

        return $query;
    }

You might noticed that, across the package, I use build...Query to encapsulate repetitive queries. Here we can do the same thing to let user decide what method to use, either simple tree or complex filteredTree. However, the hidden logic of both of them is quite the same.

What do you think?

from closuretable.

vjandrea avatar vjandrea commented on June 4, 2024

Definitely cleaner than mine, i like it!

from closuretable.

franzose avatar franzose commented on June 4, 2024

It would be nice if you made the pull request with these changes because it was your idea initially!

from closuretable.

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.