ivankff/yii2-external-crud

yii2 后端的外部创建、更新、删除、查看操作

安装: 90

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:yii2-extension

0.2.4 2021-02-20 09:47 UTC

This package is auto-updated.

Last update: 2024-09-20 18:03:13 UTC


README

控制器

public function actions()
{
    return [
        ...
        'create' => [
            'class' => 'ivankff\yii2ExternalCrud\actions\CreateAction',
            'view' => 'update',
            'additionalQueryParams' => ['F.categoryId'],
            'model' => function ($action) {
                $categoryId = Yii::$app->request->get('categoryId');
                $config = [];
                if ($categoryId)
                    $config = ['categories' => $categoryId];

                $model = new ProductForm(new Product(), $config);
                $model->status = 1;

                if (Yii::$app->user->can('backend.product.webmaster'))
                    $model->setScenario(ProductForm::SCENARIO_WEBMASTER);

                return $model;
            },
            'on ' . WriteAction::EVENT_BEFORE_VIEW => function ($event) {
                /** @var ActionWriteViewEvent $event */
                $event->viewParams['categories'] = $this->getCategories();
            }
        ],
        'update' => [
            'class' => 'ivankff\yii2ExternalCrud\actions\UpdateAction',
            'view' => 'update',
            'additionalQueryParams' => ['F.categoryId'],
            'model' => function ($id, $action) {
                $model = new ProductForm($this->findModel($id));

                if (Yii::$app->user->can('backend.product.webmaster'))
                    $model->setScenario(ProductForm::SCENARIO_WEBMASTER);

                return $model;
            },
            'on ' . WriteAction::EVENT_BEFORE_VIEW => function ($event) {
                /** @var ActionWriteViewEvent $event */
                $event->viewParams['categories'] = $this->getCategories();
            }
        ],
        'delete' => [
            'class' => 'ivankff\yii2ExternalCrud\actions\DeleteAction',
            'model' => function ($id, $action) {
                return $this->findModel($id);
            },
        ],
        ...
    ];
}