CDbCacheDependency.php 3.2 KB

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