Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Указание действия, вместо сохранения #84

Closed
brussens opened this issue Dec 3, 2015 · 4 comments
Closed
Assignees

Comments

@brussens
Copy link

brussens commented Dec 3, 2015

Александр, доброго времени!
Собственно предложение такое: вместо сохранения методами makeRoot(), prependTo(), appendTo(), insertBefore(), insertAfter(), использовать их просто для указания действия.
Дело в том, что поведение очень круто работает с виджетами, типа JTreeView, но вот если нужно использовать его в Create и Update экшенах получается вроде того:

class CategoryController extends BackendController
{
    public function actionCreate()
    {
        $model = new Category;

        if($model->load(Yii::$app->getRequest()->post()) && $model->validate()) {

            $parent_id = $model->getAttribute('parent_id');

            if($parent_id) {
                $result = $model->appendTo($parent_id, false);
            }
            else {
                $result = $model->makeRoot(false);
            }
            if($result) {
                //...
            }
        }
    }
}

А хочется лаконичного использования. Если использовать подход, описанный выше, то можно получить подобное:

class Category extends ActiveRecord
{
    public function beforeSave()
    {
        parent::beforeSave();

        if($this->getAttribute('parent_id')) {
            $this->appendTo();
        }
        else {
            $this->makeRoot();
        }
    }
}

class CategoryController extends BackendController
{
    public function actionCreate()
    {
        $model = new Category;

        if($model->load(Yii::$app->getRequest()->post()) && $model->save()) {
            Yii::$app->getSession()->setFlash('success', 'Category has been saved!');
            return $this->redirect(['index']);
        }
    }
}

Так получат своё те, кто использует parent_id, что, лично для меня, довольно удобно при манипуляциях.
Возможно можно будет просто указать дополнительный параметр в этих методах, который и будет отвечать за то, сохранять ли методом модель, или просто делать пометку для дальнейшего сохранения.
Код выше накидал на скорую руку, так что не бросайте помидорами)
Заранее спасибо.

@creocoder creocoder self-assigned this Dec 3, 2015
@brussens
Copy link
Author

brussens commented Dec 3, 2015

Может пулл кинуть? Там впринципе то для сохранения не много надо.

@brussens brussens mentioned this issue Dec 3, 2015
@yujin1st
Copy link

#97

@brussens
Copy link
Author

Not the right decision to override the behavior of the conservation method that entails errors and difficulties, when working with this behavior. Therefore it is necessary to prevent overlap save method.

@creocoder
Copy link
Owner

Its not BC, sorry. Will consider that approach in next versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants