DbFixture.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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\test;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. use yii\db\Connection;
  11. /**
  12. * DbFixture is the base class for DB-related fixtures.
  13. *
  14. * DbFixture provides the [[db]] connection to be used by DB fixtures.
  15. *
  16. * @author Qiang Xue <[email protected]>
  17. * @since 2.0
  18. */
  19. abstract class DbFixture extends Fixture
  20. {
  21. /**
  22. * @var Connection|string the DB connection object or the application component ID of the DB connection.
  23. * After the DbFixture object is created, if you want to change this property, you should only assign it
  24. * with a DB connection object.
  25. */
  26. public $db = 'db';
  27. /**
  28. * @inheritdoc
  29. */
  30. public function init()
  31. {
  32. parent::init();
  33. if (is_string($this->db)) {
  34. $this->db = Yii::$app->getComponent($this->db);
  35. }
  36. if (!is_object($this->db)) {
  37. throw new InvalidConfigException("The 'db' property must be either a DB connection instance or the application component ID of a DB connection.");
  38. }
  39. }
  40. }