{"id":111,"date":"2014-09-17T13:57:54","date_gmt":"2014-09-17T08:57:54","guid":{"rendered":"http:\/\/developer.uz\/blog\/?p=111"},"modified":"2014-09-17T13:57:54","modified_gmt":"2014-09-17T08:57:54","slug":"activerecord-yii2-and-yii1","status":"publish","type":"post","link":"https:\/\/developer.uz\/blog\/activerecord-yii2-and-yii1\/","title":{"rendered":"\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 ActiveRecord \u0432 Yii2 \u0438 Yii1"},"content":{"rendered":"<p>\u0411\u043e\u043b\u044c\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 Yii-framework \u043c\u043d\u0435 \u043f\u043e\u043d\u0440\u0430\u0432\u0438\u043b\u043e\u0441\u044c \u0442\u043e, \u043a\u0430\u043a \u043f\u0435\u0440\u0435\u0434\u0435\u043b\u0430\u043b\u0438 ActiveRecord. \u0420\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 ActiveRecord \u0432 Yii2 \u0441\u0442\u0430\u043b\u043e \u043d\u0430\u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0443\u0434\u043e\u0431\u043d\u043e \u0438 \u043f\u0440\u0438\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043f\u0435\u0440\u0432\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e \u0444\u0440\u0435\u0438\u043c\u0432\u043e\u0440\u043a\u0430 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u0445\u043e\u0447\u0435\u0442\u0441\u044f. <\/p>\n<h2>\u041c\u0435\u0442\u043e\u0434\u044b ActiveRecord \u0432 Yii1<\/h2>\n<p>\u0412 Yii1 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043b\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0432\u044b\u0431\u043e\u0440\u043a\u0438 \u0441\u0442\u0440\u043e\u043a \u0438\u0437 \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d\u0430 \u043c\u043e\u0434\u0435\u043b\u044c:<\/p>\n<ul>\n<li>find($condition, $params) &#8212; \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438, \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0449\u0435\u0439 \u0443\u0441\u043b\u043e\u0432\u0438\u044e $condition<\/li>\n<li>findAll($condition,$params) &#8212; \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u0441\u0442\u0440\u043e\u043a, \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0449\u0438\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044e $condition<\/li>\n<li>findByPk($pk,$condition,$params) &#8212; \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u043f\u043e \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u043e\u043c\u0443 \u043a\u043b\u044e\u0447\u0443<\/li>\n<li>findAllByPk ($pk,$condition,$params) &#8212; \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0441\u0442\u0440\u043e\u043a \u043f\u043e \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u043e\u043c\u0443 \u043a\u043b\u044e\u0447\u0443 (\u043a\u0430\u043a \u0438 \u0432 \u043c\u0435\u0442\u043e\u0434 findByPk \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 $pk \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0432 \u0432\u0438\u0434\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u0445 \u043a\u043b\u044e\u0447\u0435\u0439)<\/li>\n<li>findByAttributes($attributes,$condition,$params) &#8212; \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c $attributes<\/li>\n<li>findAllByAttributes($attributes,$condition,$params) &#8212; \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0441\u0442\u0440\u043e\u043a \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c $attributes<\/li>\n<li>findBySql ($sql,$params) &#8212; \u0432\u044b\u0431\u043e\u0440 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u043f\u043e sql-\u0437\u0430\u043f\u0440\u043e\u0441\u0443<\/li>\n<li>findAllBySql ($sql,$params) &#8212; \u0432\u044b\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0441\u0442\u0440\u043e\u043a \u043f\u043e sql-\u0437\u0430\u043f\u0440\u043e\u0441\u0443<\/li>\n<li>\u0418 \u043a\u0443\u0447\u0430 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0434\u043b\u044f \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0441\u0442\u0440\u043e\u043a:<br \/>\ncount($condition,$params)<br \/>\ncountByAttributes($attributes,$condition,$params)<br \/>\ncountBySql\n <\/li>\n<\/ul>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430 \u0436\u0435 \u043a\u0430\u0440\u0442\u0438\u043d\u0430 \u0441 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 Update \u0438 Delete<\/p>\n<h2>\u041c\u0435\u0442\u043e\u0434\u044b ActiveRecord \u0432 Yii2<\/h2>\n<p>\u0412\u043e \u0432\u0442\u043e\u0440\u043e\u043c Yii \u0438\u0437 \u043a\u0443\u0447\u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043e\u0441\u0442\u0430\u0432\u0438\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u0435\u0442\u043e\u0434\u044b find() \u0438  findBySql($sql, $params). \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u0442\u0435\u043f\u0435\u0440\u044c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434 find(), \u043a\u043e\u0442\u043e\u0440\u044b\u0439, \u0432 \u043e\u0442\u043b\u0438\u0447\u0438\u0438 \u043e\u0442 \u043f\u0435\u0440\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438, \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u0438, \u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 ActiveQuery. \u041c\u0435\u0442\u043e\u0434 findBySql \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u0435\u0440\u0442\u043a\u043e\u0439 \u043d\u0430\u0434 \u043c\u0435\u0442\u043e\u0434\u043e\u043c find \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0432\u0441\u0435 \u0442\u043e\u0442 \u0436\u0435 \u043e\u0431\u044a\u0435\u043a\u0442 ActiveQuery. \u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438, \u0430\u043d\u0430\u043b\u043e\u0433\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0430 ActiveQuery \u0431\u044b\u043b \u043a\u043b\u0430\u0441\u0441 CDbCriteria.<br \/>\n\u0412 Yii2 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0445 \u0441\u0442\u0440\u043e\u043a \u0440\u0435\u0433\u0443\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u043c ActiveQuery \u0441\u043e \u0437\u0432\u0443\u0447\u043d\u044b\u043c\u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f\u043c\u0438 all \u0438 one <\/p>\n<pre class=\"brush: php; title: \u041f\u0440\u0438\u043c\u0435\u0440; notranslate\" title=\"\u041f\u0440\u0438\u043c\u0435\u0440\">\r\n\/\/ \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043e\u0434\u043d\u0443 \u0437\u0430\u043f\u0438\u0441\u044c \u0438\u0437 \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d\u0430 \u043c\u043e\u0434\u0435\u043b\u044c Texts\r\nTexts::find()-&gt;one();\r\n\/\/ \u0438\u043b\u0438 \u0432\u0441\u0435\r\nTexts::find()-&gt;all();\r\n<\/pre>\n<p>\u041f\u043e\u043c\u0438\u043c\u043e \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u0430\u043a\u0435\u0442\u0430\u043c\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432. <\/p>\n<pre class=\"brush: php; title: \u041f\u0440\u0438\u043c\u0435\u0440; notranslate\" title=\"\u041f\u0440\u0438\u043c\u0435\u0440\">\r\nforeach (Texts::find()-&gt;batch(100) as $text) {\r\n    \/\/ \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043f\u043e 100 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 $text\r\n}\r\n<\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 batch \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0430 Query, \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u0442\u0441\u044f ActiveQuery.<br \/>\n\u042d\u0442\u043e \u0432\u0430\u0436\u043d\u044b\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0433\u0434\u0430 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 php \u0442\u044b\u0441\u044f\u0447\u0438 \u0441\u0442\u0440\u043e\u043a \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438. \u0414\u043b\u044f \u043f\u0435\u0440\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 \u0444\u0440\u0435\u0438\u043c\u0432\u043e\u0440\u043a\u0430 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0430\u043d\u0430\u043b\u043e\u0433<\/p>\n<pre class=\"brush: php; title: \u0421\u0430\u043c\u043e\u043f\u0438\u0441\u043d\u044b\u0439 \u0430\u043d\u0430\u043b\u043e\u0433 batch \u0434\u043b\u044f Yii1; notranslate\" title=\"\u0421\u0430\u043c\u043e\u043f\u0438\u0441\u043d\u044b\u0439 \u0430\u043d\u0430\u043b\u043e\u0433 batch \u0434\u043b\u044f Yii1\">\r\n$n = 100;\r\n$all = Texts::model()-&gt;count();\r\n\r\n$criteria=new CDbCriteria;\r\nfor($i=0; $i&lt;=$all; $i+=$n){\r\n    $criteria-&gt;limit = $i + $n;\r\n    $criteria-&gt;offset = $i;\r\n    $text = Texts::model()-&gt;findAll($criteria);\r\n    \/\/ \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043f\u043e 100 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 $text\r\n}\r\n<\/pre>\n<p>\u0423\u0432\u0435\u0440\u0435\u043d\u0430, \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0435 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 batch \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0435\u0435. \u041d\u043e \u043f\u043e \u043c\u043e\u0435\u043c\u0443 \u043f\u0440\u0438\u043c\u0435\u0440\u0443 \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u043d\u044f\u0442\u044c \u0441\u043c\u044b\u0441\u043b \u044d\u0442\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430. <\/p>\n<h2>\u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 Yii1<\/h2>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441, \u0447\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043c\u043e\u0434\u0435\u043b\u0438 \u043f\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044e \u0438\u043b\u0438 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u043e\u043c\u0443 \u043a\u043b\u044e\u0447\u0443, \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 $condition \u0432 \u043c\u0435\u0442\u043e\u0434\u044b ActiveRecord Yii1 \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 CDbCriteria<\/p>\n<pre class=\"brush: php; title: \u041f\u0440\u0438\u043c\u0435\u0440; notranslate\" title=\"\u041f\u0440\u0438\u043c\u0435\u0440\">\r\n$criteria=new CDbCriteria;\r\n$criteria-&gt;with = 'service';\r\n$criteria-&gt;addCondition(&quot;service.kid = &quot;.$kID);\r\n$criteria-&gt;order = &quot;date&quot;;\r\n\r\nServiceStep::model()-&gt;findByPk($step, $criteria);\r\n<\/pre>\n<h2>\u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 Yii2<\/h2>\n<p>\u0412 Yii2 \u043d\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 CDbCriteria. \u0412\u0430\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u0435\u0432 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u043e\u0431\u044a\u0435\u043a\u0442 ActiveRecord. \u041a\u043e\u0434 \u0441\u0442\u0430\u043b \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0447\u0438\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u0435\u0435 \u0438 \u043f\u043e\u043d\u044f\u0442\u043d\u0435\u0439<\/p>\n<pre class=\"brush: php; title: \u041f\u0440\u0438\u043c\u0435\u0440; notranslate\" title=\"\u041f\u0440\u0438\u043c\u0435\u0440\">\r\n Text::find()\r\n      -&gt;joinWith('autor')\r\n      -&gt;where(['status' =&gt; 1])\r\n      -&gt;all();\r\n<\/pre>\n<h2>\u0421\u0432\u044f\u0437\u0438 \u0442\u0430\u0431\u043b\u0438\u0446 \u0432 Yii1<\/h2>\n<p>\u0414\u043b\u044f \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446 \u0432 Yii1 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f relations<\/p>\n<pre class=\"brush: php; title: \u041c\u0435\u0442\u043e\u0434 relations \u0432 \u043c\u043e\u0434\u0435\u043b\u0438; notranslate\" title=\"\u041c\u0435\u0442\u043e\u0434 relations \u0432 \u043c\u043e\u0434\u0435\u043b\u0438\"> \r\npublic function relations()\r\n{\r\n return array(\r\n  'service' =&gt; array(self::HAS_ONE, 'Service', array('id' =&gt; 'service_id')),\r\n );\r\n}\r\n<\/pre>\n<p>\u0414\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0439 BELONGS_TO, HAS_ONE, HAS_MANY, MANY_MANY. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0432 \u0441\u0432\u044f\u0437\u0438 \u043c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0434\u043b\u044f \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u044e\u0449\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u0442\u0438\u043f \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f, \u0441\u0432\u044f\u0437\u044c \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u044e\u0449\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0438 \u0442\u043f.<\/p>\n<pre class=\"brush: php; title: \u041f\u0440\u043e\u0441\u0442\u043e \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430; notranslate\" title=\"\u041f\u0440\u043e\u0441\u0442\u043e \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430\">\r\npublic function relations()\r\n{\r\n return array(\r\n 'posts'=&gt;array(self::HAS_MANY, 'Post', 'author_id',\r\n               'order'=&gt;'posts.create_time DESC',\r\n               'with'=&gt;'categories',\r\n               'through'=&gt;'users',\r\n               'joinType'=&gt;'INNER JOIN',\r\n               'on'=&gt;'user.id = t.user_id and user.status=1',\r\n               'condition' =&gt; 't.status =1'\r\n      ),\r\n );\r\n} \r\n<\/pre>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0432\u0430\u043c \u0432\u0440\u044f\u0442\u043b\u0438 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u044f\u0442\u0441\u044f \u0432\u0441\u0435 \u044d\u0442\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0441\u0440\u0430\u0437\u0443, \u043d\u043e \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u044f \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u043b\u0430 \u0432\u0441\u0435 \u0447\u0442\u043e \u0437\u043d\u0430\u043b\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043a\u0430\u043a\u0438\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0438 \u043a\u0430\u043a \u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \ud83d\ude42<br \/>\n\u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c \u043d\u0443\u0436\u043d\u044b\u0435 \u0441\u0432\u044f\u0437\u0438 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435:<\/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\/\/\u0442\u0430\u043a \r\n$groups = Group::model()-&gt;with('service')-&gt;findAll();\r\n\r\n\/\/\u0438\u043b\u0438 \u0442\u0430\u043a\r\n$criteria=new CDbCriteria;\r\n$criteria-&gt;with = 'service';\r\n$groups = Group::model()-&gt;findAll($criteria);\r\n<\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0438\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f:<\/p>\n<pre class=\"brush: php; title: \u041f\u0440\u0438\u043c\u0435\u0440; notranslate\" title=\"\u041f\u0440\u0438\u043c\u0435\u0440\">\r\n$groups = Group::model()\r\n          -&gt;with(array(\r\n               'service' =&gt; array(\r\n                   'condition'=&gt;'status = 1'\r\n                ))\r\n           )-&gt;findAll();\r\n<\/pre>\n<h2>\u0421\u0432\u044f\u0437\u0438 \u0442\u0430\u0431\u043b\u0438\u0446 \u0432 Yii2<\/h2>\n<p>\u0412 Yii2 \u0443\u0431\u0440\u0430\u043b\u0438 \u0438\u0437 \u043c\u043e\u0434\u0435\u043b\u0438 \u043c\u0435\u0442\u043e\u0434 relations(). \u0421\u0432\u044f\u0437\u0438 \u0442\u0430\u0431\u043b\u0438\u0446 \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0433\u0435\u0442\u0442\u0435\u0440\u044b<\/p>\n<pre class=\"brush: php; title: \u041f\u0440\u0438\u043c\u0435\u0440 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0432\u044f\u0437\u0438 \u0432 \u043c\u043e\u0434\u0435\u043b\u0438; notranslate\" title=\"\u041f\u0440\u0438\u043c\u0435\u0440 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0432\u044f\u0437\u0438 \u0432 \u043c\u043e\u0434\u0435\u043b\u0438\">\r\npublic function getZodiak()\r\n{\r\n   return $this-&gt;hasOne(ChinaZodiak::className(), ['id' =&gt; 'zodiak_id']);\r\n}\r\n<\/pre>\n<p>\u0418\u0437 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0439 \u043e\u0441\u0442\u0430\u0432\u0438\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e hasOne \u0438 hasMany, \u0447\u0442\u043e \u043f\u043e-\u043c\u043e\u0435\u043c\u0443 \u0445\u043e\u0440\u043e\u0448\u043e. \u042d\u0442\u0438\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0432\u0441\u0435 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \u0432 \u0440\u0435\u043b\u044f\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0411\u0414. \u0418 \u043f\u0443\u0442\u0430\u043d\u0438\u0446\u044b \u043c\u0435\u043d\u044c\u0448\u0435. BELONGS_TO \u043f\u043e \u0441\u0443\u0442\u0438 \u044d\u0442\u043e hasOne, \u0430 MANY_MANY \u044d\u0442\u043e hasMany \u0432 \u043e\u0431\u043e\u0438\u0445 \u043c\u043e\u0434\u0435\u043b\u044f\u0445 \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u0434\u0440\u0443\u0433 \u043a \u0434\u0440\u0443\u0433\u0443.<br \/>\n\u041e\u0431\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 (hasOne \u0438 hasMany) \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 ActiveQuery, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u043a \u043d\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c\u044b \u0432\u0441\u0435 \u0442\u0435 \u0436\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u0447\u0442\u043e \u0438 \u043f\u0440\u0438 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 Yii2 <\/p>\n<pre class=\"brush: php; title: \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0437 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438; notranslate\" title=\"\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0437 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438\">\r\npublic function getBigOrders($threshold = 100)\r\n{\r\n  return $this-&gt;hasMany(Order::className(), ['customer_id' =&gt; 'id'])\r\n            -&gt;where('subtotal &gt; :threshold', [':threshold' =&gt; $threshold])\r\n            -&gt;orderBy('id');\r\n}\r\n<\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c \u043d\u0443\u0436\u043d\u044b\u0435 \u0441\u0432\u044f\u0437\u0438 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435:<\/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 $result = ChinaZodiakGoroskop::find()\r\n          -&gt;joinWith('zodiak')\r\n          -&gt;all();\r\n<\/pre>\n<p>\u0412 \u043c\u0435\u0442\u043e\u0434 joinWith \u043a\u043b\u0430\u0441\u0441\u0430 ActiveQuery \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435:<br \/>\njoinWith($with, $eagerLoading = true, $joinType = &#8216;LEFT JOIN&#8217;)<br \/>\n\u0422\u0435 \u043f\u0435\u0440\u0432\u044b\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442 \u043e\u0434\u043d\u0443 \u0438\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u0432\u044f\u0437\u0435\u0439. \u0412\u0442\u043e\u0440\u043e\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 &#171;\u0436\u0430\u0434\u043d\u0443\u044e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443&#187; &#8212; \u0442\u0435 \u0441\u0440\u0430\u0437\u0443 \u043b\u0438 \u0432\u044b\u0431\u0435\u0440\u0443\u0442\u0441\u044f \u0437\u0430\u043f\u0438\u0441\u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0433\u0434\u0430, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u044f\u0442\u0441\u044f. \u0418 \u0442\u0440\u0435\u0442\u0438\u0439 &#8212; \u0442\u0438\u043f \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f, \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e LEFT JOIN.<br \/>\nYii2, \u043f\u043e \u0437\u0430\u044f\u0432\u043b\u0435\u043d\u0438\u044f\u043c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432, \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 php5, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0441\u0438\u0438 php5.3 \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c \u0430\u043d\u043e\u043d\u0438\u043c\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u043e\u043d\u0438 \u0436\u0435 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f). \u0418\u043c\u0435\u043d\u043d\u043e \u043e\u043d\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u0441\u0432\u044f\u0437\u0438 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435:<\/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\nOrder::find()-&gt;joinWith([\r\n     'books' =&gt; function ($query) {\r\n                   $query-&gt;orderBy('name');\r\n                 }\r\n      ])-&gt;all();\r\n<\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043c\u0435\u043d\u044f\u0442\u044c \u0441\u0432\u044f\u0437\u044c \u043d\u0435 \u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0432 Yii2.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0411\u043e\u043b\u044c\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 Yii-framework \u043c\u043d\u0435 \u043f\u043e\u043d\u0440\u0430\u0432\u0438\u043b\u043e\u0441\u044c \u0442\u043e, \u043a\u0430\u043a \u043f\u0435\u0440\u0435\u0434\u0435\u043b\u0430\u043b\u0438 ActiveRecord. \u0420\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 ActiveRecord \u0432 Yii2 \u0441\u0442\u0430\u043b\u043e \u043d\u0430\u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0443\u0434\u043e\u0431\u043d\u043e \u0438 \u043f\u0440\u0438\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043f\u0435\u0440\u0432\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e \u0444\u0440\u0435\u0438\u043c\u0432\u043e\u0440\u043a\u0430 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u0445\u043e\u0447\u0435\u0442\u0441\u044f. \u041c\u0435\u0442\u043e\u0434\u044b ActiveRecord \u0432 Yii1 \u0412 Yii1 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043b\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0432\u044b\u0431\u043e\u0440\u043a\u0438 \u0441\u0442\u0440\u043e\u043a \u0438\u0437 \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d\u0430 \u043c\u043e\u0434\u0435\u043b\u044c: find($condition, $params) &#8212; \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438, [&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,13],"_links":{"self":[{"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/posts\/111"}],"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=111"}],"version-history":[{"count":2,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/posts\/111\/revisions"}],"predecessor-version":[{"id":113,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/posts\/111\/revisions\/113"}],"wp:attachment":[{"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/media?parent=111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/categories?post=111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/tags?post=111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}