etrepat / baum

Baum is an implementation of the Nested Set pattern for Laravel's Eloquent ORM.
http://etrepat.com/baum
MIT License
2.24k stars 459 forks source link

DB Requests for descendants() #252

Open alexc-hollywood opened 7 years ago

alexc-hollywood commented 7 years ago

Thanks so much for putting this together - really useful!

I have a table with about 250 rows. I'm loading the whole recordset, but when i loop around it, each call to get the descendants revisits the DB - resulting in 171+ queries on one page. From my read of the docs, without using ->get() on a call should tell it to use the collection object, rather than querying again?

I need to browse the tree returned from just 1 query, rather than this. Am i missing something? Can't see anything in the docs.

Example code:

// load into view
  'services'  => Service::orderBy('name', 'ASC')->where('parent_id', '1')->get(),
<select id="" name="find-type" class="select2 select2find">
  @foreach($services->where('parent_id', 1) AS $service)
  <?php $children = $service->getDescendants(); ?>

    @if( count($children) )
      <option value="{{$service->id}}">{{ucwords($service->name)}}</option>
      @foreach( $children AS $child )
          <!-- put in child node. results in a query -->
      @endforeach
    @else
      <option value="{{$service->id}}">{{ucwords($service->name)}}</option>
    @endif

  @endforeach
</select>

This is what happens inside debug on this:

select `services`.*, `user_services`.`user_id` as `pivot_user_id`, `user_services`.`service_id` as `pivot_service_id` from `services` inner join `user_services` on `services`.`id` = `user_services`.`service_id` where `user_services`.`user_id` in ('428', '427', '341', '41', '28', '20', '6', '4', '3', '2') and `services`.`deleted_at` is null
800μs

select * from `services` where `lft` >= '2' and `lft` < '3' and `id` != '2' and `services`.`deleted_at` is null order by `services`.`lft` asc
670μs

select * from `services` where `lft` >= '4' and `lft` < '5' and `id` != '3' and `services`.`deleted_at` is null order by `services`.`lft` asc
470μs

select * from `services` where `lft` >= '6' and `lft` < '7' and `id` != '4' and `services`.`deleted_at` is null order by `services`.`lft` asc
500μs

select * from `services` where `lft` >= '8' and `lft` < '9' and `id` != '5' and `services`.`deleted_at` is null order by `services`.`lft` asc
580μs

select * from `services` where `lft` >= '10' and `lft` < '11' and `id` != '6' and `services`.`deleted_at` is null order by `services`.`lft` asc
710μs

select * from `services` where `lft` >= '12' and `lft` < '15' and `id` != '7' and `services`.`deleted_at` is null order by `services`.`lft` asc
520μs

select * from `services` where `lft` >= '16' and `lft` < '17' and `id` != '9' and `services`.`deleted_at` is null order by `services`.`lft` asc
470μs

select * from `services` where `lft` >= '18' and `lft` < '19' and `id` != '10' and `services`.`deleted_at` is null order by `services`.`lft` asc
460μs

select * from `services` where `lft` >= '20' and `lft` < '21' and `id` != '11' and `services`.`deleted_at` is null order by `services`.`lft` asc
800μs

select * from `services` where `lft` >= '22' and `lft` < '23' and `id` != '12' and `services`.`deleted_at` is null order by `services`.`lft` asc
630μs

select * from `services` where `lft` >= '24' and `lft` < '27' and `id` != '13' and `services`.`deleted_at` is null order by `services`.`lft` asc
650μs

select * from `services` where `lft` >= '28' and `lft` < '31' and `id` != '15' and `services`.`deleted_at` is null order by `services`.`lft` asc
730μs

select * from `services` where `lft` >= '32' and `lft` < '35' and `id` != '17' and `services`.`deleted_at` is null order by `services`.`lft` asc
460μs

select * from `services` where `lft` >= '36' and `lft` < '39' and `id` != '19' and `services`.`deleted_at` is null order by `services`.`lft` asc
500μs

select * from `services` where `lft` >= '40' and `lft` < '43' and `id` != '21' and `services`.`deleted_at` is null order by `services`.`lft` asc
760μs

select * from `services` where `lft` >= '44' and `lft` < '47' and `id` != '23' and `services`.`deleted_at` is null order by `services`.`lft` asc
630μs

select * from `services` where `lft` >= '48' and `lft` < '51' and `id` != '25' and `services`.`deleted_at` is null order by `services`.`lft` asc
550μs

select * from `services` where `lft` >= '52' and `lft` < '53' and `id` != '27' and `services`.`deleted_at` is null order by `services`.`lft` asc
560μs

select * from `services` where `lft` >= '54' and `lft` < '55' and `id` != '28' and `services`.`deleted_at` is null order by `services`.`lft` asc
490μs

select * from `services` where `lft` >= '56' and `lft` < '57' and `id` != '29' and `services`.`deleted_at` is null order by `services`.`lft` asc
500μs

select * from `services` where `lft` >= '58' and `lft` < '59' and `id` != '30' and `services`.`deleted_at` is null order by `services`.`lft` asc
430μs

select * from `services` where `lft` >= '60' and `lft` < '63' and `id` != '31' and `services`.`deleted_at` is null order by `services`.`lft` asc
490μs

select * from `services` where `lft` >= '64' and `lft` < '65' and `id` != '33' and `services`.`deleted_at` is null order by `services`.`lft` asc
480μs