{"id":226103,"date":"2014-06-13T00:26:02","date_gmt":"2014-06-12T20:26:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=226103"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=226103","title":{"rendered":"<span class=\"post_title\">Yii2. \u0421\u0432\u044f\u0437\u0438 Active Record<\/span>"},"content":{"rendered":"<div class=\"content html_format\">     \t<img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/725\/4e2\/0af\/7254e20afe3ec7fab7be38b495916b05.png\" align=\"right\"\/> Yii2 \u0435\u0449\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0431\u0435\u0442\u0430-\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438 \u043d\u043e \u0432\u0438\u0434\u0438\u043c\u043e \u044d\u0442\u043e \u043d\u0438\u043a\u043e\u0433\u043e \u043d\u0435 \u043f\u0443\u0433\u0430\u0435\u0442. \u041c\u043d\u043e\u0433\u0438\u0435 \u043d\u0430\u0447\u0430\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0434\u0430\u0436\u0435 \u0432 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0435 \u0438 \u043f\u043e\u0434 \u0434\u0443\u043b\u043e\u043c \u043f\u0438\u0441\u0442\u043e\u043b\u0435\u0442\u0430, \u043e\u0442\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0434\u0430\u0436\u0435 \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u043a\u043e\u0434 \u043f\u0435\u0440\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438. \u041a\u0442\u043e-\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u0437\u0443\u0447\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>  \u041d\u0430 <a href=\"http:\/\/yiiframework.ru\/forum\/\">\u0444\u043e\u0440\u0443\u043c\u0435<\/a> \u0440\u0443\u0441\u0441\u043a\u043e\u0433\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u0430, \u0432\u0441\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043e\u0431\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u044f.<\/p>\n<p>  \u041e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0447\u0442\u043e \u0443 \u043c\u043d\u043e\u0433\u0438\u0445 \u0432\u043e\u0437\u043d\u0438\u043a\u043b\u0438 \u0442\u0440\u0443\u0434\u043d\u043e\u0441\u0442\u0438 \u0441 \u0440\u0430\u0431\u043e\u0442\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u044c\u044e ActiveRecord \u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438.<\/p>\n<p>  \u042f \u0440\u0435\u0448\u0438\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u0440\u0435\u0446\u0435\u043f\u0442, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0432\u044b \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0435 \u0435\u0433\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u043c, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0439\u043c\u0435\u0442\u0435 \u043a\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u043d\u0430\u0434\u043e. \u0412 \u043b\u044e\u0431\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430\u0434\u0435\u044e\u0441\u044c \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u0435\u043d.<br \/>  <a name=\"habracut\"><\/a><\/p>\n<h4>\u0427\u0442\u043e \u043d\u0430\u043c \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043e \u0441\u0432\u044f\u0437\u044f\u043c\u0438?<\/h4>\n<p>  \u0421\u0432\u044f\u0437\u0438 \u0432 \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0433\u0435\u0442\u0442\u0435\u0440\u043e\u0432<\/p>\n<pre><code class=\"php\">public function getCategory() {     return $this-&gt;hasOne(Category::className(), ['id' =&gt; 'category_id']); } <\/code><\/pre>\n<p>  \u0413\u0435\u0442\u0442\u0435\u0440 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 ActiveQuery \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u0435\u0440\u0435\u0434 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u043e\u0439 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438.  <\/p>\n<pre><code class=\"php\">$posts = Category::find($id)-&gt;getPosts()-&gt;limit(5)-&gt;order('created_at')-&gt;all(); <\/code><\/pre>\n<p>  <\/p>\n<blockquote><p>\u0417\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u0435:<br \/>  \u0412\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u043c\u0430\u0433\u0438\u044e <b>$post-&gt;category<\/b>, \u0432\u043c\u0435\u0441\u0442\u043e \u0433\u0435\u0442\u0442\u0435\u0440\u0430, \u043f\u043e\u043c\u043d\u0438\u0442\u0435 \u0447\u0442\u043e \u0442\u0430\u043a \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 Query-\u043e\u0431\u044a\u0435\u043a\u0442\u0430.<br \/>  \u0414\u0440\u0443\u0433\u0438\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438 <b>$post-&gt;category === $post-&gt;getCategory()-&gt;one()<\/b>  <\/p><\/blockquote>\n<p>  <\/p>\n<h5>\u041c\u0435\u0442\u043e\u0434\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043e \u0441\u0432\u044f\u0437\u044f\u043c\u0438<\/h5>\n<p>  <b>populateRelation($relationName, $relatedModelOrArray)<\/b> \u2014 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c \u0432 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0443\u044e.<\/p>\n<blockquote><p>\u0417\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u0435:<br \/>  \u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043d\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0430 \u043b\u0438 \u0441\u0432\u044f\u0437\u044c \u043c\u0435\u0436\u0434\u0443 \u044d\u0442\u0438\u043c\u0438 \u043c\u043e\u0434\u0435\u043b\u044f\u043c\u0438 (\u0433\u0435\u0442\u0442\u0435\u0440), \u0430 \u0442\u0430\u043a \u0436\u0435 \u043d\u0435 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u043d\u0443\u0436\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b.<\/p><\/blockquote>\n<p>  <\/p>\n<pre><code class=\"php\">$post = new Post(); $post-&gt;populateRelation('category', new Category()); $post-&gt;populateRelation('tags', [new Tag(), new Tag()]); <\/code><\/pre>\n<p>  <b>link($relationName, relatedModel, $extraColumns = [])<\/b> \u2014 \u0432 \u043e\u0442\u043b\u0438\u0447\u0438\u0438 \u043e\u0442 <u>populateRelation<\/u> \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043a\u0440\u043e\u043c\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438, \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u0438, \u0440\u0430\u0441\u0441\u0442\u0430\u0432\u043b\u044f\u044f \u043d\u0443\u0436\u043d\u044b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0438 \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0422\u041e\u041b\u042c\u041a\u041e \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c. $extraColumns \u2014 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 pivot table, \u0435\u0441\u043b\u0438 \u0441\u0432\u044f\u0437\u044c \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u043d\u0435\u0435.<\/p>\n<pre><code class=\"php\">$post = new Post(); $post-&gt;link('category', new Category()); $post-&gt;link('tags', new Tag()); $post-&gt;link('tags', new Tag()); <\/code><\/pre>\n<p>  \u0412\u0430\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441\u043e \u0441\u0432\u044f\u0437\u044f\u043c\u0438 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432 Yii2 \u0435\u0441\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0435 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430.<\/p>\n<pre><code class=\"php\">public function transactions() {   return [     \/\/ scenario name =&gt; operation (insert, update or delete)      self::SCENARIO_DEFAULT =&gt; self::OP_INSERT | self::OP_UPDATE,      self::SCENARIO_UPDATE =&gt; self::OP_INSERT,   ]; } <\/code><\/pre>\n<p>  \u042d\u0442\u043e \u043b\u0438\u0448\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0412\u044b \u043d\u0430\u0439\u0434\u0435\u0442\u0435 \u0432 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438. <\/p>\n<h4>\u041f\u0440\u0438\u043c\u0435\u0440<\/h4>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u044f \u0445\u043e\u0447\u0443 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043a\u0430\u043a \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041c\u043e\u0434\u0435\u043b\u044c<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"php\">class Post extends ActiveRecord {     \/\/ \u0411\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u043f\u0440\u0438 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u0445     public function transactions()     {         return [             self::SCENARIO_INSERT =&gt; self::OP_INSERT,             self::SCENARIO_UPDATE =&gt; self::OP_UPDATE,         ];     }      public function getTags()     {         return $this-&gt;hasMany(Tag::className(), ['id' =&gt; 'tag_id'])             -&gt;viaTable('post_tag', ['post_id' =&gt; 'id']);     }      \/\/ \u042f \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0435\u0442\u0442\u0435\u0440\u044b \u0434\u043b\u044f \u0441\u0432\u044f\u0437\u0435\u0439,     \/\/ \u0445\u043e\u0442\u044f \u044d\u0442\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0442\u0435\u043b\u043e\u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0435,     \/\/ \u043d\u043e \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u0441\u043b\u043e\u0436\u043d\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0440\u0430\u0437\u0443 \u0440\u044f\u0434\u043e\u043c \u0441 \u0433\u0435\u0442\u0442\u0435\u0440\u043e\u043c.     \/\/ \u0417\u0430\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u043e, \u0442.\u043a. \u0441\u0440\u0430\u0437\u0443 \u043c\u043e\u0436\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435      \/\/ \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438     public function setTags($tags)     {         $this-&gt;populateRelation('tags', $tags);         $this-&gt;tags_count = count($tags);     }      \/\/ \u0421\u0435\u0442\u0442\u0435\u0440 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0442\u0435\u0433\u043e\u0432 \u0438\u0437 \u0441\u0442\u0440\u043e\u043a\u0438, \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u044f\u0442\u043e\u0439     public function setTagsString($value)     {         $tags = [];                foreach (explode(',' $value) as $name) {              $tag = new Tag();              $tag-&gt;name = $name;              $tag[] = $tag;         }                 $this-&gt;setTags($tags);     }      public function getCover()     {         return $this-&gt;hasOne(Image::className(), ['id' =&gt; 'cover_id']);     }      public function setCover($cover)     {         $this-&gt;populateRelation('cover', $cover);     }      public function getImages()     {         return $this-&gt;hasMany(Image::className(), ['post_id' =&gt; 'id']);     }      public function setImages($images)     {         $this-&gt;populateRelation('images', $images);          if (!$this-&gt;isRelationPopulated('cover') && !$this-&gt;getCover()-&gt;one()) {             $this-&gt;setCover(reset($images));         }     }      public function loadUploadedImage()     {            $images = [];             foreach (UploadedFile::getInstances(new Image(), 'image') as $file) {                 $image = new Image();                 $image-&gt;name = $file-&gt;name;                 $images[] = $image;            }             $this-&gt;setImages($images);     }      public function beforeSave($insert)     {         if (!parent::beforeSave($insert)) {             return false;         }         \/\/ \u0412 beforeSave \u043c\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438        \/\/ \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0434\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439, \u0442.\u0435. \u043d\u0443\u0436\u043d\u044b \u0438\u0445 \u0418\u0414        \/\/ \u041d\u0435 \u0432\u043e\u043b\u043d\u0443\u0439\u0442\u0435\u0441\u044c \u043e \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0442.\u043a. \u043c\u044b \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u043b\u0438,        \/\/ \u043e\u043d\u0430 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0447\u0430\u0442\u0430 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 `insert()` \u0438 `update()`         \/\/ \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0432\u0441\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438, \u0442\u0435 \u0447\u0442\u043e \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u044b \u0438\u043b\u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u044b        $relatedRecords = $this-&gt;getRelatedRecords();         if (isset($relatedRecords['cover'])) {            $this-&gt;link('cover', $relatedRecords['cover']);        }               return true;     }      public function afterSave($insert)     {         \/\/ \u0412 afterSave \u043c\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438        \/\/ \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u043f\u043e\u0441\u043b\u0435 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438, \u0442.\u043a. \u043d\u0443\u0436\u0435\u043d \u0435\u0435 \u0418\u0414         \/\/ \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0432\u0441\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438, \u0442\u0435 \u0447\u0442\u043e \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u044b \u0438\u043b\u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u044b        $relatedRecords = $this-&gt;getRelatedRecords();         if (isset($relatedRecords['tags'])) {            foreach ($relatedRecords['tags'] as $tag) {                $this-&gt;link('tags', $tag);            }        }                   if (isset($relatedRecords['images'])) {            foreach ($relatedRecords['images'] as $image) {                $this-&gt;link('images', $image);            }        }     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041a\u043e\u043d\u0442\u0440\u043e\u043b\u0435\u0440<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"php\">class PostController extends Controller {     public function actionCreate()     {         $post = new Post();         \/\/ \u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u043d\u0443\u0436\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439,         \/\/ \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0447\u0442\u043e\u0431 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u043f\u0440\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0438         $post-&gt;setScenario(Post::SCENARIO_INSERT);          if ($post-&gt;load(Yii::$app-&gt;request-&gt;post())) {             \/\/ \u0421\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u044b             $this-&gt;loadUploadedImages();              if ($post-&gt;save()) {                 return $this-&gt;redirect(['view', 'id' =&gt; $post-&gt;id]);             }         }                    return $this-&gt;render('create', [             'post' =&gt; $post,         ]);      } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<h4>\u0412\u043c\u0435\u0441\u0442\u043e \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f<\/h4>\n<p>  \u0415\u0441\u043b\u0438 \u0432\u044b \u0437\u043d\u0430\u0435\u0442\u0435 \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 <b>Yii Framework<\/b>, \u0436\u0438\u0432\u0435\u0442\u0435 \u0432 <b>\u041a\u0438\u0448\u0438\u043d\u0435\u0432\u0435 (\u041c\u043e\u043b\u0434\u043e\u0432\u0430)<\/b> \u0438\u043b\u0438 \u043f\u043e\u0431\u043b\u0438\u0437\u043e\u0441\u0442\u0438, \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u044f\u0439\u0442\u0435\u0441\u044c \u043a \u043d\u0430\u043c! \u041c\u044b \u0445\u043e\u0442\u0438\u043c \u0441\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u043e\u0444\u0444\u043b\u0430\u0439\u043d\u0435.<br \/>  <a href=\"http:\/\/yiiframework.ru\/forum\/viewtopic.php?f=13&amp;t=18405\">\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u0437\u0434\u0435\u0441\u044c!<\/a><\/p>\n<p>  <strong>\u0416\u0434\u0435\u043c \u0432\u0441\u0435\u0445!<\/strong>      \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/226103\/\"> http:\/\/habrahabr.ru\/post\/226103\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">     \t<img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/725\/4e2\/0af\/7254e20afe3ec7fab7be38b495916b05.png\" align=\"right\"\/> Yii2 \u0435\u0449\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0431\u0435\u0442\u0430-\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438 \u043d\u043e \u0432\u0438\u0434\u0438\u043c\u043e \u044d\u0442\u043e \u043d\u0438\u043a\u043e\u0433\u043e \u043d\u0435 \u043f\u0443\u0433\u0430\u0435\u0442. \u041c\u043d\u043e\u0433\u0438\u0435 \u043d\u0430\u0447\u0430\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0434\u0430\u0436\u0435 \u0432 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0435 \u0438 \u043f\u043e\u0434 \u0434\u0443\u043b\u043e\u043c \u043f\u0438\u0441\u0442\u043e\u043b\u0435\u0442\u0430, \u043e\u0442\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0434\u0430\u0436\u0435 \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u043a\u043e\u0434 \u043f\u0435\u0440\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438. \u041a\u0442\u043e-\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u0437\u0443\u0447\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>  \u041d\u0430 <a href=\"http:\/\/yiiframework.ru\/forum\/\">\u0444\u043e\u0440\u0443\u043c\u0435<\/a> \u0440\u0443\u0441\u0441\u043a\u043e\u0433\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u0430, \u0432\u0441\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043e\u0431\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u044f.<\/p>\n<p>  \u041e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0447\u0442\u043e \u0443 \u043c\u043d\u043e\u0433\u0438\u0445 \u0432\u043e\u0437\u043d\u0438\u043a\u043b\u0438 \u0442\u0440\u0443\u0434\u043d\u043e\u0441\u0442\u0438 \u0441 \u0440\u0430\u0431\u043e\u0442\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u044c\u044e ActiveRecord \u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438.<\/p>\n<p>  \u042f \u0440\u0435\u0448\u0438\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u0440\u0435\u0446\u0435\u043f\u0442, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0432\u044b \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0435 \u0435\u0433\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u043c, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0439\u043c\u0435\u0442\u0435 \u043a\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u043d\u0430\u0434\u043e. \u0412 \u043b\u044e\u0431\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430\u0434\u0435\u044e\u0441\u044c \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u0435\u043d.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-226103","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/226103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=226103"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/226103\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=226103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=226103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=226103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}