{"id":185842,"date":"2013-07-08T03:09:03","date_gmt":"2013-07-07T23:09:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=185842"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=185842","title":{"rendered":"<span class=\"post_title\">\u0421\u0442\u0440\u0430\u043d\u043d\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432 CActiveRecord<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u0418\u043b\u0438 \u043f\u043e\u0432\u0435\u0441\u0442\u044c \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043d\u0435\u0447\u0430\u044f\u043d\u043d\u043e \u0432\u044b\u0441\u0442\u0440\u0435\u043b\u0438\u0442\u044c \u0441\u0435\u0431\u0435 \u0432 \u043d\u043e\u0433\u0443.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h4>\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/h4>\n<p>  \u0421\u0438\u0436\u0443 \u0441\u0435\u0431\u0435 \u043e\u0442\u043b\u0430\u0436\u0438\u0432\u0430\u044e \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u0442\u0440\u0430\u0433\u0438\u0432\u0430\u0435\u0442 \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 CActiveRecord.<br \/>  \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c:  <\/p>\n<ul>\n<li>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c<\/li>\n<li>\u0417\u0430\u0434\u0430\u0435\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f<\/li>\n<li>\u0421\u043e\u0437\u0434\u0430\u0435\u043c CActiveDataProvider<\/li>\n<li>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0435\u0433\u043e \u043f\u0440\u0438 \u0432\u044b\u0432\u043e\u0434\u0435 CGridView<\/li>\n<\/ul>\n<p>  \u0427\u0430\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u2116 1, \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440:  <\/p>\n<pre><code class=\"php\">public function actions(){         return array(             'list' =&gt; array(                 'view' =&gt; 'list',                 'class' =&gt; 'app\\components\\actions\\GridAction',                 'columns' =&gt; array(                     'display_name',                     'email',                     array(                         'name' =&gt; 'role',                         'filter' =&gt; array(                             User::ROLE_ADMIN =&gt; \\Yii::t('admin','Administrator'),                             User::ROLE_MANAGER =&gt; \\Yii::t('admin','Manager'),                             User::ROLE_USER =&gt; \\Yii::t('admin','User')                         )                     )                 ),                 'beforeRender' =&gt; function() {                     $this-&gt;breadcrumbs[] = \\Yii::t('admin', 'Manage users');                 },                 'model' =&gt; function(){                     $model = new User('search');                     return $model-&gt;cache(new Tags('users'));                 }             ); } <\/code><\/pre>\n<p>  \u0427\u0430\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u2116 2, \u044d\u043a\u0448\u043d:  <\/p>\n<pre><code class=\"php\">ApplicationHelpers::loadData($this-&gt;model);  $provider = $this-&gt;model-&gt;search(); $filter = $this-&gt;model;  $grid = $this-&gt;controller-&gt;createWidget('app\\widgets\\grid\\GridView',array(             'dataProvider' =&gt; $provider,             'columns' =&gt; !empty($this-&gt;columns)?$this-&gt;columns:array(),             'id' =&gt; ApplicationHelpers::safeHtmlName($this-&gt;controller).&quot;_{$this-&gt;id}_grid&quot;,             'filter' =&gt; $filter )); <\/code><\/pre>\n<p>  \u0413\u0440\u0438\u0434 \u0432\u044b\u0432\u0435\u043b\u0441\u044f, \u0441\u043c\u043e\u0442\u0440\u044e \u043b\u043e\u0433:<br \/>  <code> Querying SQL: SELECT * FROM `tbl_user` `t` LIMIT 50 in protected\/widgets\/grid\/GridView.php (18) in protected\/components\/actions\/GridAction.php (34) in admin.php (18) <\/code><br \/>  \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0438 \u043d\u0435 \u043f\u0430\u0445\u043d\u0435\u0442.<br \/>  \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 CActiveRecord \u0432 public static $db; \u0438 \u0441\u043b\u0435\u0442\u0430\u044e\u0442 \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f CActiveDataProvider \u0432 $this-&gt;model-&gt;search(). \u00ab\u0412\u043e\u0442 \u0447\u0443\u0434\u0435\u0441\u0430\u00bb, \u043d\u0435\u0434\u043e\u0443\u043c\u0435\u0432\u0430\u044e \u044f. <br \/>  \u041f\u043e\u0441\u043b\u0435 \u043f\u0430\u0440\u044b \u043c\u0438\u043d\u0443\u0442 \u0434\u0435\u0431\u0430\u0433\u0430 \u0432\u0441\u043f\u043e\u043c\u0438\u043d\u0430\u044e, \u0447\u0442\u043e \u0443 \u043c\u0435\u043d\u044f \u043a CActiveDataProvider \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d behavior. <br \/>  \u0412\u043e\u0442 \u0442\u0430\u043a\u043e\u0439:  <\/p>\n<pre><code class=\"php\">namespace app\\components\\behaviors;  class AutoPagingBehavior extends \\CBehavior{     public function attach($owner)     {         parent::attach($owner);          $pageSize = \\Yii::app()-&gt;params['defaultPageSize'];          if(isset($owner-&gt;id)){             $pageSize = \\Yii::app()-&gt;user-&gt;getState('pagesize_'.$owner-&gt;id,\\Yii::app()-&gt;params['defaultPageSize']);         }          if($requestPageSize = \\Yii::app()-&gt;request-&gt;getParam('pagesize',false)){             if(is_array($requestPageSize) && isset($requestPageSize[$owner-&gt;id])){                 $pageSize = $requestPageSize[$owner-&gt;id];                 \\Yii::app()-&gt;user-&gt;setState(&quot;pagesize_{$owner-&gt;id}&quot;,$pageSize);             }else{                 $_GET['pagesize'] = [$owner-&gt;id =&gt; $pageSize];             }         }          $owner-&gt;pagination-&gt;setPageSize($pageSize);         if($pageSize == 'all')             $owner-&gt;pagination = false;     }  } <\/code><\/pre>\n<p>  \u0414\u0435\u0431\u0430\u0433\u0433\u0435\u0440 \u0443\u0441\u0438\u043b\u0435\u043d\u043d\u043e \u0440\u0430\u043f\u043e\u0440\u0442\u0443\u0435\u0442, \u0447\u0442\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u043b\u0435\u0442\u0430\u044e\u0442 \u043f\u043e\u0441\u043b\u0435 Yii::app()-&gt;user-&gt;getState.<br \/>  \u0421\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430\u0448 WebUser.php, \u0430 \u0442\u0430\u043c:  <\/p>\n<pre><code class=\"php\">public function init(){     parent::init();     if(!$this-&gt;isGuest){         $this-&gt;model = User::model()-&gt;findByEmail($this-&gt;id);         if(empty($this-&gt;model)){             $this-&gt;logout(true);             \\Yii::app()-&gt;controller-&gt;redirect('\/');         }     } } <\/code><\/pre>\n<p>\u0412\u044b\u0437\u043e\u0432 User::model()-&gt;findByEmail($this-&gt;id) \u044d\u0442\u043e \u0445\u0435\u043b\u043f\u0435\u0440, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a. \u0418 \u044d\u0442\u043e\u0442 \u043f\u043e\u0438\u0441\u043a \u0431\u043b\u0430\u0433\u043e\u043f\u043e\u043b\u0443\u0447\u043d\u043e \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432 public static $db, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 CActiveRecord.<\/p>\n<h4>\u0412\u044b\u0432\u043e\u0434<\/h4>\n<p>  \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0432 \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043a\u0435 \u043c\u0435\u0436\u0434\u0443 \u0437\u0430\u0434\u0430\u043d\u0438\u0435\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0438 \u0441\u0430\u043c\u0438\u043c \u043f\u043e\u0438\u0441\u043a\u043e\u043c \u0447\u0435\u0440\u0435\u0437 CActiveDataProvider \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u0435 \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b <b>\u043b\u044e\u0431\u043e\u0439<\/b> \u043c\u043e\u0434\u0435\u043b\u0438, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e CDbConnection \u043e\u0434\u0438\u043d \u043d\u0430 \u0432\u0441\u0435\u0445. \u041f\u0443\u0442\u044c \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043e\u0447\u0435\u0432\u0438\u0434\u0435\u043d \u2014 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0435 CActiveRecord \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 (CActiveRecord#query)<\/p>\n<h4>\u0418\u0442\u043e\u0433<\/h4>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/ee9\/6a5\/b72\/ee96a5b72089d1478b85c02ec7d1451b.jpeg\"\/>    \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\/185842\/\"> http:\/\/habrahabr.ru\/post\/185842\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u0418\u043b\u0438 \u043f\u043e\u0432\u0435\u0441\u0442\u044c \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043d\u0435\u0447\u0430\u044f\u043d\u043d\u043e \u0432\u044b\u0441\u0442\u0440\u0435\u043b\u0438\u0442\u044c \u0441\u0435\u0431\u0435 \u0432 \u043d\u043e\u0433\u0443.  <\/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-185842","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/185842","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=185842"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/185842\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=185842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=185842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=185842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}