Giter Site home page Giter Site logo

Tree rebuilding problem about nested-set HOT 10 CLOSED

rundiz avatar rundiz commented on August 22, 2024
Tree rebuilding problem

from nested-set.

Comments (10)

ve3 avatar ve3 commented on August 22, 2024

Hello,

These are examples in tests folders
https://github.com/Rundiz/nested-set/tree/version1/tests/phpunit/PHP71
https://github.com/Rundiz/nested-set/tree/version1/tests/phpunit/PHPB71
and https://github.com/Rundiz/nested-set/tree/version1/tests/via-http

In case you want quick fix you can compare with those code.

............................

However, Do you have sample DB data for run on my side to check and test?
Please included all relevant code & sample DB data to reproduce the problem because currently I cannot check anything with little data/debug info.

Do you use listTaxonomy(), or listTaxonomyFlatten() to list the data? If you check from samples in the tests folder, they use these methods to listing properly.

This class don't have reBuildPosition() method. With just the code called to that method, I cannot check that what it is.

If you call rebuild() without any arguments, it will be rebuild all data in selected table. So, it should be display correctly if you listing with certain methods or correctly use order by left column.

from nested-set.

AlexJMcloud avatar AlexJMcloud commented on August 22, 2024
public function reBuildPosition(int $id, int $pos, int $parent): void
    {
        $getParents = $this->pdo->table($this->tableName)->
            where($this->parentIdColumnName . '= ? AND '. $this->positionColumnName . '>= ? AND '. $this->idColumnName . '!= ?', [$parent, $pos, $id])->getAll();
        if($getParents)
        {
            $i = $pos+1;
            foreach ($getParents as $item)
            {
                $this->pdo->table($this->tableName)->where($this->idColumnName, $item->{$this->idColumnName})->
                update([$this->positionColumnName => $i]);
                $i++;
            }
        }
    }

I have already added this class method myself. Taking as a basis the work of the class (when rebuilding the tree, an array of data is obtained based on the position of the element).

from nested-set.

AlexJMcloud avatar AlexJMcloud commented on August 22, 2024

And to display the tree, I use a very simple query.
Select * From brand Order by bra_lft

from nested-set.

AlexJMcloud avatar AlexJMcloud commented on August 22, 2024

Your method uses data based on element position: getTreeWithChildren.
After receiving the data, you are rebuilding the tree based on the array obtained by this method.

from nested-set.

ve3 avatar ve3 commented on August 22, 2024

The position number is based on the same level only. That means if you have 1.1 that is under parent Root 1, its position must be always start from 1.

I have added the sample code of management that is included Create/Read/Update/Delete.
See https://github.com/Rundiz/nested-set/tree/version1/tests/via-http/management

I've tested and everything work as expect but please read the note about manually process position number.

from nested-set.

AlexJMcloud avatar AlexJMcloud commented on August 22, 2024

Thank you. But I will have to do all the same automatic change of positions within the same level.

from nested-set.

AlexJMcloud avatar AlexJMcloud commented on August 22, 2024

I found the problem. When getting an array for position renumbering, I forgot to do the sorting. Added and everything worked as it should. So you can add this method to the class to automatically rebuild the position within the same level.

from nested-set.

AlexJMcloud avatar AlexJMcloud commented on August 22, 2024

`
public function reBuildPosition(int $id, int $pos, int $parent): void
{
$getParents = $this->pdo->table($this->tableName)->
where($this->parentIdColumnName . '= ? AND '. $this->positionColumnName . '>= ? AND '. $this->idColumnName . '!= ?', [$parent, $pos, $id])
->orderBy($this->positionColumnName)->getAll();
if($getParents)
{
$i = $pos+1;
foreach ($getParents as $item)
{
$this->pdo->table($this->tableName)->where($this->idColumnName, $item->{$this->idColumnName})->
update([$this->positionColumnName => $i]);
$i++;
}
}
}

`

from nested-set.

ve3 avatar ve3 commented on August 22, 2024

Thank you for your participation but I couldn't use that code because some reasons.

  1. That code is based on something like ORM or maybe query builder that pdo property isn't native PHP's \PDO class.
  2. There are a lot of ways to re-order position and some JS use or send data differently. So, it is not easy to handle these data and rebuild all the position number when re-order position. Unlike rebuild level, left, right that no one should manually update them and just let this class update the data because they need special algorithm to calculate.

I've tried to create that before but in the end, it will not work if some JS send weird data and it looks like the position can be update in many ways.

Thanks again.

from nested-set.

AlexJMcloud avatar AlexJMcloud commented on August 22, 2024

Thank you for your participation but I couldn't use that code because some reasons.

  1. That code is based on something like ORM or maybe query builder that pdo property isn't native PHP's \PDO class.
  2. There are a lot of ways to re-order position and some JS use or send data differently. So, it is not easy to handle these data and rebuild all the position number when re-order position. Unlike rebuild level, left, right that no one should manually update them and just let this class update the data because they need special algorithm to calculate.

I've tried to create that before but in the end, it will not work if some JS send weird data and it looks like the position can be update in many ways.

Thanks again.

The query is really built not on pure \PDO, but on the query builder (php \PDO basis). But I can help with sql:
Select * FROM ' . $this->table . ' WHERE ' . $this->parentIdColumnName . '= ' . $parent . ' AND '. $this->positionColumnName . '>= ' . $pos . ' AND '. $this->idColumnName . '!= ' . $id . ';'
And so I have implemented without js. A form is filled in, where there is a field with the ability to put down the position number.

I send the element id, position number and parent element number to the function.

from nested-set.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.