I have problem when implement a NestedSetBehavior from incubator libraries. My Phalcon version is 1.3.3.
I create a folder "nested" that have 5 files (.htaccess, index.php, CategoriesController.php, Categories.php, and NestedSetBehavior.php). and i create categories table :
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL,
`lft` int(11) NOT NULL,
`rgt` int(11) NOT NULL,
`level` int(11) NOT NULL,
`title` varchar(45) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
index.php file :
<?php
$loader=new \Phalcon\Loader();
$loader->registerDirs(array(__DIR__))->register();
$di = new \Phalcon\DI\FactoryDefault();
$di->set('db', function() {
return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
"host" => "localhost",
"username" => "root",
"password" => "jacky^45",
"dbname" => "dealer_acl"
));
});
$di->set('view', function() {
return new \Phalcon\Mvc\View();
});
$app = new \Phalcon\Mvc\Application($di);
echo $app->handle()->getContent();
CategoriesController.php file :
<?php
class CategoriesController extends Phalcon\Mvc\Controller
{
public function indexAction()
{
$category = new Categories();
$category->title='Cars';
$category->saveNode();
}
}
Categories.php file :
<?php
class Categories extends Phalcon\Mvc\Model
{
public $id;
public $lft;
public $rgt;
public $level;
public $title;
public function initialize()
{
$this->addBehavior(new NestedSetBehavior(array(
'leftAttribute' => 'lft',
'rightAttribute' => 'rgt',
'levelAttribute' => 'level'
)));
}
}
NestedSetBehavior.php i have copied from https://raw.githubusercontent.com/phalcon/incubator/master/Library/Phalcon/Mvc/Model/Behavior/NestedSet.php with any modification :
- delete namespace on line 3
- change nameclass from NestedSet to NestedSetBehavior
- change the value of hasmanyRoots, from false to true
private $hasManyRoots=true;
while i run https://localhost/nested/categories, get error message "There is no active transaction", but data inserted on database. So i try to add transaction code in controller:
public function indexAction()
{
$trManager = new \Phalcon\Mvc\Model\Transaction\Manager();
$tr = $trManager->get();
$category = new Categories();
$category->setTransaction($tr);
$category->title='Laptop';
$category->saveNode();
}
error message show "Fatal error: Call to a member function update() on a non-object in... on line 796".
code in line 796 is : $owner::findFirst($pk)->update(array($this->rootAttribute => $pk));
.
if i debug, $pk
variable has a integer value. but operation of $owner::findFirst($pk)
is failed to get a record. if i var_dump($owner::findFirst($pk))
get bool(false) result. So operation of update is getting fatal error.
I try another command, appendTo() and change my controller :
public function indexAction()
{
$trManager = new \Phalcon\Mvc\Model\Transaction\Manager();
$tr = $trManager->get();
$root = new Categories();
$root = $root->findFirst();
$category= new Categories();
$category->setTransaction($tr);
$category->title='BMW';
$category->appendTo($root);
}
error message show Query syntaxt error. if i debug query show like this "UPDATE categories SET lft=lft+2 WHERE lft>=2 AND root="
anyone can help me to implement NestedSetBehavior? Thanks.