3
* @link https://www.yiiframework.com/
4
* @copyright Copyright (c) 2008 Yii Software LLC
5
* @license https://www.yiiframework.com/license/
11
use yii\base\InvalidConfigException;
16
* DbDependency represents a dependency based on the query result of a SQL statement.
18
* If the query result changes, the dependency is considered as changed.
19
* The query is specified via the [[sql]] property.
21
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
23
* @author Qiang Xue <qiang.xue@gmail.com>
26
class DbDependency extends Dependency
29
* @var string the application component ID of the DB connection.
33
* @var string the SQL query whose result is used to determine if the dependency has been changed.
34
* Only the first row of the query result will be used.
38
* @var array the parameters (name => value) to be bound to the SQL statement specified by [[sql]].
44
* Generates the data needed to determine if dependency has been changed.
45
* This method returns the value of the global state.
46
* @param CacheInterface $cache the cache component that is currently evaluating this dependency
47
* @return mixed the data needed to determine if dependency has been changed.
48
* @throws InvalidConfigException if [[db]] is not a valid application component ID
50
protected function generateDependencyData($cache)
52
/* @var $db Connection */
53
$db = Instance::ensure($this->db, Connection::className());
54
if ($this->sql === null) {
55
throw new InvalidConfigException('DbDependency::sql must be set.');
58
if ($db->enableQueryCache) {
59
// temporarily disable and re-enable query caching
60
$db->enableQueryCache = false;
61
$result = $db->createCommand($this->sql, $this->params)->queryOne();
62
$db->enableQueryCache = true;
64
$result = $db->createCommand($this->sql, $this->params)->queryOne();