{"id":256064,"date":"2015-04-27T09:14:02","date_gmt":"2015-04-27T05:14:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=256064"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=256064","title":{"rendered":"\u00ab\u0414\u043e\u043c\u0430\u0448\u043a\u0430\u00bb \u043f\u043e Yii2"},"content":{"rendered":"<p>     \t\u041d\u0435 \u0442\u0430\u043a \u0434\u0430\u0432\u043d\u043e \u0431\u044b\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u043d \u0445\u0430\u0431 \u043e \u00ab\u043f\u043e\u0434\u0432\u043e\u0434\u043d\u044b\u0445 \u043a\u0430\u043c\u043d\u044f\u0445 <s>\u0438 \u0440\u0430\u043a\u0443\u0448\u043a\u0430\u0445<\/s>\u00bb <a href=\"http:\/\/habrahabr.ru\/post\/254179\/\">habrahabr.ru\/post\/254179<\/a> \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u044b\u043b\u043e \u00ab\u0434\u043e\u043c\u0430\u0448\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u043d\u0438\u0435\u00bb \u2014 \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0442\u0430\u043a \u043d\u0438\u043a\u0442\u043e \u0438 \u043d\u0435 \u043f\u0440\u0438\u0441\u043b\u0430\u043b, \u043d\u043e \u0434\u0443\u043c\u0430\u044e \u043c\u043d\u043e\u0433\u0438\u0435 \u0437\u0430\u0434\u0430\u0432\u0430\u043b\u0438\u0441\u044c \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u043c \u2014 \u043a\u0430\u043a \u0432\u0441\u0435 \u0442\u0430\u043a\u0438 \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438 \u0438\u0437 \u0440\u0430\u0437\u043d\u044b\u0445 \u043c\u043e\u0434\u0443\u043b\u0435\u0439. \u0425\u043e\u0447\u0443 \u0432\u0430\u043c \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u00ab\u041d\u0430\u0448\u0430 \u0431\u0430\u043d\u0434\u0430\u00bb, \u0432 \u0441\u043e\u0441\u0442\u0430\u0432\u0435 3 \u0447\u0435\u043b\u043e\u0432\u0435\u043a, \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u0440\u0435\u0448\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u043a\u0438 \u043d\u0430 \u0434\u0430\u043d\u043d\u043e\u043c \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0435, \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0440\u0430\u0437, \u0432 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u2014 \u0441\u0435\u043b\u0438 \u043c\u044b \u0437\u043d\u0430\u0447\u0438\u0442 \u0437\u0430 <s>\u043a\u043e\u0444\u0435 \u0441 \u043f\u0435\u0447\u0435\u043d\u044c\u043a\u0430\u043c\u0438<\/s> \u0438 \u0441\u0442\u0430\u043b\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u00ab\u0441\u043c\u044b\u0441\u043b\u043e\u0432\u0443\u044e \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443\u00bb \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u043a\u0438 \u2014 <\/p>\n<blockquote><p>\u041e\u0434\u043d\u0430 \u0433\u043e\u043b\u043e\u0432\u0430 \u2014 \u0445\u043e\u0440\u043e\u0448\u043e, \u043d\u043e \u0434\u0432\u0435 \u043b\u0443\u0447\u0448\u0435<\/p><\/blockquote>\n<p>  \u0412 \u0438\u0442\u043e\u0433\u0435 \u0443 \u043d\u0430\u0441 \u043d\u0430\u0440\u0438\u0441\u043e\u0432\u0430\u043b\u0430\u0441\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u00ab\u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044f \u043f\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0445 \u043c\u043e\u0434\u0443\u043b\u0435\u0439\u00bb:<br \/>  <b>1. \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u043b\u0435\u0439\u0448\u0435\u043d\u044b \u0434\u043b\u044f \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0434\u043e\u043b\u0436\u043d\u044b \u043c\u043e\u0434\u0443\u043b\u0438;<br \/>  2. \u0420\u0435\u043b\u0435\u0439\u0448\u0435\u043d\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f \u0432 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0435 \u043c\u043e\u0434\u0435\u043b\u0438;<br \/>  3. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0440\u0435\u043b\u0435\u0439\u0448\u0435\u043d\u043e\u0432 \u0432 \u043e\u0431\u0435 \u0441\u0442\u043e\u0440\u043e\u043d\u044b (\u043a\u0430\u043a \u043d\u0430 \u043c\u043e\u0434\u0443\u043b\u044c\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c, \u0442\u0430\u043a \u0438 \u0432 \u043c\u043e\u0434\u0443\u043b\u044c\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u2014 \u043e\u0431\u0440\u0430\u0442\u043d\u0430\u044f);<\/b><\/p>\n<p>  <img decoding=\"async\" src=\"\/\/habrastorage.org\/files\/11f\/8e7\/e74\/11f8e7e741954c6882d595ab1484b768.jpg\"\/><\/p>\n<h2>\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0441\u0432\u044f\u0437\u0435\u0439<\/h2>\n<p>  <\/p>\n<pre><code class=\"php\">protected static $relations = [];  public static function addRelation($name, $targetClass, $link, $multiple = false) {     self::$relations[$name] = [$targetClass, $link, $multiple]; } <\/code><\/pre>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">&#8230;<\/b><\/p>\n<div class=\"spoiler_text\">\u041a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u043a\u043e\u0434 \u043d\u0435 \u0438\u0434\u0435\u0430\u043b\u0435\u043d (\u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 php-doc), \u043d\u0443 \u0438 \u043c\u043d\u043e\u0433\u0438\u0435 \u0437\u0430\u0434\u0430\u043b\u0438\u0441\u044c \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u043c \u2014 \u00ab\u0430 \u043a\u0430\u043a \u0431\u044b\u0442\u044c \u0441 \u0434\u043e\u043f \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c\u0438 (andWhere, orderBy, indexBy, &#8230;)\u00bb? \u041d\u043e \u044f, \u043a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e \u043d\u0435 \u0431\u0443\u0434\u0443 \u0437\u0430\u0442\u0440\u0430\u0433\u0438\u0432\u0430\u0442\u044c \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044d\u0442\u043e\u0442 \u0432\u043e\u043f\u0440\u043e\u0441, \u0442.\u043a. \u044d\u0442\u043e \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043d\u0430\u0431\u0440\u043e\u0441\u043a\u0438 \u043c\u044b\u0441\u043b\u0435\u0439. <br \/>  <i>\u0422\u0440\u0435\u0439\u0442 \u0440\u0435\u0448\u0438\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0441\u043e \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e\u043c \u2014 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u043c \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0430 \u043c\u043e\u0434\u0435\u043b\u0438.<\/i>  <\/div>\n<\/div>\n<p>  <\/p>\n<h2>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0432\u044f\u0437\u0435\u0439<\/h2>\n<p>  <\/p>\n<pre><code class=\"php\">public function getRelation($name, $throwException = true) {     if (isset(self::$relations[$name])) {         return $this-&gt;createRelation($name, $throwException);     }     return parent::getRelation($name, $throwException); }  public function __get($name) {     if (isset(self::$relations[$name]) && !$this-&gt;isRelationPopulated($name)) {         $related = $this-&gt;getRelation($name)-&gt;findFor($name, $this);         $this-&gt;populateRelation($name, $related);         return $related;     }     return parent::__get($name); }  protected function createRelation($name, $throwException = true) {     $relation = self::$relations;     if (!isset($relation[$name])) {         if (!$throwException) {             return null;         }         throw new InvalidCallException('Relation ' . $name . ' not find in class ' . get_class($this));     }     $relation = $relation[$name];     $class = array_shift($relation);     $query = $class::find();     $query-&gt;link = array_shift($relation);     $query-&gt;multiple = array_shift($relation);     $query-&gt;primaryModel = $this;     return $query; } <\/code><\/pre>\n<p>  \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 [[yii\\db\\BaseActiveRecord::hasOne|hasMany]]<\/p>\n<h2>\u041e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0432\u044f\u0437\u0435\u0439 \u0438 \u0438\u0445 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f<\/h2>\n<p>  <i>\u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u043a\u043e\u0434\u0435, \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0437 \u00ab\u0434\u043e\u043c\u0430\u0448\u043a\u0438\u00bb \u2014 \u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u043d\u043a\u043e\u0432\u0441\u043a\u0438\u0445 \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u043e\u0432 (\u043d\u0435\u0439\u043c\u0441\u043f\u0435\u0439\u0441\u044b \u043e\u043f\u0443\u0441\u0442\u0438\u043c \u0434\u043b\u044f \u043e\u0431\u043b\u0435\u0433\u0447\u0435\u043d\u0438\u044f \u0432\u043e\u0441\u043f\u0440\u0438\u044f\u0442\u0438\u044f \u043a\u043e\u043d\u0442\u0435\u043d\u0442\u0430).<\/i>  <\/p>\n<pre><code class=\"php\">public $relations = [     'User' =&gt; [         'bankAccounts' =&gt; ['BankAccount', ['user_id' =&gt; 'id'], true]     ],     'BankAccount' =&gt; [         'user' =&gt; ['User', ['id' =&gt; 'user_id'], false]     ], ];  protected static function registerRelations() {     foreach ($this-&gt;relations as $owner =&gt; $relations) {         foreach ($relations as $name =&gt; $relation) {             list ($targetClass, $link, $multiple) = $relation;             $owner::addRelation($name, $targetClass, $link, $multiple);         }     } } <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u043c\u043e\u0433 \u0431\u044b\u0442\u044c \u00ab\u0432\u044b\u0432\u043e\u0434\u00bb \u0438\u043b\u0438 \u00ab\u0438\u0442\u043e\u0433\u00bb, \u043d\u043e \u0434\u0430\u043d\u043d\u044b\u0439 \u0445\u0430\u0431 \u0434\u0443\u043c\u0430\u044e \u0432 \u044d\u0442\u043e\u043c \u043d\u0435 \u043d\u0443\u0436\u0434\u0430\u0435\u0442\u0441\u044f, \u043f\u0440\u043e\u0448\u0443 \u043f\u0440\u043e\u0449\u0435\u043d\u0438\u0435 \u0437\u0430 \u043c\u0430\u043b\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u2014 \u0434\u0443\u043c\u0430\u044e \u0442\u0443\u0442 \u0432\u0441\u0435 \u043f\u0440\u043e\u0437\u0440\u0430\u0447\u043d\u043e. <br \/>  \u0416\u0434\u0443 \u043a\u0440\u0438\u0442\u0438\u043a\u0438, \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432, \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0439.<br \/>  \u0412 \u0441\u043a\u043e\u0440\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u043e \u0438 \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043d\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0432 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u043e\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u0435, \u043e\u0431 \u044d\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u043e \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0445\u0430\u0431\u0435 \u0438 \u043d\u0430 <a href=\"http:\/\/yiiframework.ru\/forum\/viewforum.php?f=9\">\u0440\u0443\u0441\u0441\u043a\u043e\u044f\u0437\u044b\u0447\u043d\u043e\u043c yii \u0444\u043e\u0440\u0443\u043c\u0435<\/a><\/p>\n<p>  \u043f.\u0441. \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0438 \u0432 \u0448\u043a\u043e\u043b\u0443 \u043d\u0435 \u043f\u043e\u0439\u0434\u0443\u0442 \u2014 \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c     \t<\/p>\n<div class=\"clear\"><\/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\/256685\/\"> http:\/\/habrahabr.ru\/post\/256685\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>     \t\u041d\u0435 \u0442\u0430\u043a \u0434\u0430\u0432\u043d\u043e \u0431\u044b\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u043d \u0445\u0430\u0431 \u043e \u00ab\u043f\u043e\u0434\u0432\u043e\u0434\u043d\u044b\u0445 \u043a\u0430\u043c\u043d\u044f\u0445 <s>\u0438 \u0440\u0430\u043a\u0443\u0448\u043a\u0430\u0445<\/s>\u00bb <a href=\"http:\/\/habrahabr.ru\/post\/254179\/\">habrahabr.ru\/post\/254179<\/a> \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u044b\u043b\u043e \u00ab\u0434\u043e\u043c\u0430\u0448\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u043d\u0438\u0435\u00bb \u2014 \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0442\u0430\u043a \u043d\u0438\u043a\u0442\u043e \u0438 \u043d\u0435 \u043f\u0440\u0438\u0441\u043b\u0430\u043b, \u043d\u043e \u0434\u0443\u043c\u0430\u044e \u043c\u043d\u043e\u0433\u0438\u0435 \u0437\u0430\u0434\u0430\u0432\u0430\u043b\u0438\u0441\u044c \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u043c \u2014 \u043a\u0430\u043a \u0432\u0441\u0435 \u0442\u0430\u043a\u0438 \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438 \u0438\u0437 \u0440\u0430\u0437\u043d\u044b\u0445 \u043c\u043e\u0434\u0443\u043b\u0435\u0439. \u0425\u043e\u0447\u0443 \u0432\u0430\u043c \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.  <\/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-256064","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/256064","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=256064"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/256064\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=256064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=256064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=256064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}