Skip to content
This repository has been archived by the owner on Oct 8, 2023. It is now read-only.

Prevent cycles 2 #29

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions TreeGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,16 +390,22 @@ protected function guessColumns()

/**
* Normalize tree data
*
* @param array $data
* @param string $parentId
* @param null $parentId
* @param array $backWalk Already passed nodes
* @return array
*/
protected function normalizeData(array $data, $parentId = null) {
protected function normalizeData(array $data, $parentId = null, $backWalk = []) {
$result = [];
foreach ($data as $element) {
if (in_array(ArrayHelper::getValue($element, $this->keyColumnName), $backWalk)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$keyValue = ArrayHelper::getValue($element, $this->keyColumnName);
if (in_array($keyValue, $backWalk)) {

continue;
}
if (ArrayHelper::getValue($element, $this->parentColumnName) == $parentId) {
$result[] = $element;
$children = $this->normalizeData($data, ArrayHelper::getValue($element, $this->keyColumnName));
$backWalk[] = ArrayHelper::getValue($element, $this->keyColumnName);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$backWalk[] = ArrayHelper::getValue($element, $keyValue);

$children = $this->normalizeData($data, ArrayHelper::getValue($element, $this->keyColumnName), $backWalk);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$children = $this->normalizeData($data, $keyValue, $backWalk);

if ($children) {
$result = array_merge($result, $children);
}
Expand Down