1: <?php
2: /**
3: * CDbCacheDependency class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CDbCacheDependency represents a dependency based on the query result of a SQL statement.
13: *
14: * If the query result (a scalar) changes, the dependency is considered as changed.
15: * To specify the SQL statement, set {@link sql} property.
16: * The {@link connectionID} property specifies the ID of a {@link CDbConnection} application
17: * component. It is this DB connection that is used to perform the query.
18: *
19: * @author Qiang Xue <qiang.xue@gmail.com>
20: * @package system.caching.dependencies
21: * @since 1.0
22: */
23: class CDbCacheDependency extends CCacheDependency
24: {
25: /**
26: * @var string the ID of a {@link CDbConnection} application component. Defaults to 'db'.
27: */
28: public $connectionID='db';
29: /**
30: * @var string the SQL statement whose result is used to determine if the dependency has been changed.
31: * Note, the SQL statement should return back a single value.
32: */
33: public $sql;
34: /**
35: * @var array parameters (name=>value) to be bound to the SQL statement specified by {@link sql}.
36: * @since 1.1.4
37: */
38: public $params;
39:
40: private $_db;
41:
42: /**
43: * Constructor.
44: * @param string $sql the SQL statement whose result is used to determine if the dependency has been changed.
45: */
46: public function __construct($sql=null)
47: {
48: $this->sql=$sql;
49: }
50:
51: /**
52: * PHP sleep magic method.
53: * This method ensures that the database instance is set null because it contains resource handles.
54: * @return array
55: */
56: public function __sleep()
57: {
58: $this->_db=null;
59: return array_keys((array)$this);
60: }
61:
62: /**
63: * Generates the data needed to determine if dependency has been changed.
64: * This method returns the value of the global state.
65: * @throws CException if {@link sql} is empty
66: * @return mixed the data needed to determine if dependency has been changed.
67: */
68: protected function generateDependentData()
69: {
70: if($this->sql!==null)
71: {
72: $db=$this->getDbConnection();
73: $command=$db->createCommand($this->sql);
74: if(is_array($this->params))
75: {
76: foreach($this->params as $name=>$value)
77: $command->bindValue($name,$value);
78: }
79: if($db->queryCachingDuration>0)
80: {
81: // temporarily disable and re-enable query caching
82: $duration=$db->queryCachingDuration;
83: $db->queryCachingDuration=0;
84: $result=$command->queryRow();
85: $db->queryCachingDuration=$duration;
86: }
87: else
88: $result=$command->queryRow();
89: return $result;
90: }
91: else
92: throw new CException(Yii::t('yii','CDbCacheDependency.sql cannot be empty.'));
93: }
94:
95: /**
96: * @return CDbConnection the DB connection instance
97: * @throws CException if {@link connectionID} does not point to a valid application component.
98: */
99: protected function getDbConnection()
100: {
101: if($this->_db!==null)
102: return $this->_db;
103: else
104: {
105: if(($this->_db=Yii::app()->getComponent($this->connectionID)) instanceof CDbConnection)
106: return $this->_db;
107: else
108: throw new CException(Yii::t('yii','CDbCacheDependency.connectionID "{id}" is invalid. Please make sure it refers to the ID of a CDbConnection application component.',
109: array('{id}'=>$this->connectionID)));
110: }
111: }
112: }
113: