yii2

Форк
1
/
BaseActiveFixture.php 
103 строки · 2.9 Кб
1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7

8
namespace yii\test;
9

10
use yii\base\ArrayAccessTrait;
11
use yii\base\InvalidConfigException;
12

13
/**
14
 * BaseActiveFixture is the base class for fixture classes that support accessing fixture data as ActiveRecord objects.
15
 *
16
 * For more details and usage information on BaseActiveFixture, see the [guide article on fixtures](guide:test-fixtures).
17
 *
18
 * @author Qiang Xue <qiang.xue@gmail.com>
19
 * @since 2.0
20
 */
21
abstract class BaseActiveFixture extends DbFixture implements \IteratorAggregate, \ArrayAccess, \Countable
22
{
23
    use ArrayAccessTrait;
24
    use FileFixtureTrait;
25

26
    /**
27
     * @var string the AR model class associated with this fixture.
28
     */
29
    public $modelClass;
30
    /**
31
     * @var array the data rows. Each array element represents one row of data (column name => column value).
32
     */
33
    public $data = [];
34

35
    /**
36
     * @var \yii\db\ActiveRecord[] the loaded AR models
37
     */
38
    private $_models = [];
39

40

41
    /**
42
     * Returns the AR model by the specified model name.
43
     * A model name is the key of the corresponding data row in [[data]].
44
     * @param string $name the model name.
45
     * @return \yii\db\ActiveRecord|null the AR model, or null if the model cannot be found in the database
46
     * @throws \yii\base\InvalidConfigException if [[modelClass]] is not set.
47
     */
48
    public function getModel($name)
49
    {
50
        if (!isset($this->data[$name])) {
51
            return null;
52
        }
53
        if (array_key_exists($name, $this->_models)) {
54
            return $this->_models[$name];
55
        }
56

57
        if ($this->modelClass === null) {
58
            throw new InvalidConfigException('The "modelClass" property must be set.');
59
        }
60
        $row = $this->data[$name];
61
        /* @var $modelClass \yii\db\ActiveRecord */
62
        $modelClass = $this->modelClass;
63
        $keys = [];
64
        foreach ($modelClass::primaryKey() as $key) {
65
            $keys[$key] = isset($row[$key]) ? $row[$key] : null;
66
        }
67

68
        return $this->_models[$name] = $modelClass::findOne($keys);
69
    }
70

71
    /**
72
     * Loads the fixture.
73
     *
74
     * The default implementation simply stores the data returned by [[getData()]] in [[data]].
75
     * You should usually override this method by putting the data into the underlying database.
76
     */
77
    public function load()
78
    {
79
        $this->data = $this->getData();
80
    }
81

82
    /**
83
     * Returns the fixture data.
84
     *
85
     * @return array the data to be put into the database
86
     * @throws InvalidConfigException if the specified data file does not exist.
87
     * @see loadData()
88
     */
89
    protected function getData()
90
    {
91
        return $this->loadData($this->dataFile);
92
    }
93

94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function unload()
98
    {
99
        parent::unload();
100
        $this->data = [];
101
        $this->_models = [];
102
    }
103
}
104

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.