{"id":59,"date":"2014-09-03T14:16:26","date_gmt":"2014-09-03T09:16:26","guid":{"rendered":"http:\/\/developer.uz\/blog\/?p=59"},"modified":"2014-09-03T14:16:26","modified_gmt":"2014-09-03T09:16:26","slug":"base-model-and-base-controller-yii","status":"publish","type":"post","link":"https:\/\/developer.uz\/blog\/base-model-and-base-controller-yii\/","title":{"rendered":"\u0411\u0430\u0437\u043e\u0432\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0434\u043b\u044f Yii"},"content":{"rendered":"<p>\u041e\u0434\u043d\u0438\u043c \u0438\u0437 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432 Yii \u0444\u0440\u0435\u0438\u043c\u0432\u043e\u0440\u043a\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430\u043b\u0438\u0447\u0438\u0435 gii &#8212; \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u043c\u0435\u0435\u0442 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438, \u0444\u043e\u0440\u043c\u044b, \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b \u0438 \u0434\u0430\u0436\u0435 \u0441\u0435\u0440\u0438\u044e \u0444\u0430\u0439\u043b\u043e\u0432 Crud-\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0434\u043b\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043c\u043e\u0434\u0435\u043b\u0438. \u0422\u0430\u043a\u0430\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443, \u0442\u043a \u0432\u0441\u044f \u0440\u0443\u0442\u0438\u043d\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0443\u0436\u0435 \u0441\u0434\u0435\u043b\u0430\u043d\u0430 \u0437\u0430 \u0432\u0430\u0441, \u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u043a\u043e\u0434\u0435. \u0418\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u044f \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u0437\u0430 \u0441\u0447\u0435\u0442 \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043a\u0443\u0441\u043a\u043e\u0432 \u043a\u043e\u0434\u0430 \u0432 \u0431\u0430\u0437\u043e\u0432\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440.<br \/>\n\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c <strong>\u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440<\/strong> \u043e\u0442\u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043e\u0442 CController, \u0443 \u043c\u0435\u043d\u044f \u043e\u043d \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0441\u0442\u043e Controller. \u0424\u0430\u0438\u043b \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u043c \u0432 \u043f\u0430\u043f\u043a\u0443 protected\/components<\/p>\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\nclass Controller extends CController\r\n{\r\n   public function crudSearch($class)\r\n    {\r\n        $model=new $class('search');\r\n        $model-&gt;unsetAttributes();\r\n\r\n        if(isset($_GET[$class]))\r\n            $model-&gt;attributes=$_GET[$class];\r\n        return $model;\r\n    }\r\n\r\n    public function loadModel($id, $class)\r\n    {\r\n        $model=$class::model()-&gt;findByPk($id);\r\n        if($model===null)\r\n            throw new CHttpException(404,'The requested page does not exist.');\r\n        return $model;\r\n    }\r\n}\r\n<\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u0441\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e<\/p>\n<pre class=\"brush: php; title: \u0442\u0435 \u0432\u043e\u0442 \u044d\u0442\u043e; notranslate\" title=\"\u0442\u0435 \u0432\u043e\u0442 \u044d\u0442\u043e\">\r\nclass SiteController extends CController\r\n<\/pre>\n<pre class=\"brush: php; title: \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043d\u0430 \u0432\u043e\u0442 \u044d\u0442\u043e; notranslate\" title=\"\u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043d\u0430 \u0432\u043e\u0442 \u044d\u0442\u043e\">\r\nclass SiteController extends Controller\r\n<\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c <strong>\u0431\u0430\u0437\u043e\u0432\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c<\/strong>, \u043e\u0442\u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u043e\u0442 CActiveRecord. \u042f \u043d\u0430\u0437\u0432\u0430\u043b\u0430 \u0435\u0435 BaseModel \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u043b\u0430 \u0432 \u043f\u0430\u043f\u043a\u0443 protected\/components<\/p>\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\nclass BaseModel extends CActiveRecord\r\n{\r\n    public function saveAttributes($attributes)\r\n    {\r\n        $this-&gt;attributes = $attributes;\r\n        return $this-&gt;save();\r\n    }\r\n\r\n    public function saveFromPost($param)\r\n    {\r\n        if(isset($_POST[$param]))\r\n        {\r\n            if($this-&gt;saveAttributes($_POST[$param]))\r\n                return true;\r\n        }\r\n        return false;\r\n    }\r\n\r\n    public function getAttributeByPk($id, $atribute)\r\n    {\r\n        $result = $this-&gt;findByPk($id);\r\n        if(is_object($result)) return $result-&gt;$atribute;\r\n        else return '';\r\n    }\r\n}\r\n<\/pre>\n<p>\u0411\u0430\u0437\u043e\u0432\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u043e\u043a\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u0441\u0435\u0433\u043e 3 \u043d\u0435\u0437\u0430\u043c\u0435\u043d\u0438\u043c\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u0430: saveAttributes, saveFromPost \u0438 getAttributeByPk. \u0422\u0435\u043f\u0435\u0440\u044c \u0432\u0441\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u043e\u043b\u0436\u043d\u044b \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438<\/p>\n<pre class=\"brush: php; title: \u0442\u0435 \u0432\u043e\u0442 \u044d\u0442\u043e; notranslate\" title=\"\u0442\u0435 \u0432\u043e\u0442 \u044d\u0442\u043e\">\r\nclass Country extends CActiveRecord\r\n<\/pre>\n<pre class=\"brush: php; title: \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043d\u0430 \u0432\u043e\u0442 \u044d\u0442\u043e; notranslate\" title=\"\u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043d\u0430 \u0432\u043e\u0442 \u044d\u0442\u043e\">\r\nclass Country extends BaseModel\r\n<\/pre>\n<p>\u0421\u0440\u0430\u0432\u043d\u0438\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \ud83d\ude42<br \/>\n\u0412\u043e\u0442 \u043a\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440, \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e gii<\/p>\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\n public function actionAdmin()\r\n    {\r\n        $model=new Country('search');\r\n        $model-&gt;unsetAttributes();  \r\n        if(isset($_GET['Country']))\r\n            $model-&gt;attributes=$_GET['Country'];\r\n\r\n        $this-&gt;render('admin',array(\r\n            'model'=&gt;$model,\r\n        ));\r\n    }\r\n\r\npublic function actionCreate()\r\n    {\r\n        $model=new Country;\r\n\r\n        if(isset($_POST['Country']))\r\n        {\r\n            $model-&gt;attributes=$_POST['Country'];\r\n            if($model-&gt;save())\r\n                $this-&gt;redirect(array('view','id'=&gt;$model-&gt;id));\r\n        }\r\n\r\n        $this-&gt;render('create',array(\r\n            'model'=&gt;$model,\r\n        ));\r\n    }\r\n\r\n    public function actionUpdate($id)\r\n    {\r\n        $model=$this-&gt;loadModel($id);\r\n\r\n        if(isset($_POST['Country']))\r\n        {\r\n            $model-&gt;attributes=$_POST['Country'];\r\n            if($model-&gt;save())\r\n                $this-&gt;redirect(array('view','id'=&gt;$model-&gt;id));\r\n        }\r\n\r\n        $this-&gt;render('update',array(\r\n            'model'=&gt;$model,\r\n        ));\r\n    }\r\n<\/pre>\n<p>\u0410 \u0432\u043e\u0442 \u0442\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u043e\u0442 \u0436\u0435 \u043a\u043e\u0434 \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043a\u0443\u0441\u043a\u043e\u0432 \u0432 \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440:<\/p>\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\n public function actionAdmin()\r\n    {\r\n        $model = $this-&gt;crudSearch('Country');\r\n        $this-&gt;render('country',array(\r\n            'model'=&gt;$model,\r\n        ));\r\n    }\r\n\r\npublic function actionCreate()\r\n    {\r\n        $model= new Country();\r\n\r\n        if($model-&gt;saveFromPost('Country'))\r\n            $this-&gt;redirect('\/admin\/country');\r\n\r\n        $this-&gt;render('createCountry',array(\r\n            'model'=&gt;$model,\r\n        ));\r\n    }\r\n\r\n    public function actionUpdate($id)\r\n    {\r\n        $model=$this-&gt;loadModel($id);\r\n\r\n       if($model-&gt;saveFromPost('Country'))\r\n            $this-&gt;redirect('\/admin\/country');\r\n\r\n        $this-&gt;render('update',array(\r\n            'model'=&gt;$model,\r\n        ));\r\n    }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u041e\u0434\u043d\u0438\u043c \u0438\u0437 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432 Yii \u0444\u0440\u0435\u0438\u043c\u0432\u043e\u0440\u043a\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430\u043b\u0438\u0447\u0438\u0435 gii &#8212; \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u043c\u0435\u0435\u0442 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438, \u0444\u043e\u0440\u043c\u044b, \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b \u0438 \u0434\u0430\u0436\u0435 \u0441\u0435\u0440\u0438\u044e \u0444\u0430\u0439\u043b\u043e\u0432 Crud-\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0434\u043b\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043c\u043e\u0434\u0435\u043b\u0438. \u0422\u0430\u043a\u0430\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443, \u0442\u043a \u0432\u0441\u044f \u0440\u0443\u0442\u0438\u043d\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0443\u0436\u0435 \u0441\u0434\u0435\u043b\u0430\u043d\u0430 \u0437\u0430 \u0432\u0430\u0441, \u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u043a\u043e\u0434\u0435. \u0418\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u044f \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u0437\u0430 \u0441\u0447\u0435\u0442 \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043a\u0443\u0441\u043a\u043e\u0432 \u043a\u043e\u0434\u0430 \u0432 [&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":[12],"_links":{"self":[{"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/posts\/59"}],"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=59"}],"version-history":[{"count":2,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/posts\/59\/revisions"}],"predecessor-version":[{"id":96,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/posts\/59\/revisions\/96"}],"wp:attachment":[{"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/media?parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/categories?post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/tags?post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}