Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nestedToArray to treecollection #193

Merged

Conversation

damsfx
Copy link
Contributor

@damsfx damsfx commented Nov 21, 2024

Added a new method to the TreeCollection class to obtain the collection in a nested array.

In order to add a tree view mode to the checkboxlist formwidget, I need to be able to get the collection items as a nested tree.

$tree = new Winter\Test\Models\Channel;
$tree->all();  // Winter\Storm\Database\TreeCollection

$tree->nestedArray(title', 'id'); 
// or 
$tree->all()->toNestedArray('title', 'id');
// return an nested array
[
    1 => [
      "title" => "Channel Orange",
      "children" => [
        2 => [
          "title" => "Autumn Leaves",
          "children" => [
            3 => [
              "title" => "September",
            ],
            4 => [
              "title" => "Winter",
            ],
            5 => [
              "title" => "November",
            ],
          ],
        ],
        6 => [
          "title" => "Summer Breeze",
        ],
      ],
    ],
    7 => [
      "title" => "Channel Green",
      "children" => [
        8 => [
          "title" => "Winter Snow",
        ],
        9 => [
          "title" => "Spring Trees",
        ],
      ],
    ],
  ]

Can be used with multiple column names to be returned in the array:

$tree->nestedArray(['title', 'description'], 'id'),
// or 
$tree->all()->toNestedArray(['title', 'description'], 'id');

Can also be used without key :

$tree->nestedArray(title'); 
// or 
$tree->all()->nestedToArray('title');

$tree->nestedArray(['title', 'description']),
// or 
$tree->all()->nestedToArray(['title', 'description']);

@damsfx damsfx changed the title Add nestedd toarray treecollection Add nestedToArray to treecollection Nov 21, 2024
@LukeTowers LukeTowers added this to the 1.2.7 milestone Nov 22, 2024
@LukeTowers LukeTowers merged commit 7ed916a into wintercms:develop Nov 22, 2024
10 checks passed
@LukeTowers
Copy link
Member

Looks great @damsfx! Could you PR the docs as well?

@damsfx
Copy link
Contributor Author

damsfx commented Nov 22, 2024

Looks great @damsfx! Could you PR the docs as well?

wintercms/docs#221 for the trait.
I don't really know how to describe the possible parameters.

For the api/Winter/Storm/Database/TreeCollection collection ... is documentation generated automatically?

@LukeTowers
Copy link
Member

@damsfx
Copy link
Contributor Author

damsfx commented Nov 23, 2024

@LukeTowers I don't see the new toNestedArray() method on my side.

@LukeTowers
Copy link
Member

@damsfx I think I need to look into what triggers it to regenerate the API docs

@damsfx damsfx deleted the add-nestedd-toarray-treecollection branch November 25, 2024 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants