etrepat / baum

Baum is an implementation of the Nested Set pattern for Laravel's Eloquent ORM.
http://etrepat.com/baum
MIT License
2.24k stars 459 forks source link

Saving a node causes it to attempt to also move the node to itself. #296

Open jcwatson11 opened 6 years ago

jcwatson11 commented 6 years ago

I'm not sure what I'm doing wrong. But if I try to update a Baum node using Laravel/Eloquent and just use the save() method, it fires the Eloquent 'saved' event, which causes Baum to call moveToNewParent. But since I'm updating a node that doesn't actually need to be moved, it errors out with:

A node cannot be moved to a descendant of itself (inside moved tree).

My solution to this problem is to modify the moveToNewParent() method like so:

public function moveToNewParent() {
    $pid = static::$moveToNewParentId;

    if ( is_null($pid) )
      $this->makeRoot();
    else if ( $pid !== FALSE ) {
      $pkey = $this->getKey();
      $childNode = static::find($pkey);
      $parentNode = static::find($pid);
      if($childNode && $parentNode && !$childNode->insideSubtree($parentNode)) {
        $this->makeChildOf($pid);
      }
    }
  }

Something doesn't feel right about this solution though, since I can't be the first to try to update a node without trying to move it.

What am I doing wrong?

jcwatson11 commented 6 years ago

It looks like this package is not longer maintained by etrepat. We decided to fork the package and make the change for ourselves.

Troyer-x commented 6 years ago

Same problem, @jcwatson11 your code solution works for me.

mkwsra commented 5 years ago

Guys checkout v2 :)