beyondlex / mongotree

Laravel package for working with nested trees in MongoDB
3 stars 1 forks source link

ancestors & scoping fix #3

Open ombabush opened 6 years ago

ombabush commented 6 years ago

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!

sololance commented 6 years ago

Hi!

Can you please create a PR.

Thank you so much!