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

getAncestorsAndSelf() not working correctly #148

Closed flyingL123 closed 9 years ago

flyingL123 commented 9 years ago

I'm not sure why, but getAncestorsAndSelf() does not seem to be working correctly for me. I have a category object who's parent ID is equal to my root object ID. Here are the entries in my database showing the category in question as well as the root category:

+------+-----------+------+------+-------+---------------+
| id   | parent_id | lft  | rgt  | depth | name          |
+------+-----------+------+------+-------+---------------+
| 3078 |      NULL |    1 |  323 |     0 | ROOT CATEGORY |
| 3400 |      3078 |  319 |  320 |     1 | Sunglasses    |
+------+-----------+------+------+-------+---------------+

Now, when I select category ID 3400 from the database, and call getAncestorsAndSelf():

$cat = Category::find(3400);
$cat->getAncestorsAndSelf()->toArray();

I get the following response:

array(
     0 => array(
       'id' => '3078',
       'ext_id' => '0',
       'ext_type_id' => '0',
       'parent_id' => NULL,
       'lft' => '1',
       'rgt' => '323',
       'depth' => '0',
       'name' => 'ROOT CATEGORY',
       'google_category_id' => '',
       'amazon_browse_node_id' => '',
       'created_at' => '2015-01-04 06:01:57',
       'updated_at' => '2015-01-05 15:06:12',
       'deleted_at' => NULL
     ),
     1 => array(
       'id' => '3082',
       'ext_id' => '21',
       'ext_type_id' => '1',
       'parent_id' => '3078',
       'lft' => '40',
       'rgt' => '441',
       'depth' => '1',
       'name' => 'Surfboard',
       'google_category_id' => 'Sporting Goods > Water Sports > Surfing',
       'amazon_browse_node_id' => '3418471',
       'created_at' => '2015-01-05 15:06:03',
       'updated_at' => '2015-02-04 20:04:21',
       'deleted_at' => NULL
     ),
     2 => array(
       'id' => '3088',
       'ext_id' => '64',
       'ext_type_id' => '1',
       'parent_id' => '3082',
       'lft' => '51',
       'rgt' => '372',
       'depth' => '2',
       'name' => 'Surfboard Bags',
       'google_category_id' => 'Sporting Goods > Water Sports > Surfing > Surfboard Cases & Bags',
       'amazon_browse_node_id' => '3418471',
       'created_at' => '2015-01-05 15:06:03',
       'updated_at' => '2015-02-04 20:04:21',
       'deleted_at' => NULL
     ),
     3 => array(
       'id' => '3400',
       'ext_id' => '367',
       'ext_type_id' => '1',
       'parent_id' => '3078',
       'lft' => '319',
       'rgt' => '320',
       'depth' => '1',
       'name' => 'Sunglasses',
       'google_category_id' => '',
       'amazon_browse_node_id' => '',
       'created_at' => '2015-01-05 15:06:11',
       'updated_at' => '2015-01-05 15:06:12',
       'deleted_at' => NULL
     )
   )

Here are those 4 categories in my database:

+------+-----------+------+------+-------+----------------+
| id   | parent_id | lft  | rgt  | depth | name           |
+------+-----------+------+------+-------+----------------+
| 3078 |      NULL |    1 |  323 |     0 | ROOT CATEGORY  |
| 3082 |      3078 |   40 |  441 |     1 | Surfboard      |
| 3088 |      3082 |   51 |  372 |     2 | Surfboard Bags |
| 3400 |      3078 |  319 |  320 |     1 | Sunglasses     |
+------+-----------+------+------+-------+----------------+

I don't understand why I am getting all of these categories back when the parent_id for Category 3400 is the root category. Why are the other ones appearing in the response? Is this a bug?

flyingL123 commented 9 years ago

Looks like my lft and rgt values were out of whack. I just ran Category::rebuild and it fixed everything. Thanks for that awesome utility!