creocoder / yii2-nested-sets

The nested sets behavior for the Yii framework.
Other
446 stars 129 forks source link

Make Root on Insert param #107

Open konkov-alexey opened 7 years ago

konkov-alexey commented 7 years ago
    /**
     * @var bool
     * whether to automatically set operation to OPERATION_MAKE_ROOT
     * on saving new records, so you could use
     *      $model->save();
     * instead of
     *      if ($model->isNewRecord) {
     *          $model->makeRoot();
     *      } else {
     *          $model-save();
     *      }
     */
    public $makeRootOnInsert = false;

then in before insert

    public function beforeInsert()
    {
        if ($this->makeRootOnInsert && !in_array($this->operation, [
                self::OPERATION_MAKE_ROOT,
                self::OPERATION_PREPEND_TO,
                self::OPERATION_APPEND_TO,
                self::OPERATION_INSERT_BEFORE,
                self::OPERATION_INSERT_AFTER,
            ])) {
            $this->operation = self::OPERATION_MAKE_ROOT;
        }
        /* ... */
    }