{"id":218471,"date":"2014-04-07T11:16:02","date_gmt":"2014-04-07T07:16:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=218471"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=218471","title":{"rendered":"<span class=\"post_title\">\u0423\u0441\u043a\u043e\u0440\u044f\u0435\u043c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043d\u0430 PHP \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438<\/span>"},"content":{"rendered":"<div class=\"content html_format\">\n<h4>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430<\/h4>\n<p>  \u0427\u0430\u0441\u0442\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0430\u043a (\u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043a\u043e\u0434\u0430 \u043d\u0430 Yii, \u043d\u043e \u043f\u043e\u0434\u0445\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u043a \u043b\u044e\u0431\u043e\u043c\u0443 \u043a\u043e\u0434\u0443):  <\/p>\n<pre><code class=\"php\">$model = new User(); $model-&gt;name = '\u0412\u0430\u0441\u044f'; if (!$model-&gt;save())     throw new RuntimeException('Can not save!'); <\/code><\/pre>\n<p>  \u0418\u043b\u0438 \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"php\">$model = User::model()-&gt;find(); if (!$model)     throw new CHttpException(404, 'User not found!'); <\/code><\/pre>\n<p>  \u041a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435:  <\/p>\n<pre><code class=\"php\">$dependency = new \\caching\\TagDependency('Post'); $posts = Post::model()-&gt;cache(1000, $dependency)-&gt;findAll(); <\/code><\/pre>\n<p>  \u0422\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438:  <\/p>\n<pre><code class=\"php\">$trx = $this-&gt;getDbConnection()-&gt;beginTransaction(); try {     if (!$user-&gt;makePayment())         throw new \\RuntimeException('Can not complete!');      $trx-&gt;commit(); } catch (\\Exception $e) {     $trx-&gt;rollback();      throw $e; } <\/code><\/pre>\n<p>  <b>\u0421\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430!<\/b><\/p>\n<h4>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h4>\n<p>  \u0412\u044b\u0448\u0435\u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0435 \u043a\u0443\u0441\u043a\u0438 \u043a\u043e\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u0434\u043e:<br \/>  \u0412\u043e\u0437\u0431\u0443\u0436\u0434\u0430\u0435\u043c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435\u0443\u0434\u0430\u0447\u043d\u043e\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430:  <\/p>\n<pre><code class=\"php\">$model-&gt;saveException(); User::model()-&gt;findException(); Post::model()-&gt;findAllCached(); <\/code><\/pre>\n<p>  \u041e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u043c\u0435\u0442\u043e\u0434 \u0432 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e:  <\/p>\n<pre><code class=\"php\">$user-&gt;makePaymentTrx(); <\/code><\/pre>\n<p>  \u0422\u0430\u043a\u0436\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0438:  <\/p>\n<pre><code class=\"php\">User::model()-&gt;findTrxCached() <\/code><\/pre>\n<p>  <a name=\"habracut\"><\/a><\/p>\n<h4>\u041a\u0430\u043a \u044d\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c?<\/h4>\n<p>  \u041d\u0430 Yii \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u0432\u043e\u0439 \u043a\u043b\u0430\u0441\u0441 ActiveRecord \u0438 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c \u043e\u0442 \u043d\u0435\u0433\u043e \u0441\u0432\u043e\u0438 \u043c\u043e\u0434\u0435\u043b\u0438:  <\/p>\n<pre><code class=\"php\">class ActiveRecord extends CActiveRecord { \tpublic function __call($name, $args) \t{ \t\tif (preg_match('\/^(.+)(cached|exception|trx)$\/i', $name, $matches)) { \t\t\tswitch (strtolower($matches[2])) { \t\t\t\tcase 'cached': \t\t\t\t\treturn $this-&gt;cachedMethod($matches[1], $args); \t\t\t\tcase 'exception': \t\t\t\t\treturn $this-&gt;exceptionMethod($matches[1], $args); \t\t\t\tcase 'trx': \t\t\t\t\treturn $this-&gt;trxMethod($matches[1], $args); \t\t\t} \t\t\t \t\t}  \t\treturn parent::__call($name, $args); \t}  \tpublic function trxMethod($method, $args) \t{ \t\t$trx = $this-&gt;getDbConnection()-&gt;beginTransaction(); \t\ttry { \t\t\t$value = call_user_func_array(array($this, $method), $args);  \t\t\t$trx-&gt;commit(); \t\t} catch (\\Exception $e) { \t\t\t$trx-&gt;rollback();  \t\t\tthrow $e; \t\t}  \t\treturn $value; \t}  \tpublic function exceptionMethod($method, $args) \t{\t \t\t$value = call_user_func_array(array($this, $method), $args); \t\t \t\tif (!$value) \t\t\tthrow new Exception('False result!'); \t\t \t\treturn $value; \t} \t \tpublic function cachedMethod($method, $args) \t{ \t\t$key = get_class($this) . $method . serialize($this-&gt;getPrimaryKey()) .  serialize($args) . serialize($this-&gt;getDbCriteria()); \t\t$key = md5($key);  \t\t$value = \\Yii::app()-&gt;cache-&gt;get($key);  \t\tif ($value === false) { \t\t\t$value = call_user_func_array(array($this, $method), $args); \t\t\t\\Yii::app()-&gt;cache-&gt;set($key, $value, 0, new \\caching\\TagDependency(get_class($this))); \t\t} else { \t\t\t\/\/reset scope as in find*() methods \t\t\t$this-&gt;resetScope(); \t\t}  \t\treturn $value; \t} } <\/code><\/pre>\n<p>  \u041e\u0441\u043d\u043e\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 \u2014 \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435:  <\/p>\n<pre><code class=\"php\">preg_match('\/^(.+)(cached|exception|trx)$\/i', $name, $matches) <\/code><\/pre>\n<p>  \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u044e\u0434\u0430 \u0441\u0432\u043e\u0438 \u0441\u0443\u0444\u0444\u0438\u043a\u0441\u044b.<\/p>\n<h4>\u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430:<\/h4>\n<p>  <\/p>\n<ul>\n<li>\u0420\u0443\u0442\u0438\u043d\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u043c\u0435\u043d\u044c\u0448\u0435 \u043c\u0435\u0441\u0442\u0430 \u0432 \u043a\u043e\u0434\u0435.<\/li>\n<li>\u0411\u044b\u0441\u0442\u0440\u0435\u0435 \u0438 \u043f\u0440\u043e\u0449\u0435 \u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434.<\/li>\n<\/ul>\n<h4>\u041d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438:<\/h4>\n<p>  <\/p>\n<ul>\n<li>IDE \u043d\u0435 \u0432\u0438\u0434\u0438\u0442 \u0442\u0430\u043a\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0438\u043b\u0438 \u0438\u0445 \u043d\u0430\u0434\u043e \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0432\u0440\u0443\u0447\u043d\u0443\u044e, \u0447\u0442\u043e \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u043e.<\/li>\n<\/ul>\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\/218471\/\"> http:\/\/habrahabr.ru\/post\/218471\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">\n<h4>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430<\/h4>\n<p>  \u0427\u0430\u0441\u0442\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0430\u043a (\u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043a\u043e\u0434\u0430 \u043d\u0430 Yii, \u043d\u043e \u043f\u043e\u0434\u0445\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u043a \u043b\u044e\u0431\u043e\u043c\u0443 \u043a\u043e\u0434\u0443):  <\/p>\n<pre><code class=\"php\">$model = new User(); $model-&gt;name = '\u0412\u0430\u0441\u044f'; if (!$model-&gt;save())     throw new RuntimeException('Can not save!'); <\/code><\/pre>\n<p>  \u0418\u043b\u0438 \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"php\">$model = User::model()-&gt;find(); if (!$model)     throw new CHttpException(404, 'User not found!'); <\/code><\/pre>\n<p>  \u041a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435:  <\/p>\n<pre><code class=\"php\">$dependency = new \\caching\\TagDependency('Post'); $posts = Post::model()-&gt;cache(1000, $dependency)-&gt;findAll(); <\/code><\/pre>\n<p>  \u0422\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438:  <\/p>\n<pre><code class=\"php\">$trx = $this-&gt;getDbConnection()-&gt;beginTransaction(); try {     if (!$user-&gt;makePayment())         throw new \\RuntimeException('Can not complete!');      $trx-&gt;commit(); } catch (\\Exception $e) {     $trx-&gt;rollback();      throw $e; } <\/code><\/pre>\n<p>  <b>\u0421\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430!<\/b><\/p>\n<h4>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h4>\n<p>  \u0412\u044b\u0448\u0435\u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0435 \u043a\u0443\u0441\u043a\u0438 \u043a\u043e\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u0434\u043e:<br \/>  \u0412\u043e\u0437\u0431\u0443\u0436\u0434\u0430\u0435\u043c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435\u0443\u0434\u0430\u0447\u043d\u043e\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430:  <\/p>\n<pre><code class=\"php\">$model-&gt;saveException(); User::model()-&gt;findException(); Post::model()-&gt;findAllCached(); <\/code><\/pre>\n<p>  \u041e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u043c\u0435\u0442\u043e\u0434 \u0432 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e:  <\/p>\n<pre><code class=\"php\">$user-&gt;makePaymentTrx(); <\/code><\/pre>\n<p>  \u0422\u0430\u043a\u0436\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0438:  <\/p>\n<pre><code class=\"php\">User::model()-&gt;findTrxCached() <\/code><\/pre>\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-218471","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/218471","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=218471"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/218471\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=218471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=218471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=218471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}