DbDependency.php 2.94 KB
Newer Older
Qiang Xue committed
1 2
<?php
/**
Qiang Xue committed
3
 * DbDependency class file.
Qiang Xue committed
4 5
 *
 * @link http://www.yiiframework.com/
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008 Yii Software LLC
Qiang Xue committed
7 8 9
 * @license http://www.yiiframework.com/license/
 */

Qiang Xue committed
10 11
namespace yii\caching;

12
use yii\base\InvalidConfigException;
Qiang Xue committed
13 14
use yii\db\Connection;
use yii\db\Query;
Qiang Xue committed
15

Qiang Xue committed
16
/**
Qiang Xue committed
17
 * DbDependency represents a dependency based on the query result of a SQL statement.
Qiang Xue committed
18
 *
Qiang Xue committed
19 20
 * If the query result changes, the dependency is considered as changed.
 * The query is specified via the [[query]] property.
Qiang Xue committed
21 22
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
23
 * @since 2.0
Qiang Xue committed
24
 */
Qiang Xue committed
25
class DbDependency extends Dependency
Qiang Xue committed
26 27
{
	/**
Qiang Xue committed
28
	 * @var string the ID of the [[Connection|DB connection]] application component. Defaults to 'db'.
Qiang Xue committed
29
	 */
Qiang Xue committed
30
	public $connectionID = 'db';
Qiang Xue committed
31
	/**
Qiang Xue committed
32 33
	 * @var Query the SQL query whose result is used to determine if the dependency has been changed.
	 * Only the first row of the query result will be used.
Qiang Xue committed
34
	 */
Qiang Xue committed
35
	public $query;
Qiang Xue committed
36
	/**
Qiang Xue committed
37
	 * @var Connection the DB connection instance
Qiang Xue committed
38 39 40 41 42
	 */
	private $_db;

	/**
	 * Constructor.
Qiang Xue committed
43
	 * @param Query $query the SQL query whose result is used to determine if the dependency has been changed.
Qiang Xue committed
44
	 * @param array $config name-value pairs that will be used to initialize the object properties
Qiang Xue committed
45
	 */
Qiang Xue committed
46
	public function __construct($query = null, $config = array())
Qiang Xue committed
47
	{
Qiang Xue committed
48
		$this->query = $query;
Qiang Xue committed
49
		parent::__construct($config);
Qiang Xue committed
50 51 52 53 54 55 56 57 58
	}

	/**
	 * PHP sleep magic method.
	 * This method ensures that the database instance is set null because it contains resource handles.
	 * @return array
	 */
	public function __sleep()
	{
Qiang Xue committed
59
		$this->_db = null;
Qiang Xue committed
60 61 62 63 64 65 66 67
		return array_keys((array)$this);
	}

	/**
	 * Generates the data needed to determine if dependency has been changed.
	 * This method returns the value of the global state.
	 * @return mixed the data needed to determine if dependency has been changed.
	 */
Qiang Xue committed
68
	protected function generateDependencyData()
Qiang Xue committed
69
	{
Qiang Xue committed
70
		$db = $this->getDb();
71 72 73
		/**
		 * @var \yii\db\Command $command
		 */
Qiang Xue committed
74
		$command = $this->query->createCommand($db);
75
		if ($db->enableQueryCache) {
Qiang Xue committed
76
			// temporarily disable and re-enable query caching
77
			$db->enableQueryCache = false;
Qiang Xue committed
78
			$result = $command->queryRow();
79
			$db->enableQueryCache = true;
Qiang Xue committed
80
		} else {
Qiang Xue committed
81
			$result = $command->queryRow();
Qiang Xue committed
82
		}
Qiang Xue committed
83
		return $result;
Qiang Xue committed
84 85 86
	}

	/**
Qiang Xue committed
87 88
	 * Returns the DB connection instance used for caching purpose.
	 * @return Connection the DB connection instance
89
	 * @throws InvalidConfigException if [[connectionID]] does not point to a valid application component.
Qiang Xue committed
90
	 */
Qiang Xue committed
91
	public function getDb()
Qiang Xue committed
92
	{
Qiang Xue committed
93 94 95 96
		if ($this->_db === null) {
			$db = \Yii::$application->getComponent($this->connectionID);
			if ($db instanceof Connection) {
				$this->_db = $db;
Qiang Xue committed
97
			} else {
98
				throw new InvalidConfigException("DbCache::connectionID must refer to the ID of a DB application component.");
Qiang Xue committed
99
			}
Qiang Xue committed
100
		}
Qiang Xue committed
101 102 103 104 105 106 107
		return $this->_db;
	}

	/**
	 * Sets the DB connection used by the cache component.
	 * @param Connection $value the DB connection instance
	 */
Qiang Xue committed
108
	public function setDb($value)
Qiang Xue committed
109 110
	{
		$this->_db = $value;
Qiang Xue committed
111 112
	}
}