@ -1,6 +1,6 @@
<?php
<?php
/**
/**
* C DbCache Dependency class file.
* DbDependency class file.
*
*
* @link http://www.yiiframework.com/
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2012 Yii Software LLC
* @copyright Copyright © 2008-2012 Yii Software LLC
@ -11,7 +11,7 @@ namespace yii\caching;
/**
/**
* C DbCache Dependency represents a dependency based on the query result of a SQL statement.
* DbDependency represents a dependency based on the query result of a SQL statement.
*
*
* If the query result (a scalar) changes, the dependency is considered as changed.
* If the query result (a scalar) changes, the dependency is considered as changed.
* To specify the SQL statement, set {@link sql} property.
* To specify the SQL statement, set {@link sql} property.
@ -21,7 +21,7 @@ namespace yii\caching;
* @author Qiang Xue < qiang.xue @ gmail . com >
* @author Qiang Xue < qiang.xue @ gmail . com >
* @since 2.0
* @since 2.0
*/
*/
class C DbCache Dependency extends C CacheDependency
class DbDependency extends CacheDependency
{
{
/**
/**
* @var string the ID of a {@link CDbConnection} application component. Defaults to 'db'.
* @var string the ID of a {@link CDbConnection} application component. Defaults to 'db'.
@ -65,31 +65,29 @@ class CDbCacheDependency extends CCacheDependency
* This method returns the value of the global state.
* This method returns the value of the global state.
* @return mixed the data needed to determine if dependency has been changed.
* @return mixed the data needed to determine if dependency has been changed.
*/
*/
protected function generateDependentData()
protected function generateDependencyData()
{
if($this->sql!==null)
{
{
if ($this->sql !== null) {
$db = $this->getDbConnection();
$db = $this->getDbConnection();
$command = $db->createCommand($this->sql);
$command = $db->createCommand($this->sql);
if(is_array($this->params))
if (is_array($this->params)) {
{
foreach ($this->params as $name => $value) {
foreach($this->params as $name=>$value)
$command->bindValue($name, $value);
$command->bindValue($name, $value);
}
}
if($db->queryCachingDuration>0)
}
{
if ($db->queryCachingDuration > 0) {
// temporarily disable and re-enable query caching
// temporarily disable and re-enable query caching
$duration = $db->queryCachingDuration;
$duration = $db->queryCachingDuration;
$db->queryCachingDuration = 0;
$db->queryCachingDuration = 0;
$result = $command->queryRow();
$result = $command->queryRow();
$db->queryCachingDuration = $duration;
$db->queryCachingDuration = $duration;
}
} else {
else
$result = $command->queryRow();
$result = $command->queryRow();
}
return $result;
return $result;
} else {
throw new CException(Yii::t('yii', 'DbDependency.sql cannot be empty.'));
}
}
else
throw new CException(Yii::t('yii','CDbCacheDependency.sql cannot be empty.'));
}
}
/**
/**
@ -98,15 +96,15 @@ class CDbCacheDependency extends CCacheDependency
*/
*/
protected function getDbConnection()
protected function getDbConnection()
{
{
if($this->_db!==null)
if ($this->_db !== null) {
return $this->_db;
return $this->_db;
else
} else {
{
if (($this->_db = \Yii::$application->getComponent($this->connectionID)) instanceof CDbConnection) {
if(($this->_db=\Yii::$application->getComponent($this->connectionID)) instanceof CDbConnection)
return $this->_db;
return $this->_db;
else
} else {
throw new CException(Yii::t('yii','C DbCache Dependency.connectionID "{id}" is invalid. Please make sure it refers to the ID of a CDbConnection application component.',
throw new CException(Yii::t('yii', 'DbDependency.connectionID "{id}" is invalid. Please make sure it refers to the ID of a CDbConnection application component.',
array('{id}' => $this->connectionID)));
array('{id}' => $this->connectionID)));
}
}
}
}
}
}
}