michaelbaril / bonsai

This package is an implementation of the "Closure Table" design pattern for Laravel 6+ and MySQL.
32 stars 0 forks source link

getTree()'s root elements are unordered #1

Closed mikebridge closed 5 months ago

mikebridge commented 8 months ago

I'm not sure if this is expected behaviour, but I have an ordered closure table that uses the BelongsToOrderedTree trait, and I notice that the root elements of getTree are not ordered. The children are ordered however.

According to the documentation, The children relation will ... be ordered.---so maybe it doesn't apply to the root elements?

For example:

TestNode::getTree()
[
    {
        "id": 1,
        "parent_id": null,
        "name": "Test Node #1",
        "organization_id": 1,
        "order": 3,
        "deleted_at": null,
        "created_at": "2024-01-04T19:52:10+0000",
        "updated_at": "2024-01-05T20:44:25+0000",
        "resource_urn": "test-node.1",
        "children": [
            {
                "id": 4,
                "parent_id": 1,
                "name": "Test Node #4",
                "organization_id": 1,
                "order": 1,
                "deleted_at": null,
                "created_at": "2024-01-04T19:52:19+0000",
                "updated_at": "2024-01-05T20:44:34+0000",
                "resource_urn": "test-node.4",
                "closure": {
                    "ancestor_id": 1,
                    "descendant_id": 4,
                    "depth": 1
                },
                "children": []
            },
            {
                "id": 3,
                "parent_id": 1,
                "name": "Test Node #3",
                "organization_id": 1,
                "order": 2,
                "deleted_at": null,
                "created_at": "2024-01-04T19:52:16+0000",
                "updated_at": "2024-01-05T20:44:38+0000",
                "resource_urn": "test-node.3",
                "closure": {
                    "ancestor_id": 1,
                    "descendant_id": 3,
                    "depth": 1
                },
                "children": []
            },
            {
                "id": 5,
                "parent_id": 1,
                "name": "Test Node #5",
                "organization_id": 1,
                "order": 3,
                "deleted_at": null,
                "created_at": "2024-01-04T19:52:21+0000",
                "updated_at": "2024-01-05T20:44:38+0000",
                "resource_urn": "test-node.5",
                "closure": {
                    "ancestor_id": 1,
                    "descendant_id": 5,
                    "depth": 1
                },
                "children": []
            }
        ],
        "descendants": [
            {
                "id": 3,
                "parent_id": 1,
                "name": "Test Node #3",
                "organization_id": 1,
                "order": 2,
                "deleted_at": null,
                "created_at": "2024-01-04T19:52:16+0000",
                "updated_at": "2024-01-05T20:44:38+0000",
                "resource_urn": "test-node.3",
                "closure": {
                    "ancestor_id": 1,
                    "descendant_id": 3,
                    "depth": 1
                },
                "children": []
            },
            {
                "id": 4,
                "parent_id": 1,
                "name": "Test Node #4",
                "organization_id": 1,
                "order": 1,
                "deleted_at": null,
                "created_at": "2024-01-04T19:52:19+0000",
                "updated_at": "2024-01-05T20:44:34+0000",
                "resource_urn": "test-node.4",
                "closure": {
                    "ancestor_id": 1,
                    "descendant_id": 4,
                    "depth": 1
                },
                "children": []
            },
            {
                "id": 5,
                "parent_id": 1,
                "name": "Test Node #5",
                "organization_id": 1,
                "order": 3,
                "deleted_at": null,
                "created_at": "2024-01-04T19:52:21+0000",
                "updated_at": "2024-01-05T20:44:38+0000",
                "resource_urn": "test-node.5",
                "closure": {
                    "ancestor_id": 1,
                    "descendant_id": 5,
                    "depth": 1
                },
                "children": []
            }
        ]
    },
    {
        "id": 2,
        "parent_id": null,
        "name": "Test Node #2",
        "organization_id": 1,
        "order": 1,
        "deleted_at": null,
        "created_at": "2024-01-04T19:52:14+0000",
        "updated_at": "2024-01-05T20:44:23+0000",
        "resource_urn": "test-node.2",
        "children": [],
        "descendants": []
    },
    {
        "id": 6,
        "parent_id": null,
        "name": "Test Node #6",
        "organization_id": 1,
        "order": 2,
        "deleted_at": null,
        "created_at": "2024-01-04T19:52:23+0000",
        "updated_at": "2024-01-05T20:44:25+0000",
        "resource_urn": "test-node.6",
        "children": [],
        "descendants": []
    }
]
michaelbaril commented 5 months ago

Hi @mikebridge , thanks for your feedback and sorry for the late answer. You're right, they should be ordered, I guess I just didn't think of it :)

Fixed in v3.1.1