arrays icon indicating copy to clipboard operation
arrays copied to clipboard

Add 'nest' method to ArrayHelper

Open johonunu opened this issue 8 years ago • 6 comments

I think we would be nice to introduce method 'nest' similar to CakePHP one to ArrayHelper. It is used to create threaded data (with children nodes).

More information regarding it: https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::nest https://github.com/cakephp/utility/blob/master/Hash.php#L1172

Is it possible to get same response using ArrayHelper::map somehow ?

johonunu avatar Mar 23 '17 13:03 johonunu

Thank you for putting effort in the improvement of the Yii framework. We have reviewed your pull request.

Unfortunately a use case is missing. It is required to get a better understanding of the pull request and helps us to determine the necessity and applicability of the suggested change to the framework.

Could you supply us with a use case please? Please be as detailed as possible and show some code!

Thanks!

This is an automated comment, triggered by adding the label pr:missing usecase.

yii-bot avatar Mar 23 '17 16:03 yii-bot

@samdark

Here are some use cases:

  • When you want to generate/print multi-level structure for comments
  • When you want to generate/print multi-level menu
  • When you want to generate tree (nested set) for use with js plugins: https://mbraak.github.io/jqTree/ http://mjsarfatti.com/sandbox/nestedSortable/

johonunu avatar Mar 23 '17 18:03 johonunu

  • When you want to generate/print multi-level structure for comments
  • When you want to generate/print multi-level menu

Why aren't you iterating and outputting it right away?

protected function renderComments($parentID = null, $i = 0)
{
    $out = '';
    foreach ($this->comments as $comment) {
        if ($comment->parent_id === $parentID) {
            
            $out .= $this->render('comment', [
                'comment' => $comment, 'class' => $parentID !== null && $i !== 0 ? 'child-comment' : ''
            ]);
            $i++;
            $out .= $this->renderComments($comment->id, $i);
        }
    }
    
    return $out;
}
  • When you want to generate tree (nested set) for use with js plugins

OK, that one makes sense...

samdark avatar Mar 24 '17 10:03 samdark

Is it possible to get same response using ArrayHelper::map somehow ?

No.

samdark avatar Mar 24 '17 10:03 samdark

@johonunu CakePHP releases their utility classes in a separate package; you could use that.

https://packagist.org/packages/cakephp/utility

SamMousa avatar Mar 30 '17 11:03 SamMousa

@SamMousa I know that, I just wanted to note that it would be good idea to have it in the core. Most of the methods in Hash class are available inside ArrayHelper, but something like "nest" isn't. I can live without it in core, if I am the only one missing it ;)

johonunu avatar Mar 31 '17 20:03 johonunu