We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Can not save record with incubator nested set behaviour

I use incubator nested set behaviour to create a category

I have setup nestedset.php:

private $hasManyRoots = true;

and create category.php model like this:

class Category extends Model
{
    public $id;
    public $root;
    public $slug;
    public $level;
    public $lft;
    public $rgt;
    public $title;

    public function getSource()
    {
        return "category";
    }

    public function initialize()
    {
        $this->addBehavior(new NestedSetBehavior([
            'rootAttribute'  => 'root',
            'leftAttribute'  => 'lft',
            'rightAttribute' => 'rgt',
            'levelAttribute' => 'level',
        ]));
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return string
     */
    public static function getRoots()
    {
        return self::find('lft = 1');
    }

    public static function getDescendants()
    {
        $category = Categories::findFirst(lft);
        $descendants = $category->descendants();
        return $this->descendants;
    }

    public static function getChildren()
    {
        $category = Categories::findFirst(lft);
        $children = $category->children();
        return $this->children;
    }

    /**
     * @return mixed
     */
    public function getLevel()
    {
        return $this->level;
    }

    /**
     * @param mixed $depth
     */
    public function setLevel($level)
    {
        $this->level = $level;
    }

    /**
     * @return mixed
     */
    public function getLft()
    {
        return $this->lft;
    }

    /**
     * @param mixed $left_key
     */
    public function setLft($lft)
    {
        $this->lft = $lft;
    }

    /**
     * @return mixed
     */
    public function getRgt()
    {
        return $this->rgt;
    }

    /**
     * @param mixed $right_key
     */
    public function setRgt($rgt)
    {
        $this->rgt = $rgt;
    }

    /**
     * @return mixed
     */
    public function getSlug()
    {
        return $this->slug;
    }

    /**
     * @param mixed $slug
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;
    }

}

and CategoryController like this:

public function addAction()
    { 
         if ($this->request->isPost()) {
                $category = new Category([
                    'title' => $this->request->getPost('title'),
                    'position' => $this->request->getPost('position', 'int'),
                     'parent_id' => $this->request->getPost('parentid', 'int'),
                     'slug' => PhSlug::generate($this->request->getPost('title'), "-"),
                ]);

             //Save data
            if (empty($data->parent_id)){
                $category->saveNode();
            } else {
                $node = Category::findOne($data->parent_id);
             $category->appendTo($node);
          }

         $this->dispatcher->forward([
            'Controller' => 'category',
            'action' => 'index'
            ]);
    }

But the record isn't created. Can anybody help me please



43.9k

Hi,

I haven't used the nested set behavior since phalcon 3 is out there. Maybe it is related to that, because with version 2 of phalcon, it was working great.

Can you provide some debugging info (webserver logs, database logs - see here for that: https://docs.phalcon.io/en/latest/reference/db.html#logging-sql-statements) ?



43.9k

Hi again,

I've found a couple of minutes to spend on that problem. And this work:


// Category Model

<?php 
use Phalcon\Mvc\Model\Behavior\NestedSet as NestedSetBehavior; // don't you forget that ???

class Category extends Phalcon\Mvc\Model
{

    public $id;
    public $root;
    public $level;
    public $lft;
    public $rgt;
    public $title;

    public function getSource()
    {
        return "category";
    }

    public function initialize()
    {
        $this->addBehavior(new NestedSetBehavior([
            'rootAttribute'  => 'root',
            'leftAttribute'  => 'lft',
            'rightAttribute' => 'rgt',
            'levelAttribute' => 'level',
        ]));
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return string
     */
    public static function getRoots()
    {
        return self::find('lft = 1');
    }

    public static function getDescendants()
    {
        $category = Categories::findFirst(lft);
        $descendants = $category->descendants();
        return $this->descendants;
    }

    public static function getChildren()
    {
        $category = Categories::findFirst(lft);
        $children = $category->children();
        return $this->children;
    }

    /**
     * @return mixed
     */
    public function getLevel()
    {
        return $this->level;
    }

    /**
     * @param mixed $depth
     */
    public function setLevel($level)
    {
        $this->level = $level;
    }

    /**
     * @return mixed
     */
    public function getLft()
    {
        return $this->lft;
    }

    /**
     * @param mixed $left_key
     */
    public function setLft($lft)
    {
        $this->lft = $lft;
    }

    /**
     * @return mixed
     */
    public function getRgt()
    {
        return $this->rgt;
    }

    /**
     * @param mixed $right_key
     */
    public function setRgt($rgt)
    {
        $this->rgt = $rgt;
    }
}
?>

// controller action

        $category1 = new Category([
            'title' => 'category 1',
        ]);
        $category1->saveNode();

        $node = Category::findFirst();
        $category = new Category([
            'title' => 'category 2',
        ]);
        $category->appendTo($node);