Skip to content

Problem with createFromArray method #242

@alexbabich1990

Description

@alexbabich1990

Hello
Hello, i updated the "franzose/ClosureTable" package to ^6 version.
I noted one problem, if i use createFromArray() method for creating tree, position always change.

For example:
$array = [
[
'id' => 1,
'title' => 'Much Very White',
'position' => 0,
'url' => 'color-tree',
],
[
'id' => 2,
'title' => 'Car tree',
'url' => 'car-tree',
'position' => 1,
],
[
'id' => 3,
'title' => 'Country tree',
'url' => 'country-tree',
'position' => 2
],
];

After using createFromArray() method i got:

[
'id' => 1,
'title' => 'Much Very White',
'position' => 2,
'url' => 'color-tree',
],
[
'id' => 2,
'title' => 'Car tree',
'url' => 'car-tree',
'position' => 0,
],
[
'id' => 3,
'title' => 'Country tree',
'url' => 'country-tree',
'position' => 1
],

I mean, the position field on all points was changed.

if i update vendor/franzose/closure-table/src/Models/Entity.php

    static::saving(static function (Entity $entity) {
        if ($entity->isDirty($entity->getPositionColumn())) {
            $latest = static::getLatestPosition($entity);

            if (!$entity->isMoved) {
                $latest--;
            }

            $entity->position = max(0, min($entity->position, $latest));
        } elseif (!$entity->exists) {
            $entity->position = static::getLatestPosition($entity);
        }
    });

to

    static::saving(static function (Entity $entity) {
       if (!$entity->exists) {
            $entity->position = static::getLatestPosition($entity);
        }
    });

after that createFromArray() method works correct for me.
Could you please check it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions