You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					76 lines
				
				2.3 KiB
			
		
		
			
		
	
	
					76 lines
				
				2.3 KiB
			| 
											14 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
| 
											13 years ago
										 |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
| 
											14 years ago
										 |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
| 
											14 years ago
										 | namespace yii\caching;
 | ||
|  | 
 | ||
| 
											13 years ago
										 | use Yii;
 | ||
| 
											13 years ago
										 | use yii\base\InvalidConfigException;
 | ||
| 
											13 years ago
										 | use yii\db\Connection;
 | ||
| 
											14 years ago
										 | 
 | ||
| 
											14 years ago
										 | /**
 | ||
| 
											14 years ago
										 |  * DbDependency represents a dependency based on the query result of a SQL statement.
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											14 years ago
										 |  * If the query result changes, the dependency is considered as changed.
 | ||
| 
											13 years ago
										 |  * The query is specified via the [[sql]] property.
 | ||
| 
											14 years ago
										 |  *
 | ||
|  |  * @author Qiang Xue <qiang.xue@gmail.com>
 | ||
| 
											14 years ago
										 |  * @since 2.0
 | ||
| 
											14 years ago
										 |  */
 | ||
| 
											14 years ago
										 | class DbDependency extends Dependency
 | ||
| 
											14 years ago
										 | {
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var string the application component ID of the DB connection.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $db = 'db';
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var string the SQL query whose result is used to determine if the dependency has been changed.
 | ||
| 
											13 years ago
										 | 	 * Only the first row of the query result will be used.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $sql;
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var array the parameters (name => value) to be bound to the SQL statement specified by [[sql]].
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $params;
 | ||
| 
											14 years ago
										 | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Constructor.
 | ||
|  | 	 * @param string $sql the SQL query whose result is used to determine if the dependency has been changed.
 | ||
| 
											13 years ago
										 | 	 * @param array $params the parameters (name => value) to be bound to the SQL statement specified by [[sql]].
 | ||
| 
											13 years ago
										 | 	 * @param array $config name-value pairs that will be used to initialize the object properties
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public function __construct($sql, $params = array(), $config = array())
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		$this->sql = $sql;
 | ||
|  | 		$this->params = $params;
 | ||
|  | 		parent::__construct($config);
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Generates the data needed to determine if dependency has been changed.
 | ||
|  | 	 * This method returns the value of the global state.
 | ||
| 
											13 years ago
										 | 	 * @throws InvalidConfigException
 | ||
| 
											14 years ago
										 | 	 * @return mixed the data needed to determine if dependency has been changed.
 | ||
|  | 	 */
 | ||
| 
											14 years ago
										 | 	protected function generateDependencyData()
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		$db = Yii::$app->getComponent($this->db);
 | ||
|  | 		if (!$db instanceof Connection) {
 | ||
|  | 			throw new InvalidConfigException("DbDependency::db must be the application component ID of a DB connection.");
 | ||
|  | 		}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 		if ($db->enableQueryCache) {
 | ||
| 
											14 years ago
										 | 			// temporarily disable and re-enable query caching
 | ||
| 
											13 years ago
										 | 			$db->enableQueryCache = false;
 | ||
| 
											13 years ago
										 | 			$result = $db->createCommand($this->sql, $this->params)->queryRow();
 | ||
| 
											13 years ago
										 | 			$db->enableQueryCache = true;
 | ||
| 
											14 years ago
										 | 		} else {
 | ||
| 
											13 years ago
										 | 			$result = $db->createCommand($this->sql, $this->params)->queryRow();
 | ||
| 
											14 years ago
										 | 		}
 | ||
| 
											14 years ago
										 | 		return $result;
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | }
 |