Comments (5)
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.
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.
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.
Definitely cleaner than mine, i like it!
from closuretable.
It would be nice if you made the pull request with these changes because it was your idea initially!
from closuretable.
Related Issues (20)
- Compatibility with later Laravel versions (5.7, 5.8 and 6.x) HOT 5
- make command does not work with Laravel 6.2 HOT 3
- Collection::makeTree() hits database for each child HOT 4
- Upgrade guide to v6 HOT 2
- Examples from readme are not works HOT 2
- Child with multiple parents not possible (child will be moved) HOT 1
- Hardcoded table column names in the ClosureTable model
- Error: array_key_exists() expects parameter 2 to be array, int given HOT 1
- How get Tree from 0 level ? HOT 5
- Problem with createFromArray method HOT 2
- erro in delete member
- createFromArray
- ancestorsWithSelf() queries database for each ancestor
- Readme example doesnt work in 6.1.1 HOT 2
- Multiple parents HOT 1
- Deprecated interfaces in Entity and ClosureTable
- What should I do when my data volume is very large, more than 100 million? HOT 2
- reorderSiblings method doesn't update softdeleted node's position
- Seeking a method to not update updated_at
- Drive: data de modificação sendo alterada
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from closuretable.