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 460 forks source link

toHierarchy Adds a duplicate record to the array #200

Closed 06chaynes closed 2 years ago

06chaynes commented 8 years ago

I built an array of child records using the following: $children = $organization->descendants()->filter($input)->get();

Which gives me the following:

Baum\Extensions\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\Organization Object ( [table:protected] => organizations [fillable:protected] => Array ( [0] => name )

                [hidden:protected] => Array
                    (
                        [0] => rgt
                        [1] => lft
                        [2] => depth
                    )

                [parentColumn:protected] => parent_id
                [leftColumn:protected] => lft
                [rightColumn:protected] => rgt
                [depthColumn:protected] => depth
                [orderColumn:protected] => 
                [guarded:protected] => Array
                    (
                        [0] => id
                        [1] => parent_id
                        [2] => lft
                        [3] => rgt
                        [4] => depth
                    )

                [scoped:protected] => Array
                    (
                    )

                [connection:protected] => 
                [primaryKey:protected] => id
                [perPage:protected] => 15
                [incrementing] => 1
                [timestamps] => 1
                [attributes:protected] => Array
                    (
                        [id] => 2
                        [parent_id] => 1
                        [lft] => 2
                        [rgt] => 5
                        [depth] => 1
                        [name] => Child 1
                    )

                [original:protected] => Array
                    (
                        [id] => 2
                        [parent_id] => 1
                        [lft] => 2
                        [rgt] => 5
                        [depth] => 1
                        [name] => Child 1
                    )

                [relations:protected] => Array
                    (
                    )

                [visible:protected] => Array
                    (
                    )

                [appends:protected] => Array
                    (
                    )

                [dates:protected] => Array
                    (
                    )

                [dateFormat:protected] => 
                [casts:protected] => Array
                    (
                    )

                [touches:protected] => Array
                    (
                    )

                [observables:protected] => Array
                    (
                    )

                [with:protected] => Array
                    (
                    )

                [morphClass:protected] => 
                [exists] => 1
                [wasRecentlyCreated] => 
            )

        [1] => App\Organization Object
            (
                [table:protected] => organizations
                [fillable:protected] => Array
                    (
                        [0] => name
                    )

                [hidden:protected] => Array
                    (
                        [0] => rgt
                        [1] => lft
                        [2] => depth
                    )

                [parentColumn:protected] => parent_id
                [leftColumn:protected] => lft
                [rightColumn:protected] => rgt
                [depthColumn:protected] => depth
                [orderColumn:protected] => 
                [guarded:protected] => Array
                    (
                        [0] => id
                        [1] => parent_id
                        [2] => lft
                        [3] => rgt
                        [4] => depth
                    )

                [scoped:protected] => Array
                    (
                    )

                [connection:protected] => 
                [primaryKey:protected] => id
                [perPage:protected] => 15
                [incrementing] => 1
                [timestamps] => 1
                [attributes:protected] => Array
                    (
                        [id] => 3
                        [parent_id] => 2
                        [lft] => 3
                        [rgt] => 4
                        [depth] => 2
                        [name] => Child 2
                    )

                [original:protected] => Array
                    (
                        [id] => 3
                        [parent_id] => 2
                        [lft] => 3
                        [rgt] => 4
                        [depth] => 2
                        [name] => Child 2
                    )

                [relations:protected] => Array
                    (
                    )

                [visible:protected] => Array
                    (
                    )

                [appends:protected] => Array
                    (
                    )

                [dates:protected] => Array
                    (
                    )

                [dateFormat:protected] => 
                [casts:protected] => Array
                    (
                    )

                [touches:protected] => Array
                    (
                    )

                [observables:protected] => Array
                    (
                    )

                [with:protected] => Array
                    (
                    )

                [morphClass:protected] => 
                [exists] => 1
                [wasRecentlyCreated] => 
            )

    )

)

This appears to be correct to me. Now when I cast to Hierarchy with this: $children->toHierarchy();

I get the following output:

Baum\Extensions\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\Organization Object ( [table:protected] => organizations [fillable:protected] => Array ( [0] => name )

                [hidden:protected] => Array
                    (
                        [0] => rgt
                        [1] => lft
                        [2] => depth
                    )

                [parentColumn:protected] => parent_id
                [leftColumn:protected] => lft
                [rightColumn:protected] => rgt
                [depthColumn:protected] => depth
                [orderColumn:protected] => 
                [guarded:protected] => Array
                    (
                        [0] => id
                        [1] => parent_id
                        [2] => lft
                        [3] => rgt
                        [4] => depth
                    )

                [scoped:protected] => Array
                    (
                    )

                [connection:protected] => 
                [primaryKey:protected] => id
                [perPage:protected] => 15
                [incrementing] => 1
                [timestamps] => 1
                [attributes:protected] => Array
                    (
                        [id] => 2
                        [parent_id] => 1
                        [lft] => 2
                        [rgt] => 5
                        [depth] => 1
                        [name] => Child 1
                    )

                [original:protected] => Array
                    (
                        [id] => 2
                        [parent_id] => 1
                        [lft] => 2
                        [rgt] => 5
                        [depth] => 1
                        [name] => Child 1
                    )

                [relations:protected] => Array
                    (
                        [children] => Illuminate\Database\Eloquent\Collection Object
                            (
                                [items:protected] => Array
                                    (
                                        [0] => App\Organization Object
                                            (
                                                [table:protected] => organizations
                                                [fillable:protected] => Array
                                                    (
                                                        [0] => name
                                                    )

                                                [hidden:protected] => Array
                                                    (
                                                        [0] => rgt
                                                        [1] => lft
                                                        [2] => depth
                                                    )

                                                [parentColumn:protected] => parent_id
                                                [leftColumn:protected] => lft
                                                [rightColumn:protected] => rgt
                                                [depthColumn:protected] => depth
                                                [orderColumn:protected] => 
                                                [guarded:protected] => Array
                                                    (
                                                        [0] => id
                                                        [1] => parent_id
                                                        [2] => lft
                                                        [3] => rgt
                                                        [4] => depth
                                                    )

                                                [scoped:protected] => Array
                                                    (
                                                    )

                                                [connection:protected] => 
                                                [primaryKey:protected] => id
                                                [perPage:protected] => 15
                                                [incrementing] => 1
                                                [timestamps] => 1
                                                [attributes:protected] => Array
                                                    (
                                                        [id] => 3
                                                        [parent_id] => 2
                                                        [lft] => 3
                                                        [rgt] => 4
                                                        [depth] => 2
                                                        [name] => Child 2
                                                    )

                                                [original:protected] => Array
                                                    (
                                                        [id] => 3
                                                        [parent_id] => 2
                                                        [lft] => 3
                                                        [rgt] => 4
                                                        [depth] => 2
                                                        [name] => Child 2
                                                    )

                                                [relations:protected] => Array
                                                    (
                                                        [children] => Illuminate\Database\Eloquent\Collection Object
                                                            (
                                                                [items:protected] => Array
                                                                    (
                                                                    )

                                                            )

                                                    )

                                                [visible:protected] => Array
                                                    (
                                                    )

                                                [appends:protected] => Array
                                                    (
                                                    )

                                                [dates:protected] => Array
                                                    (
                                                    )

                                                [dateFormat:protected] => 
                                                [casts:protected] => Array
                                                    (
                                                    )

                                                [touches:protected] => Array
                                                    (
                                                    )

                                                [observables:protected] => Array
                                                    (
                                                    )

                                                [with:protected] => Array
                                                    (
                                                    )

                                                [morphClass:protected] => 
                                                [exists] => 1
                                                [wasRecentlyCreated] => 
                                            )

                                    )

                            )

                    )

                [visible:protected] => Array
                    (
                    )

                [appends:protected] => Array
                    (
                    )

                [dates:protected] => Array
                    (
                    )

                [dateFormat:protected] => 
                [casts:protected] => Array
                    (
                    )

                [touches:protected] => Array
                    (
                    )

                [observables:protected] => Array
                    (
                    )

                [with:protected] => Array
                    (
                    )

                [morphClass:protected] => 
                [exists] => 1
                [wasRecentlyCreated] => 
            )

        [1] => App\Organization Object
            (
                [table:protected] => organizations
                [fillable:protected] => Array
                    (
                        [0] => name
                    )

                [hidden:protected] => Array
                    (
                        [0] => rgt
                        [1] => lft
                        [2] => depth
                    )

                [parentColumn:protected] => parent_id
                [leftColumn:protected] => lft
                [rightColumn:protected] => rgt
                [depthColumn:protected] => depth
                [orderColumn:protected] => 
                [guarded:protected] => Array
                    (
                        [0] => id
                        [1] => parent_id
                        [2] => lft
                        [3] => rgt
                        [4] => depth
                    )

                [scoped:protected] => Array
                    (
                    )

                [connection:protected] => 
                [primaryKey:protected] => id
                [perPage:protected] => 15
                [incrementing] => 1
                [timestamps] => 1
                [attributes:protected] => Array
                    (
                        [id] => 3
                        [parent_id] => 2
                        [lft] => 3
                        [rgt] => 4
                        [depth] => 2
                        [name] => Child 2
                    )

                [original:protected] => Array
                    (
                        [id] => 3
                        [parent_id] => 2
                        [lft] => 3
                        [rgt] => 4
                        [depth] => 2
                        [name] => Child 2
                    )

                [relations:protected] => Array
                    (
                        [children] => Illuminate\Database\Eloquent\Collection Object
                            (
                                [items:protected] => Array
                                    (
                                    )

                            )

                    )

                [visible:protected] => Array
                    (
                    )

                [appends:protected] => Array
                    (
                    )

                [dates:protected] => Array
                    (
                    )

                [dateFormat:protected] => 
                [casts:protected] => Array
                    (
                    )

                [touches:protected] => Array
                    (
                    )

                [observables:protected] => Array
                    (
                    )

                [with:protected] => Array
                    (
                    )

                [morphClass:protected] => 
                [exists] => 1
                [wasRecentlyCreated] => 
            )

    )

)

Notice how child 2 now has two entries, one in the correct place and a second at the end of the array. I cannot find anything that might be causing this so far. Any ideas on this?

06chaynes commented 8 years ago

I just confirmed that adding more children will also add more duplicates.

gazsp commented 8 years ago

Can you write a failing unit test to reproduce this?