skeeks\cms\models\Tree::setAttributesForFutureParent PHP Method

setAttributesForFutureParent() public method

Установка атрибутов если родителем этой ноды будет новый, читаем родителя, и обновляем необходимые данные у себя
public setAttributesForFutureParent ( Tree $parent )
$parent Tree
    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;
    }

Usage Example

Example #1
0
 /**
  * Создание дочерней ноды
  *
  * @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;
 }