C
C
cat_crash2011-10-27 10:00:51
Yii
cat_crash, 2011-10-27 10:00:51

Yii and NestedSetBehavior - moving branches?

Friends, after a week of fighting with NestedSetBehavior, I completely despaired ...
The essence of the problem is how to use it to transfer one branch with the entire nested set from one parent to another.
The manual mentions the code:

$model = $this-&gt;loadModel($id);<br/>
$model-&gt;deleteNode();<br/>
$model-&gt;refresh();<br/>
$parent = $this-&gt;loadModel(intval($_POST['Category']['parentId']));<br/>
$parent-&gt;append($model);<br/>
$model-&gt;refresh();<br/>

But in fact it throws an error that I don't understand
2011/10/26 19:27:15 [error] [exception.CDbException] exception 'CDbException' with message 'The node cannot be inserted because it is not new.' in /home/prj/www/protected/extensions/trees/NestedSetBehavior.php:601
Stack trace:
#0 /home/prj/www/protected/extensions/trees/NestedSetBehavior.php(324): NestedSetBehavior->addNode(Object (Category), '2', 1, true, NULL)
#1 /home/prj/yii/framework/base/CComponent.php(264): NestedSetBehavior->appendTo(Object(Category), true, NULL)
#2 /home/prj/yii/framework/db/ar/CActiveRecord.php(214): CComponent->__call(Array, Array)
#3 [internal function]: CActiveRecord->__call('appendTo', Array)
#4 /home/prj/www/protected/extensions/trees/NestedSetBehavior.php(336): Category->appendTo('appendTo', Array)
#5 /home/prj/yii/framework/base/CComponent.php( 264): NestedSetBehavior->append(Object(Category), true, NULL)
#6 /home/prj/yii/framework/db/ar/CActiveRecord.php(214): CComponent->__call(Object(Category))
# 7 [internal function]: CActiveRecord->__call(Array, Array)
#8 /home/prj/www/protected/modules/admin/controllers/CategoryController.php(91): Category->append('append', Array)
#9 [internal function]: CategoryController->actionUpdate('append', Array)
#10 /home/prj/yii/framework/web/actions/CAction.php(104): ReflectionMethod->invokeArgs(Object(Category))
#11 /home/prj/yii/framework/web/actions/CInlineAction.php(48): CAction->runWithParamsInternal('16')
#12 /home/prj/yii/framework/web/CController.php(300) : CInlineAction->runWithParams(Object(CategoryController), Array)
#13 /home/prj/yii/framework/web/filters/CFilterChain.php(134): CController->runAction(Object(CategoryController), Object(ReflectionMethod), Array)
#14 /home/prj/yii/framework/web/filters/CFilter.php(41): CFilterChain->run(Array)
#15 /home/prj/www/protected/components/Controller.php(43) : CFilter->filter(Object(CInlineAction))
#16 /home/prj/yii/framework/web/filters/CInlineFilter.php(59): Controller->filterUserGroupsAccessControl()
#17 /home/prj/yii/framework/ web/filters/CFilterChain.php(131): CInlineFilter->filter(Object(CFilterChain))
#18 /home/prj/yii/framework/web/CController.php(283): CFilterChain->run(Object(CFilterChain))
#19 /home/prj/yii/framework/web/CController.php(257): CController->runActionWithFilters(Object(CFilterChain))
#20 /home/prj/yii/framework/web/CWebApplication.php(277): CController->run()
#21 /home/prj/yii/framework/web/CWebApplication .php(136): CWebApplication->runController(Object(CInlineAction), Array)
#22 /home/prj/yii/framework/base/CApplication.php(158): CWebApplication->processRequest('update')
#23 / home/prj/www/index.php(30): CApplication->run('admin/category/...')
#24 {main} REQUEST_URI=/admin/category/update/id/16
Who struggled and figured it out?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Gubarev, 2014-12-30
@cat_crash

The whole point is that you are using the append method on a non-new (saved) model.
To move a saved model, you must use the move* methods, such as moveAfter, moveAsFirst, and so on.
Example. To change the parent, you can do this:

$model->moveAsFirst($parent); // вместо $parent->append($model);

D
DEVIANCE, 2011-10-27
@DEViANCE

See the documentation github.com/yiiext/nested-set-behavior/blob/master/readme_ru.md section "Methods modifying a tree", there are examples.
There is also a Russian-language Yii forum and a jabber conference, where you can also ask questions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question