DbQueryDependency.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. *
  60. * This method returns the query result.
  61. * @param CacheInterface $cache the cache component that is currently evaluating this dependency
  62. * @return mixed the data needed to determine if dependency has been changed.
  63. * @throws InvalidConfigException on invalid configuration.
  64. */
  65. protected function generateDependencyData($cache)
  66. {
  67. $db = $this->db;
  68. if ($db !== null) {
  69. $db = Instance::ensure($db);
  70. }
  71. if (!$this->query instanceof QueryInterface) {
  72. throw new InvalidConfigException('"' . get_class($this) . '::$query" should be an instance of "yii\db\QueryInterface".');
  73. }
  74. if (!empty($db->enableQueryCache)) {
  75. // temporarily disable and re-enable query caching
  76. $originEnableQueryCache = $db->enableQueryCache;
  77. $db->enableQueryCache = false;
  78. $result = $this->executeQuery($this->query, $db);
  79. $db->enableQueryCache = $originEnableQueryCache;
  80. } else {
  81. $result = $this->executeQuery($this->query, $db);
  82. }
  83. return $result;
  84. }
  85. /**
  86. * Executes the query according to [[method]] specification.
  87. * @param QueryInterface $query query to be executed.
  88. * @param mixed $db connection.
  89. * @return mixed query result.
  90. */
  91. private function executeQuery($query, $db)
  92. {
  93. if ($this->method === null) {
  94. return $query->one($db);
  95. }
  96. if (is_string($this->method)) {
  97. return call_user_func([$query, $this->method], $db);
  98. }
  99. return call_user_func($this->method, $query, $db);
  100. }
  101. }