You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In a few projects I had a form with parent category search field, and you need to move node or insert in exact position while creating or updating model. If you don't use nested sets and use only parentId field there is no problem at all, but with pure nested sets you should choose what operation to use in every case. After all I've come to this simple solution and just want to share it.
For simplicity you can add parentId field (not necessary to database!) and use this overridden behavior.
After this you can set parentId value and use just a save() method anywhere instead of many others.
Overridden behavior
class NestedSetsBehavior extends \creocoder\nestedsets\NestedSetsBehavior
{
/** * Simplifying saving by checking parentId field if operation is not set * or modifying field value otherwise */protectedfunctioncheckOperation() {
/** @var Category $owner */$owner = $this->owner;
if (!in_array($this->operation, [
self::OPERATION_MAKE_ROOT,
self::OPERATION_PREPEND_TO,
self::OPERATION_APPEND_TO,
self::OPERATION_INSERT_BEFORE,
self::OPERATION_INSERT_AFTER,
])
) {
if ($owner->parentId) {
$this->operation = self::OPERATION_APPEND_TO;
$this->node = Category::findOne($owner->parentId);
} else {
$owner->parentId = null;
$this->operation = self::OPERATION_MAKE_ROOT;
}
}
if ($this->operation == self::OPERATION_MAKE_ROOT) {
$owner->parentId = null;
} elseif ($this->node && in_array($this->operation, [
self::OPERATION_PREPEND_TO,
self::OPERATION_APPEND_TO,
])
) {
$owner->parentId = $this->node->id;
} elseif ($this->node && in_array($this->operation, [
self::OPERATION_INSERT_BEFORE,
self::OPERATION_INSERT_AFTER,
])
) {
$owner->parentId = $this->node->parentId;
}
}
/** * * @inheritDoc */publicfunctionbeforeInsert() {
$this->checkOperation();
parent::beforeInsert();
}
/** * @inheritDoc */publicfunctionbeforeUpdate() {
$this->checkOperation();
parent::beforeUpdate();
}
}
PS: i asked this question some time ago here, and see people looking out here and as i'm not the only one, who wonders, just wanted to share my solution of this issue.
The text was updated successfully, but these errors were encountered:
In a few projects I had a form with parent category search field, and you need to move node or insert in exact position while creating or updating model. If you don't use nested sets and use only parentId field there is no problem at all, but with pure nested sets you should choose what operation to use in every case. After all I've come to this simple solution and just want to share it.
For simplicity you can add parentId field (not necessary to database!) and use this overridden behavior.
After this you can set parentId value and use just a save() method anywhere instead of many others.
Overridden behavior
In controller
In view
PS: i asked this question some time ago here, and see people looking out here and as i'm not the only one, who wonders, just wanted to share my solution of this issue.
The text was updated successfully, but these errors were encountered: