beyondlex / mongotree Goto Github PK
View Code? Open in Web Editor NEWLaravel package for working with nested trees in MongoDB
Laravel package for working with nested trees in MongoDB
Hello,
When working with multiple levels, I can't get only the next descendants.
I am using a mongo db.
Let me explain ...
When working with 2 levels, it all works just fine.
I have the following structure, using 3 levels ( parent, child, grandchild ):
parent 1
parent 2
parent 3
The following structure stores all the parents.
This is ok.
[
{
"_id": "5bb5ac508ac2ef20f4005d26",
"platform": "mobile",
"name": {
"en": "m1",
"ar": "1"
},
"image": {
"en": "1",
"ar": "1"
},
"link": {
"en": "1",
"ar": "1"
},
"parent_id": null,
"_lft": 1,
"_rgt": 6,
"updated_at": "2018-10-04 06:04:35",
"created_at": "2018-10-04 05:59:44"
},
{
"_id": "5bb5ac588ac2ef20f4005d27",
"platform": "mobile",
"name": {
"en": "m2",
"ar": "1"
},
"image": {
"en": "1",
"ar": "1"
},
"link": {
"en": "1",
"ar": "1"
},
"parent_id": null,
"_lft": 7,
"_rgt": 10,
"updated_at": "2018-10-04 06:04:35",
"created_at": "2018-10-04 05:59:52"
},
{
"_id": "5bb5acb48ac2ef20f4005d2d",
"platform": "mobile",
"name": {
"en": "m3",
"ar": "1"
},
"image": {
"en": "1",
"ar": "1"
},
"link": {
"en": "1",
"ar": "1"
},
"parent_id": null,
"_lft": 19,
"_rgt": 24,
"updated_at": "2018-10-04 06:04:35",
"created_at": "2018-10-04 06:01:24"
}
]
The following structure stores one child and one grandchild.
This is wrong.
Within this array, only the model with the _id 5bb5ac988ac2ef20f4005d2b should be returned, because that's the child.
5bb5ad738ac2ef20f4005d37 is the child of 5bb5ac988ac2ef20f4005d2b and this should not be here.
When callind $model->descendants
for 5bb5ac508ac2ef20f4005d26 ... I get the following :
[
{
"_id": "5bb5ac988ac2ef20f4005d2b",
"platform": "mobile",
"name": {
"en": "m1 c1",
"ar": "1"
},
"image": {
"en": "1",
"ar": "1"
},
"link": {
"en": "1",
"ar": "1"
},
"parent_id": "5bb5ac508ac2ef20f4005d26",
"_lft": 2,
"_rgt": 5,
"updated_at": "2018-10-04 06:04:35",
"created_at": "2018-10-04 06:00:56"
},
{
"_id": "5bb5ad738ac2ef20f4005d37",
"platform": "mobile",
"name": {
"en": "m1 c1 g1",
"ar": "1"
},
"image": {
"en": "1",
"ar": "1"
},
"link": {
"en": "1",
"ar": "1"
},
"parent_id": "5bb5ac988ac2ef20f4005d2b",
"_lft": 3,
"_rgt": 4,
"updated_at": "2018-10-04 06:04:35",
"created_at": "2018-10-04 06:04:35"
}
]
The grandchild 5bb5ad738ac2ef20f4005d37 should not be returned.
parent 1
parent 2
parent 3
When asking parent 1
for the children, the return should contain only parent 1 child 1
.
Hi!
I found out that ->ancestors() not working, not sure that it is the right way to fix it, but that patch solves the problem:
in original (lazychaser/laravel-nestedset) QueryBuilder.php inside whereAncestorOf function I change
$inner->whereRaw("{$value} between {$lft} and {$rgt}");
to
$inner->where( "_lft" , "<=", $value );
$inner->where( "_rgt", ">=", $value );
not sure how to pass proper "{$lft}" value from variable here, it's not working after call to wrappedColumns.
With this patch it's possible to get current level ( instead of not working withDepth() ):
$item->ancestors()->get()->count();
Second issue is that when you turn on Scoping, all new root elements saved as [_lft,_rgt] = [1,2] and if parent_id set – everything crashed.
To fix that one I change in TreeTrait.php function applyNestedSetScope():
$query->where($table.'.'.$attribute, '=',
to
$query->where($attribute, '=',
Not sure why mongo do not like this table prefix.
Hope this helps!
Hi!
First of all, thank you for creating this library.
Secondly, could you please add some documention how to install and use this library for new users?
PS I can make a PR if needed.
Thank you!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.