ViewFinderTrait.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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\db;
  8. /**
  9. * ViewFinderTrait implements the method getViewNames for finding views in a database.
  10. *
  11. * @author Qiang Xue <qiang.xue@gmail.com>
  12. * @author Bob Olde Hampsink <b.oldehampsink@nerds.company>
  13. * @since 2.0.12
  14. */
  15. trait ViewFinderTrait
  16. {
  17. /**
  18. * @var array list of ALL view names in the database
  19. */
  20. private $_viewNames = [];
  21. /**
  22. * Returns all views names in the database.
  23. * @param string $schema the schema of the views. Defaults to empty string, meaning the current or default schema.
  24. * @return array all views names in the database. The names have NO schema name prefix.
  25. */
  26. abstract protected function findViewNames($schema = '');
  27. /**
  28. * Returns all view names in the database.
  29. * @param string $schema the schema of the views. Defaults to empty string, meaning the current or default schema name.
  30. * If not empty, the returned view names will be prefixed with the schema name.
  31. * @param bool $refresh whether to fetch the latest available view names. If this is false,
  32. * view names fetched previously (if available) will be returned.
  33. * @return string[] all view names in the database.
  34. */
  35. public function getViewNames($schema = '', $refresh = false)
  36. {
  37. if (!isset($this->_viewNames[$schema]) || $refresh) {
  38. $this->_viewNames[$schema] = $this->findViewNames($schema);
  39. }
  40. return $this->_viewNames[$schema];
  41. }
  42. }