BaseActiveFixture.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\base\ArrayAccessTrait;
  9. use yii\base\InvalidConfigException;
  10. /**
  11. * BaseActiveFixture is the base class for fixture classes that support accessing fixture data as ActiveRecord objects.
  12. *
  13. * For more details and usage information on BaseActiveFixture, see the [guide article on fixtures](guide:test-fixtures).
  14. *
  15. * @author Qiang Xue <qiang.xue@gmail.com>
  16. * @since 2.0
  17. */
  18. abstract class BaseActiveFixture extends DbFixture implements \IteratorAggregate, \ArrayAccess, \Countable
  19. {
  20. use ArrayAccessTrait;
  21. use FileFixtureTrait;
  22. /**
  23. * @var string the AR model class associated with this fixture.
  24. */
  25. public $modelClass;
  26. /**
  27. * @var array the data rows. Each array element represents one row of data (column name => column value).
  28. */
  29. public $data = [];
  30. /**
  31. * @var \yii\db\ActiveRecord[] the loaded AR models
  32. */
  33. private $_models = [];
  34. /**
  35. * Returns the AR model by the specified model name.
  36. * A model name is the key of the corresponding data row in [[data]].
  37. * @param string $name the model name.
  38. * @return null|\yii\db\ActiveRecord the AR model, or null if the model cannot be found in the database
  39. * @throws \yii\base\InvalidConfigException if [[modelClass]] is not set.
  40. */
  41. public function getModel($name)
  42. {
  43. if (!isset($this->data[$name])) {
  44. return null;
  45. }
  46. if (array_key_exists($name, $this->_models)) {
  47. return $this->_models[$name];
  48. }
  49. if ($this->modelClass === null) {
  50. throw new InvalidConfigException('The "modelClass" property must be set.');
  51. }
  52. $row = $this->data[$name];
  53. /* @var $modelClass \yii\db\ActiveRecord */
  54. $modelClass = $this->modelClass;
  55. $keys = [];
  56. foreach ($modelClass::primaryKey() as $key) {
  57. $keys[$key] = isset($row[$key]) ? $row[$key] : null;
  58. }
  59. return $this->_models[$name] = $modelClass::findOne($keys);
  60. }
  61. /**
  62. * Loads the fixture.
  63. *
  64. * The default implementation simply stores the data returned by [[getData()]] in [[data]].
  65. * You should usually override this method by putting the data into the underlying database.
  66. */
  67. public function load()
  68. {
  69. $this->data = $this->getData();
  70. }
  71. /**
  72. * Returns the fixture data.
  73. *
  74. * @return array the data to be put into the database
  75. * @throws InvalidConfigException if the specified data file does not exist.
  76. * @see [[loadData]]
  77. */
  78. protected function getData()
  79. {
  80. return $this->loadData($this->dataFile);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function unload()
  86. {
  87. parent::unload();
  88. $this->data = [];
  89. $this->_models = [];
  90. }
  91. }