baum icon indicating copy to clipboard operation
baum copied to clipboard

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

Open jcwatson11 opened this issue 8 years ago • 3 comments

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 avatar Jan 11 '18 23:01 jcwatson11

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

jcwatson11 avatar Feb 01 '18 21:02 jcwatson11

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

Troyer-x avatar Apr 04 '18 07:04 Troyer-x

Guys checkout v2 :)

mkwsra avatar May 10 '19 22:05 mkwsra