Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

154 Zeilen
5.5KB

  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\TableSchema;
  11. /**
  12. * ActiveFixture represents a fixture backed up by a [[modelClass|ActiveRecord class]] or a [[tableName|database table]].
  13. *
  14. * Either [[modelClass]] or [[tableName]] must be set. You should also provide fixture data in the file
  15. * specified by [[dataFile]] or overriding [[getData()]] if you want to use code to generate the fixture data.
  16. *
  17. * When the fixture is being loaded, it will first call [[resetTable()]] to remove any existing data in the table.
  18. * It will then populate the table with the data returned by [[getData()]].
  19. *
  20. * After the fixture is loaded, you can access the loaded data via the [[data]] property. If you set [[modelClass]],
  21. * you will also be able to retrieve an instance of [[modelClass]] with the populated data via [[getModel()]].
  22. *
  23. * @property TableSchema $tableSchema The schema information of the database table associated with this
  24. * fixture. This property is read-only.
  25. *
  26. * @author Qiang Xue <qiang.xue@gmail.com>
  27. * @since 2.0
  28. */
  29. class ActiveFixture extends BaseActiveFixture
  30. {
  31. /**
  32. * @var string the name of the database table that this fixture is about. If this property is not set,
  33. * the table name will be determined via [[modelClass]].
  34. * @see modelClass
  35. */
  36. public $tableName;
  37. /**
  38. * @var string|boolean the file path or path alias of the data file that contains the fixture data
  39. * to be returned by [[getData()]]. If this is not set, it will default to `FixturePath/data/TableName.php`,
  40. * where `FixturePath` stands for the directory containing this fixture class, and `TableName` stands for the
  41. * name of the table associated with this fixture. You can set this property to be false to prevent loading any data.
  42. */
  43. public $dataFile;
  44. /**
  45. * @var TableSchema the table schema for the table associated with this fixture
  46. */
  47. private $_table;
  48. /**
  49. * @inheritdoc
  50. */
  51. public function init()
  52. {
  53. parent::init();
  54. if (!isset($this->modelClass) && !isset($this->tableName)) {
  55. throw new InvalidConfigException('Either "modelClass" or "tableName" must be set.');
  56. }
  57. }
  58. /**
  59. * Loads the fixture.
  60. *
  61. * The default implementation will first clean up the table by calling [[resetTable()]].
  62. * It will then populate the table with the data returned by [[getData()]].
  63. *
  64. * If you override this method, you should consider calling the parent implementation
  65. * so that the data returned by [[getData()]] can be populated into the table.
  66. */
  67. public function load()
  68. {
  69. $this->resetTable();
  70. $this->data = [];
  71. $table = $this->getTableSchema();
  72. foreach ($this->getData() as $alias => $row) {
  73. $this->db->createCommand()->insert($table->fullName, $row)->execute();
  74. if ($table->sequenceName !== null) {
  75. foreach ($table->primaryKey as $pk) {
  76. if (!isset($row[$pk])) {
  77. $row[$pk] = $this->db->getLastInsertID($table->sequenceName);
  78. break;
  79. }
  80. }
  81. }
  82. $this->data[$alias] = $row;
  83. }
  84. }
  85. /**
  86. * Returns the fixture data.
  87. *
  88. * The default implementation will try to return the fixture data by including the external file specified by [[dataFile]].
  89. * The file should return an array of data rows (column name => column value), each corresponding to a row in the table.
  90. *
  91. * If the data file does not exist, an empty array will be returned.
  92. *
  93. * @return array the data rows to be inserted into the database table.
  94. */
  95. protected function getData()
  96. {
  97. if ($this->dataFile === null) {
  98. $class = new \ReflectionClass($this);
  99. $dataFile = dirname($class->getFileName()) . '/data/' . $this->getTableSchema()->fullName . '.php';
  100. return is_file($dataFile) ? require($dataFile) : [];
  101. } else {
  102. return parent::getData();
  103. }
  104. }
  105. /**
  106. * Removes all existing data from the specified table and resets sequence number to 1 (if any).
  107. * This method is called before populating fixture data into the table associated with this fixture.
  108. */
  109. protected function resetTable()
  110. {
  111. $table = $this->getTableSchema();
  112. $this->db->createCommand()->delete($table->fullName)->execute();
  113. if ($table->sequenceName !== null) {
  114. $this->db->createCommand()->resetSequence($table->fullName, 1)->execute();
  115. }
  116. }
  117. /**
  118. * @return TableSchema the schema information of the database table associated with this fixture.
  119. * @throws \yii\base\InvalidConfigException if the table does not exist
  120. */
  121. public function getTableSchema()
  122. {
  123. if ($this->_table !== null) {
  124. return $this->_table;
  125. }
  126. $db = $this->db;
  127. $tableName = $this->tableName;
  128. if ($tableName === null) {
  129. /* @var $modelClass \yii\db\ActiveRecord */
  130. $modelClass = $this->modelClass;
  131. $tableName = $modelClass::tableName();
  132. }
  133. $this->_table = $db->getSchema()->getTableSchema($tableName);
  134. if ($this->_table === null) {
  135. throw new InvalidConfigException("Table does not exist: {$tableName}");
  136. }
  137. return $this->_table;
  138. }
  139. }