# ActiveFixture

> ActiveFixture represents a fixture backed up by a ActiveRecord class ($modelClass) or a database table ($tableName).

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [test](/docs/api/yii/test/index.md) › ActiveFixture

| class | `yii\test\ActiveFixture` |
| --- | --- |
| Extends | [`BaseActiveFixture`](/docs/api/yii/test/baseactivefixture/index.md) » [`DbFixture`](/docs/api/yii/test/dbfixture/index.md) » [`Fixture`](/docs/api/yii/test/fixture/index.md) » [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md), [`IteratorAggregate`](https://www.php.net/manual/en/class.iteratoraggregate.php), [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php), [`Countable`](https://www.php.net/manual/en/class.countable.php) |
| Uses traits | [`yii\base\ArrayAccessTrait`](/docs/api/yii/base/arrayaccesstrait/index.md), [`FileFixtureTrait`](/docs/api/yii/test/filefixturetrait/index.md) |
| Since | 2.0 |
| Source | [`framework/test/ActiveFixture.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/test/ActiveFixture.php#L35) |

ActiveFixture represents a fixture backed up by a [ActiveRecord class](/docs/api/yii/test/baseactivefixture/index.md#property-modelClass) or a [database table](/docs/api/yii/test/activefixture/index.md#property-tableName).

Either [`$modelClass`](/docs/api/yii/test/baseactivefixture/index.md#property-modelClass) or [`$tableName`](/docs/api/yii/test/activefixture/index.md#property-tableName) must be set. You should also provide fixture data in the file
specified by [`$dataFile`](/docs/api/yii/test/activefixture/index.md#property-dataFile) or overriding [`getData()`](/docs/api/yii/test/activefixture/index.md#method-getData) if you want to use code to generate the fixture data.

When the fixture is being loaded, it will first call [`resetTable()`](/docs/api/yii/test/activefixture/index.md#method-resetTable) to remove any existing data in the table.
It will then populate the table with the data returned by [`getData()`](/docs/api/yii/test/activefixture/index.md#method-getData).

After the fixture is loaded, you can access the loaded data via the [`$data`](/docs/api/yii/test/baseactivefixture/index.md#property-data) property. If you set [`$modelClass`](/docs/api/yii/test/baseactivefixture/index.md#property-modelClass),
you will also be able to retrieve an instance of [`$modelClass`](/docs/api/yii/test/baseactivefixture/index.md#property-modelClass) with the populated data via [`getModel()`](/docs/api/yii/test/baseactivefixture/index.md#method-getModel).

For more details and usage information on ActiveFixture, see the [guide article on fixtures](/docs/guide/test-fixtures/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="property-data"></span>[`$data`](/docs/api/yii/test/baseactivefixture/index.md#property-data) | `array<string, array<string, mixed>>` | The data rows. Default: `[]`. | [`BaseActiveFixture`](/docs/api/yii/test/baseactivefixture/index.md) |
| <span id="property-dataDirectory"></span>[`$dataDirectory`](/docs/api/yii/test/filefixturetrait/index.md#property-dataDirectory) | `string` | The directory path or [path alias](/docs/guide/concept-aliases/index.md) that contains the fixture data Default: `null`. | [`FileFixtureTrait`](/docs/api/yii/test/filefixturetrait/index.md) |
| [`$dataFile`](#property-dataFile) | `string\|bool\|null` | The file path or [path alias](/docs/guide/concept-aliases/index.md) of the data file that contains the fixture data to be returned by [`getData()`](/docs/api/yii/test/activefixture/index.md#method-getData). Default: `null`. | `ActiveFixture` |
| <span id="property-db"></span>[`$db`](/docs/api/yii/test/dbfixture/index.md#property-db) | [`yii\db\Connection`](/docs/api/yii/db/connection/index.md)`\|array\|string` | The DB connection object or the application component ID of the DB connection. Default: `'db'`. | [`DbFixture`](/docs/api/yii/test/dbfixture/index.md) |
| <span id="property-depends"></span>[`$depends`](/docs/api/yii/test/fixture/index.md#property-depends) | `array` | The fixtures that this fixture depends on. Default: `[]`. | [`Fixture`](/docs/api/yii/test/fixture/index.md) |
| <span id="property-iterator"></span>`$iterator` read-only | [`ArrayIterator`](https://www.php.net/manual/en/class.arrayiterator.php)`<array-key, mixed>` | An iterator for traversing the cookies in the collection. | [`yii\base\ArrayAccessTrait`](/docs/api/yii/base/arrayaccesstrait/index.md) |
| <span id="property-modelClass"></span>[`$modelClass`](/docs/api/yii/test/baseactivefixture/index.md#property-modelClass) | `string` | The AR model class associated with this fixture. Default: `null`. | [`BaseActiveFixture`](/docs/api/yii/test/baseactivefixture/index.md) |
| [`$tableName`](#property-tableName) | `string\|null` | The name of the database table that this fixture is about. Default: `null`. | `ActiveFixture` |
| <span id="property-tableSchema"></span>`$tableSchema` read-only | [`yii\db\TableSchema`](/docs/api/yii/db/tableschema/index.md) | The schema information of the database table associated with this fixture. | `ActiveFixture` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-afterLoad"></span>[`afterLoad()`](/docs/api/yii/test/fixture/index.md#method-afterLoad) | This method is called AFTER all fixture data have been loaded for the current test. | [`Fixture`](/docs/api/yii/test/fixture/index.md) |
| <span id="method-afterUnload"></span>[`afterUnload()`](/docs/api/yii/test/fixture/index.md#method-afterUnload) | This method is called AFTER all fixture data have been unloaded for the current test. | [`Fixture`](/docs/api/yii/test/fixture/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-beforeLoad"></span>[`beforeLoad()`](/docs/api/yii/test/fixture/index.md#method-beforeLoad) | This method is called BEFORE any fixture data is loaded for the current test. | [`Fixture`](/docs/api/yii/test/fixture/index.md) |
| <span id="method-beforeUnload"></span>[`beforeUnload()`](/docs/api/yii/test/fixture/index.md#method-beforeUnload) | This method is called BEFORE any fixture data is unloaded for the current test. | [`Fixture`](/docs/api/yii/test/fixture/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-count"></span>[`count()`](/docs/api/yii/base/arrayaccesstrait/index.md#method-count) | Returns the number of data items. | [`yii\base\ArrayAccessTrait`](/docs/api/yii/base/arrayaccesstrait/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getIterator"></span>[`getIterator()`](/docs/api/yii/base/arrayaccesstrait/index.md#method-getIterator) | Returns an iterator for traversing the data. | [`yii\base\ArrayAccessTrait`](/docs/api/yii/base/arrayaccesstrait/index.md) |
| <span id="method-getModel"></span>[`getModel()`](/docs/api/yii/test/baseactivefixture/index.md#method-getModel) | Returns the AR model by the specified model name. | [`BaseActiveFixture`](/docs/api/yii/test/baseactivefixture/index.md) |
| [`getTableSchema()`](#method-getTableSchema) |  | `ActiveFixture` |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`init()`](#method-init) | Initializes the object. | `ActiveFixture` |
| [`load()`](#method-load) | Loads the fixture. | `ActiveFixture` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-offsetExists"></span>[`offsetExists()`](/docs/api/yii/base/arrayaccesstrait/index.md#method-offsetExists) | This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php). | [`yii\base\ArrayAccessTrait`](/docs/api/yii/base/arrayaccesstrait/index.md) |
| <span id="method-offsetGet"></span>[`offsetGet()`](/docs/api/yii/base/arrayaccesstrait/index.md#method-offsetGet) | This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php). | [`yii\base\ArrayAccessTrait`](/docs/api/yii/base/arrayaccesstrait/index.md) |
| <span id="method-offsetSet"></span>[`offsetSet()`](/docs/api/yii/base/arrayaccesstrait/index.md#method-offsetSet) | This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php). | [`yii\base\ArrayAccessTrait`](/docs/api/yii/base/arrayaccesstrait/index.md) |
| <span id="method-offsetUnset"></span>[`offsetUnset()`](/docs/api/yii/base/arrayaccesstrait/index.md#method-offsetUnset) | This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php). | [`yii\base\ArrayAccessTrait`](/docs/api/yii/base/arrayaccesstrait/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`unload()`](#method-unload) | Unloads the fixture. | `ActiveFixture` |
| [`getData()`](#method-getData) protected | Returns the fixture data. | `ActiveFixture` |
| <span id="method-loadData"></span>[`loadData()`](/docs/api/yii/test/filefixturetrait/index.md#method-loadData) protected | Returns the fixture data. | [`FileFixtureTrait`](/docs/api/yii/test/filefixturetrait/index.md) |
| [`resetTable()`](#method-resetTable) protected | Removes all existing data from the specified table and resets sequence number to 1 (if any). | `ActiveFixture` |

## Property details

### $dataFile <span id="property-dataFile"></span>

```php
public string|bool|null $dataFile = null
```

The file path or [path alias](/docs/guide/concept-aliases/index.md) of the data file that contains the fixture data to be returned by [`getData()`](/docs/api/yii/test/activefixture/index.md#method-getData).

If this is not set, it will default to `FixturePath/data/TableName.php`,
where `FixturePath` stands for the directory containing this fixture class, and `TableName` stands for the
name of the table associated with this fixture. You can set this property to be false to prevent loading any data.

### $tableName <span id="property-tableName"></span>

```php
public string|null $tableName = null
```

The name of the database table that this fixture is about.

If this property is not set,
the table name will be determined via [`$modelClass`](/docs/api/yii/test/baseactivefixture/index.md#property-modelClass).

## Method details

### getTableSchema() <span id="method-getTableSchema"></span>

```php
public function getTableSchema(): yii\db\TableSchema
```

**Returns**

[`yii\db\TableSchema`](/docs/api/yii/db/tableschema/index.md): The schema information of the database table associated with this fixture.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the table does not exist

### init() <span id="method-init"></span>

```php
public function init(): mixed
```

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the
given configuration.

**Returns**

`mixed`

### load() <span id="method-load"></span>

```php
public function load(): mixed
```

Loads the fixture.

It populate the table with the data returned by [`getData()`](/docs/api/yii/test/activefixture/index.md#method-getData).

If you override this method, you should consider calling the parent implementation
so that the data returned by [`getData()`](/docs/api/yii/test/activefixture/index.md#method-getData) can be populated into the table.

**Returns**

`mixed`

### unload() <span id="method-unload"></span>

```php
public function unload(): mixed
```

Unloads the fixture.

This method is called after every test method finishes.
You may override this method to perform necessary cleanup work for the fixture.

**Returns**

`mixed`

### getData() <span id="method-getData"></span>

```php
protected function getData(): array
```

Returns the fixture data.

The default implementation will try to return the fixture data by including the external file specified by [`$dataFile`](/docs/api/yii/test/activefixture/index.md#property-dataFile).
The file should return an array of data rows (column name => column value), each corresponding to a row in the table.

If the data file does not exist, an empty array will be returned.

**Returns**

`array`: The data rows to be inserted into the database table.

### resetTable() <span id="method-resetTable"></span>

```php
protected function resetTable(): mixed
```

Removes all existing data from the specified table and resets sequence number to 1 (if any).

This method is called before populating fixture data into the table associated with this fixture.

**Returns**

`mixed`