DbDependency.php 2.97 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-2012 Yii Software LLC
Qiang Xue committed
7 8 9
 * @license http://www.yiiframework.com/license/
 */

Qiang Xue committed
10 11
namespace yii\caching;

Qiang Xue committed
12 13 14
use yii\base\Exception;
use yii\db\dao\Connection;
use yii\db\dao\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 71 72 73 74 75 76 77
		$db = $this->getDbConnection();
		$command = $this->query->createCommand($db);
		if ($db->queryCachingDuration >= 0) {
			// temporarily disable and re-enable query caching
			$duration = $db->queryCachingDuration;
			$db->queryCachingDuration = -1;
			$result = $command->queryRow();
			$db->queryCachingDuration = $duration;
Qiang Xue committed
78
		} else {
Qiang Xue committed
79
			$result = $command->queryRow();
Qiang Xue committed
80
		}
Qiang Xue committed
81
		return $result;
Qiang Xue committed
82 83 84
	}

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

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