public setAttributesForFutureParent ( |
||
$parent |
public function setAttributesForFutureParent(Tree $parent)
{
//Родитель должен быть уже сохранен
if ($parent->isNewRecord) {
throw new Exception('Родитель дольжен быть сохранен');
}
if ($this->id == $parent->id) {
throw new Exception('Родитель не может быть вложен в родителя');
}
$newPids = $parent->pids;
$newPids[] = $parent->primaryKey;
$this->setAttribute("level", $parent->level + 1);
$this->setAttribute('pid', $parent->primaryKey);
$this->setAttribute("pids", $newPids);
if (!$this->name) {
$this->generateName();
}
if (!$this->code) {
//Просто генерируем pageName
$this->generateCode();
}
if ($parent->dir) {
$this->setAttribute("dir", $parent->dir . Tree::PIDS_DELIMETR . $this->code);
} else {
$this->setAttribute("dir", $this->code);
}
return $this;
}
/** * Создание дочерней ноды * * @param Tree $target * @return Tree * @throws Exception * @throws \skeeks\sx\validate\Exception */ public function processCreateNode(Tree $target) { //Текущая сущьность должна быть уже сохранена if ($this->isNewRecord) { throw new Exception('Текущая сущьность должна быть уже сохранена'); } //Новая сущьность должна быть еще не сохранена if (!$target->isNewRecord) { throw new Exception('Новая сущьность должна быть еще не сохранена'); } //Установка атрибутов будущему ребенку $target->setAttributesForFutureParent($this); if (!$target->save(false)) { throw new Exception(\Yii::t('skeeks/cms', "Failed to create the child element: ") . Json::encode($target->attributes)); } $this->save(false); return $target; }