$model = new User(); $model->name = ''; if (!$model->save()) throw new RuntimeException('Can not save!');
$model = User::model()->find(); if (!$model) throw new CHttpException(404, 'User not found!');
$dependency = new \caching\TagDependency('Post'); $posts = Post::model()->cache(1000, $dependency)->findAll();
$trx = $this->getDbConnection()->beginTransaction(); try { if (!$user->makePayment()) throw new \RuntimeException('Can not complete!'); $trx->commit(); } catch (\Exception $e) { $trx->rollback(); throw $e; }
$model->saveException(); User::model()->findException(); Post::model()->findAllCached();
$user->makePaymentTrx();
User::model()->findTrxCached()
class ActiveRecord extends CActiveRecord { public function __call($name, $args) { if (preg_match('/^(.+)(cached|exception|trx)$/i', $name, $matches)) { switch (strtolower($matches[2])) { case 'cached': return $this->cachedMethod($matches[1], $args); case 'exception': return $this->exceptionMethod($matches[1], $args); case 'trx': return $this->trxMethod($matches[1], $args); } } return parent::__call($name, $args); } public function trxMethod($method, $args) { $trx = $this->getDbConnection()->beginTransaction(); try { $value = call_user_func_array(array($this, $method), $args); $trx->commit(); } catch (\Exception $e) { $trx->rollback(); throw $e; } return $value; } public function exceptionMethod($method, $args) { $value = call_user_func_array(array($this, $method), $args); if (!$value) throw new Exception('False result!'); return $value; } public function cachedMethod($method, $args) { $key = get_class($this) . $method . serialize($this->getPrimaryKey()) . serialize($args) . serialize($this->getDbCriteria()); $key = md5($key); $value = \Yii::app()->cache->get($key); if ($value === false) { $value = call_user_func_array(array($this, $method), $args); \Yii::app()->cache->set($key, $value, 0, new \caching\TagDependency(get_class($this))); } else { //reset scope as in find*() methods $this->resetScope(); } return $value; } }
preg_match('/^(.+)(cached|exception|trx)$/i', $name, $matches)
Source: https://habr.com/ru/post/218471/
All Articles