DbQueryDependency.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\caching;
  8. use yii\base\InvalidConfigException;
  9. use yii\db\QueryInterface;
  10. use yii\di\Instance;
  11. /**
  12. * DbQueryDependency represents a dependency based on the query result of an [[QueryInterface]] instance.
  13. *
  14. * If the query result changes, the dependency is considered as changed.
  15. * The query is specified via the [[query]] property.
  16. *
  17. * Object of any class which matches [[QueryInterface]] can be used, so this dependency can be used not only
  18. * with regular relational databases but with MongoDB, Redis and so on as well.
  19. *
  20. * For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
  21. *
  22. * @see QueryInterface
  23. *
  24. * @author Paul Klimov <klimov.paul@gmail.com>
  25. * @since 2.0.12
  26. */
  27. class DbQueryDependency extends Dependency
  28. {
  29. /**
  30. * @var string|array|object the application component ID of the database connection, connection object or
  31. * its array configuration.
  32. * This field can be left blank, allowing query to determine connection automatically.
  33. */
  34. public $db;
  35. /**
  36. * @var QueryInterface the query which result is used to determine if the dependency has been changed.
  37. * Actual query method to be invoked is determined by [[method]].
  38. */
  39. public $query;
  40. /**
  41. * @var string|callable method which should be invoked in over the [[query]] object.
  42. *
  43. * If specified as a string an own query method with such name will be invoked, passing [[db]] value as its
  44. * first argument. For example: `exists`, `all`.
  45. *
  46. * This field can be specified as a PHP callback of following signature:
  47. *
  48. * ```php
  49. * function (QueryInterface $query, mixed $db) {
  50. * //return mixed;
  51. * }
  52. * ```
  53. *
  54. * If not set - [[QueryInterface::one()]] will be used.
  55. */
  56. public $method;
  57. /**
  58. * Generates the data needed to determine if dependency is changed.
  59. * This method returns the query result
  60. * @param Cache $cache the cache component that is currently evaluating this dependency
  61. * @return mixed the data needed to determine if dependency has been changed.
  62. * @throws InvalidConfigException on invalid configuration.
  63. */
  64. protected function generateDependencyData($cache)
  65. {
  66. $db = $this->db;
  67. if ($db !== null) {
  68. $db = Instance::ensure($db);
  69. }
  70. if (!$this->query instanceof QueryInterface) {
  71. throw new InvalidConfigException('"' . get_class($this) . '::$query" should be an instance of "yii\db\QueryInterface".');
  72. }
  73. if (!empty($db->enableQueryCache)) {
  74. // temporarily disable and re-enable query caching
  75. $originEnableQueryCache = $db->enableQueryCache;
  76. $db->enableQueryCache = false;
  77. $result = $this->executeQuery($this->query, $db);
  78. $db->enableQueryCache = $originEnableQueryCache;
  79. } else {
  80. $result = $this->executeQuery($this->query, $db);
  81. }
  82. return $result;
  83. }
  84. /**
  85. * Executes the query according to [[method]] specification.
  86. * @param QueryInterface $query query to be executed.
  87. * @param mixed $db connection.
  88. * @return mixed query result.
  89. */
  90. private function executeQuery($query, $db)
  91. {
  92. if ($this->method === null) {
  93. return $query->one($db);
  94. }
  95. if (is_string($this->method)) {
  96. return call_user_func([$query, $this->method], $db);
  97. }
  98. return call_user_func($this->method, $query, $db);
  99. }
  100. }