{"id":126,"date":"2014-10-22T12:24:29","date_gmt":"2014-10-22T07:24:29","guid":{"rendered":"http:\/\/developer.uz\/blog\/?p=126"},"modified":"2014-10-22T12:24:29","modified_gmt":"2014-10-22T07:24:29","slug":"crud-for-yii2","status":"publish","type":"post","link":"https:\/\/developer.uz\/blog\/crud-for-yii2\/","title":{"rendered":"CRUD \u0434\u043b\u044f Yii2 \u0437\u0430 \u043f\u0430\u0440\u0443 \u043c\u0438\u043d\u0443\u0442"},"content":{"rendered":"<p>\u0412 \u0430\u0434\u043c\u0438\u043d\u043a\u0435 \u043b\u044e\u0431\u043e\u0433\u043e \u0441\u0430\u0439\u0442\u0430 \u0435\u0441\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c\/\u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c (CRUD &#8212; \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438). \u0412 Yii, \u0435\u0449\u0435 \u0441\u043e \u0432\u0440\u0435\u043c\u0435\u043d \u043f\u0435\u0440\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 \u0444\u0440\u0435\u0438\u043c\u0432\u043e\u0440\u043a\u0430, \u0435\u0441\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 CRUD. \u041e\u043d \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f.<br \/>\n\u041c\u043d\u0435 \u043d\u0435 \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f \u0442\u043e, \u0447\u0442\u043e \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440, \u0432 \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 (AccessControl). \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0431\u043e\u043b\u0435\u0435 2-3\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439, \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u0430\u0431\u043e\u0440 CRUD \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u0442\u043e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u0442 \u043a \u0443\u0436\u0430\u0441\u043d\u043e\u043c\u0443 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e \u043a\u043e\u0434\u0430. \u042f \u0445\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0438\u0437\u0431\u0430\u0432\u043b\u044f\u044e\u0441\u044c \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043f\u043e\u043f\u0443\u0442\u043d\u043e \u0443\u043f\u0440\u043e\u0449\u0430\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u044b\u0445 CRUD-\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439.<\/p>\n<p>\u0412\u0441\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0435\u0441\u044f \u043a\u0443\u0441\u043a\u0438 \u043a\u043e\u0434\u0430, \u0432\u044b\u043d\u0435\u0441\u043b\u0430 \u0432 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440, \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044e\u0442\u0441\u044f \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b:<\/p>\n<pre class=\"brush: php; title: \u0423 \u043c\u0435\u043d\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438\u0441\u044c \u0442\u0430\u043a\u0438\u0435 3 \u043c\u0435\u0442\u043e\u0434\u0430; notranslate\" title=\"\u0423 \u043c\u0435\u043d\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438\u0441\u044c \u0442\u0430\u043a\u0438\u0435 3 \u043c\u0435\u0442\u043e\u0434\u0430\">\r\n    public function createGridPage($model, $view, $searchModel='')\r\n    {\r\n        if($searchModel != '')\r\n        {\r\n            $searchModel = new $searchModel();\r\n            $dataProvider = $searchModel-&gt;search(Yii::$app-&gt;request-&gt;queryParams);\r\n\r\n            return $this-&gt;render($view, [\r\n                'dataProvider' =&gt; $dataProvider,\r\n                'searchModel' =&gt;  $searchModel,\r\n            ]);\r\n        }\r\n        else {\r\n            $dataProvider = new ActiveDataProvider([\r\n                'query' =&gt; $model::find(),\r\n            ]);\r\n\r\n            return $this-&gt;render($view, [\r\n                'dataProvider' =&gt; $dataProvider,\r\n            ]);\r\n        }\r\n    }\r\n\r\n    public function actionWithItemInGrid($id, $modelName, $view, $redirectAfterSave, $dopInView='')\r\n    {\r\n        if(is_numeric($id))\r\n            $model = $this-&gt;findModel($id, $modelName);\r\n        else\r\n            $model = new $modelName();\r\n\r\n        if ($model-&gt;load(Yii::$app-&gt;request-&gt;post()) &amp;&amp; $model-&gt;save()) {\r\n            Yii::$app-&gt;getSession()-&gt;setFlash('success', '\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u043e');\r\n            return $this-&gt;redirect($redirectAfterSave);\r\n        } else {\r\n            if(is_array($dopInView))\r\n                $viewParams = $dopInView;\r\n            $viewParams['model'] =  $model;\r\n\r\n            return $this-&gt;render($view, $viewParams);\r\n        }\r\n    }\r\n\r\n    public function deleteAndRedirect($id, $model, $redirectAfterDelete)\r\n    {\r\n        $this-&gt;findModel($id, $model)-&gt;delete();\r\n        return $this-&gt;redirect($redirectAfterDelete);\r\n    }\r\n<\/pre>\n<p>\u041c\u043d\u0435 \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043d\u0435 \u0441\u0430\u043c\u043e\u0439 \u043b\u0443\u0447\u0448\u0435\u0439 \u0438\u0434\u0435\u0435\u0439 \u0432\u044b\u043d\u043e\u0441\u0438\u0442\u044c \u0432 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u043d\u0433 \u0432\u044c\u044e\u0448\u0435\u043a, \u043d\u043e \u043f\u043e\u043a\u0430 \u043e\u0441\u0442\u0430\u0432\u043b\u044e \u043a\u0430\u043a \u0435\u0441\u0442\u044c. <\/p>\n<pre class=\"brush: php; title: \u041f\u0440\u0438\u043c\u0435\u0440 CRUD \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441 \u043d\u0435\u043a\u0438\u043c\u0438 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430\u043c\u0438; notranslate\" title=\"\u041f\u0440\u0438\u043c\u0435\u0440 CRUD \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441 \u043d\u0435\u043a\u0438\u043c\u0438 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430\u043c\u0438\">\r\n    public function actionPages()\r\n    {\r\n        return $this-&gt;createGridPage('common\\models\\Pages', 'pages', 'app\\models\\search\\SearchPages');\r\n    }\r\n\r\n    public function actionAddPage()\r\n    {\r\n        return $this-&gt;actionWithItemInGrid(null, 'common\\models\\Pages', 'createPage', ['pages']);\r\n    }\r\n\r\n    public function actionUpdatePage($id)\r\n    {\r\n        return $this-&gt;actionWithItemInGrid($id, 'common\\models\\Pages', 'updatePage', ['update-page', 'id' =&gt; $id]);\r\n    }\r\n\r\n    public function actionDeletePage($id)\r\n    {\r\n        return $this-&gt;deleteAndRedirect($id, 'common\\models\\Pages', ['pages']);\r\n    }\r\n<\/pre>\n<p>\u041f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438 \u0441\u0441\u044b\u043b\u043a\u0443 \u0434\u043b\u044f \u0440\u0435\u0434\u0438\u0440\u0435\u043a\u0442\u0430 \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445.<br \/>\n\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c view, \u043f\u0435\u0440\u0435\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u0438 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u043f\u0430\u043f\u043a\u0443 \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430.<br \/>\n\u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e, \u0432\u0441\u0435 \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\/\u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u043a\u0430\u043a update\/delete \u0438 \u0442\u043f. \u041f\u043e\u043d\u044f\u0442\u043d\u043e\u0435 \u0434\u0435\u043b\u043e, \u0447\u0442\u043e \u0432 \u043e\u0434\u043d\u043e\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0434\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 update. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0438 \u0432 GridView. \u042f \u0437\u0430\u0447\u0430\u0441\u0442\u0443\u044e \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u043a\u043d\u043e\u043f\u043a\u0443 {view} \u0437\u0430 \u043d\u0435\u043d\u0430\u0434\u043e\u0431\u043d\u043e\u0441\u0442\u044c\u044e. \u0418 \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0434, \u0441\u043e\u0437\u0434\u0430\u043b\u0430 \u0441\u0432\u043e\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u043a\u043d\u043e\u043f\u043e\u043a, \u043e\u0442 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043e\u0442 ActionColumn<\/p>\n<pre class=\"brush: php; title: \u0412\u043e\u0442 \u0447\u0442\u043e \u0443 \u043c\u0435\u043d\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c; notranslate\" title=\"\u0412\u043e\u0442 \u0447\u0442\u043e \u0443 \u043c\u0435\u043d\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c\">\r\nnamespace backend\\components;\r\n\r\nuse yii\\grid\\ActionColumn;\r\nuse Yii;\r\nuse yii\\helpers\\Html;\r\n\r\nclass ButtonUpdateDelete extends ActionColumn{\r\n\r\n    public $template = '{update} {delete}';\r\n    public $updateAction, $deleteAction;\r\n\r\n    protected function initDefaultButtons()\r\n    {\r\n        if (!isset($this-&gt;buttons['update'])) {\r\n            $this-&gt;buttons['update'] = function ($url, $model) {\r\n                return Html::a('&lt;span class=&quot;glyphicon glyphicon-pencil&quot;&gt;&lt;\/span&gt;', [$this-&gt;updateAction, 'id' =&gt; $model-&gt;id], [\r\n                    'title' =&gt; Yii::t('yii', 'Update'),\r\n                    'data-pjax' =&gt; '0',\r\n                ]);\r\n            };\r\n        }\r\n        if (!isset($this-&gt;buttons['delete'])) {\r\n            $this-&gt;buttons['delete'] = function ($url, $model) {\r\n                return Html::a('&lt;span class=&quot;glyphicon glyphicon-trash&quot;&gt;&lt;\/span&gt;', [$this-&gt;deleteAction, 'id' =&gt; $model-&gt;id], [\r\n                    'title' =&gt; Yii::t('yii', 'Delete'),\r\n                    'data-confirm' =&gt; Yii::t('yii', 'Are you sure you want to delete this item?'),\r\n                    'data-method' =&gt; 'post',\r\n                    'data-pjax' =&gt; '0',\r\n                ]);\r\n            };\r\n        }\r\n    }\r\n} \r\n<\/pre>\n<p>\u041f\u043e \u0441\u0443\u0442\u0438, \u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043b\u0430 \u043c\u0435\u0442\u043e\u0434 initDefaultButtons, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u0430 $updateAction \u0438 $deleteAction.<\/p>\n<pre class=\"brush: php; title: \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0448\u0438 \u043d\u043e\u0432\u044b\u0435 \u043a\u043d\u043e\u043f\u043a\u0438 \u0432 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438; notranslate\" title=\"\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0448\u0438 \u043d\u043e\u0432\u044b\u0435 \u043a\u043d\u043e\u043f\u043a\u0438 \u0432 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438\">\r\nGridView::widget([\r\n        'dataProvider' =&gt; $dataProvider,\r\n        'layout' =&gt; &quot;{pager}\\n{items}\\n{pager}&quot;,\r\n        'filterModel' =&gt; $searchModel,\r\n        'columns' =&gt; [\r\n            ['class' =&gt; 'yii\\grid\\SerialColumn'],\r\n            'title',\r\n            ... \r\n            ['class' =&gt; 'backend\\components\\ButtonUpdateDelete',\r\n                'updateAction' =&gt; 'update-page',\r\n                'deleteAction' =&gt; 'delete-page'\r\n            ],\r\n        ],\r\n    ]); \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u0412 \u0430\u0434\u043c\u0438\u043d\u043a\u0435 \u043b\u044e\u0431\u043e\u0433\u043e \u0441\u0430\u0439\u0442\u0430 \u0435\u0441\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c\/\u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c (CRUD &#8212; \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438). \u0412 Yii, \u0435\u0449\u0435 \u0441\u043e \u0432\u0440\u0435\u043c\u0435\u043d \u043f\u0435\u0440\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 \u0444\u0440\u0435\u0438\u043c\u0432\u043e\u0440\u043a\u0430, \u0435\u0441\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 CRUD. \u041e\u043d \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f. \u041c\u043d\u0435 \u043d\u0435 \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f \u0442\u043e, \u0447\u0442\u043e \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440, \u0432 \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 (AccessControl). \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0431\u043e\u043b\u0435\u0435 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[13],"_links":{"self":[{"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/posts\/126"}],"collection":[{"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/comments?post=126"}],"version-history":[{"count":3,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/posts\/126\/revisions"}],"predecessor-version":[{"id":129,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/posts\/126\/revisions\/129"}],"wp:attachment":[{"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/media?parent=126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/categories?post=126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/tags?post=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}