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