title: How to load Yii 2 fixture?
body: <p>I use a simple fixture to populate the table in the test database. But when I try to load it, check integrity code throws exception 'Table not found: []'.</p>
  
  <p>My fixture:</p>
  
  <pre><code>&lt;?php
  
  namespace tests\unit\fixtures;
  
  use yii\test\ActiveFixture;
  
  class IdentityDocumentFixture extends ActiveFixture
  {
      public $modelClass = 'backend\modules\persons\models\IdentityDocument';
  }
  </code></pre>
  
  <p>To load fixture, I use this command from @app/tests: <code>yii fixture/load IdentityDocument</code></p>
  
  <pre><code>C:\php-projects\ais\tests\codeception\bin&gt;yii fixture/load IdentityDocument
  Fixtures namespace is:
          tests\unit\fixtures
  
  Global fixtures will be used:
  
          1. yii\test\InitDb
  
  Fixtures below will be loaded:
  
          1. IdentityDocument
  
  Load above fixtures? (yes|no) [no]:yes
  </code></pre>
  
  <p>As a result, I get this error message:</p>
  
  <blockquote>
    <p><strong>Exception 'yii\base\InvalidParamException' with message 'Table not found: []'</strong> in
    C:\php-projects\ais\vendor\yiisoft\yii2\db\mssql\QueryBuilder.php:180</p>
  </blockquote>
  
  <p>Stack trace:</p>
  
  <blockquote>
    <p>0 C:\php-projects\ais\vendor\yiisoft\yii2\db\Command.php(753):
    yii\db\mssql\QueryBuilder->checkIntegrity(false, '', '') 1
    C:\php-projects\ais\vendor\yiisoft\yii2\test\InitDbFixture.php(94):
    yii\db\Command->checkIntegrity(false, '') 2
    C:\php-projects\ais\vendor\yiisoft\yii2\test\InitDbFixture.php(76):
    yii\test\InitDbFixture->checkIntegrity(false) 3
    C:\php-projects\ais\vendor\yiisoft\yii2\test\FixtureTrait.php(114):
    yii\test\InitDbFixture->beforeUnload() 4
    C:\php-projects\ais\vendor\yiisoft\yii2\console\controllers\FixtureController.php(147):
    yii\console\controllers\FixtureController->unloadFixtures(Array) 5
    [internal function]:
    yii\console\controllers\FixtureController->actionLoad('IdentityDocumen...')
    6 C:\php-projects\ais\vendor\yiisoft\yii2\base\InlineAction.php(55):
    call_user_func_array(Array, Array) 7
    C:\php-projects\ais\vendor\yiisoft\yii2\base\Controller.php(154):
    yii\base\InlineAction->runWithParams(Array) 8
    C:\php-projects\ais\vendor\yiisoft\yii2\console\Controller.php(91):
    yii\base\Controller->runAction('load', Array) 9
    C:\php-projects\ais\vendor\yiisoft\yii2\base\Module.php(454):
    yii\console\Controller->runAction('load', Array) 10
    C:\php-projects\ais\vendor\yiisoft\yii2\console\Application.php(167):
    yii\base\Module->runAction('fixture/load', Array) 11
    C:\php-projects\ais\vendor\yiisoft\yii2\console\Application.php(143):
    yii\console\Application->runAction('fixture/load', Array) 12
    C:\php-projects\ais\vendor\yiisoft\yii2\base\Application.php(375):
    yii\console\Application->handleRequest(Object(yii\console\Request)) 13
    C:\php-projects\ais\tests\codeception\bin\yii(22):
    yii\base\Application->run() 14 {main}</p>
  </blockquote>
  
  <p>How to load fixture? The test database exists, the data sources are configured correctly.</p>
  
  <hr>
  
  <p>[Update]</p>
  
  <p>The real name of the table is <code>tbl_identitydocument</code>:</p>
  
  <pre><code>CREATE TABLE [dbo].[tbl_identitydocument](
      [ID] [int] IDENTITY(1,1) NOT NULL,
      [Name] [varchar](100) NOT NULL,
      [FISID] [int] NULL,
   CONSTRAINT [PK_tbl_identitydocument] PRIMARY KEY CLUSTERED 
  (
      [ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  ) ON [PRIMARY]
  </code></pre>
  
  <p>Here is the method <code>tableName()</code> of the model <code>IdentityDocument</code>:</p>
  
  <pre><code>public static function tableName()
  {
      return '{{%identitydocument}}';
  }
  </code></pre>
  
  <p>Model code completely:</p>
  
  <pre><code>&lt;?php
  
  namespace backend\modules\persons\models;
  
  use Yii;
  use backend\modules\persons\Module;
  
  /**
   * This is the model class for table "{{%identitydocument}}".
   *
   * @property integer $ID
   * @property string $Name
   * @property integer $FISID
   */
  class IdentityDocument extends \yii\db\ActiveRecord
  {
      /**
       * @inheritdoc
       */
      public static function tableName()
      {
          return '{{%identitydocument}}';
      }
  
      /**
       * @inheritdoc
       */
      public function rules()
      {
          return [
              [['Name'], 'required'],
              [['Name'], 'string'],
              [['FISID'], 'integer']
          ];
      }
  
      /**
       * @inheritdoc
       */
      public function attributeLabels()
      {
          return [
              'ID' =&gt; Module::t('ML', 'ID'),
              'Name' =&gt; Module::t('ML', 'Name'),
              'FISID' =&gt; Module::t('ML', 'FISID'),
          ];
      }
  }
  </code></pre>
  
  <p>Under the debugger I can see that method <code>IdentityDocument::tableName()</code> returns:</p>
  
  <pre><code>$tableNameTmp = IdentityDocument::tableName();
  </code></pre>
  
  <p>I see that <code>$tableNameTmp</code> is equal <code>{{%identitydocument}}</code> (<em>verbatim</em>, returns this string)</p>
  
  <hr>
  
  <p>[Update]</p>
  
  <p>Under the debugger I can see the following. In the class <code>TestCase.php</code> there is a method <code>setUp()</code>:</p>
  
  <pre><code>/**
   * @inheritdoc
   */
  protected function setUp()
  {
      parent::setUp();
      $this-&gt;mockApplication();
      $this-&gt;unloadFixtures();
      $this-&gt;loadFixtures();
  }
  </code></pre>
  
  <p>The exception is thrown here: <code>$this-&gt;unloadFixtures()</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/WMZxV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WMZxV.png" alt="enter image description here"></a></p>
  
  <p>At the screenshot it is visible that <code>initScript</code> referenced to the path <code>@app/tests/fixtures/initdb.php</code> <em>but there is no such file</em>.. Also the <code>schemas</code> array  contains a single element <code>""</code>.</p>
  
  <hr>
  
  <p>[Update]</p>
  
  <p>I used the instructions written by <a href="https://stackoverflow.com/users/479964/slinstj">@slinstj</a> here - <a href="https://github.com/yiisoft/yii2-app-advanced/issues/172" rel="nofollow noreferrer">Run <code>fixture/load User</code> fails to load User data</a>. In my local project (advanced pattern and MySQL rdbms) the fixture is loaded successfully:</p>
  
  <p><a href="https://i.stack.imgur.com/GjigW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GjigW.png" alt="enter image description here"></a></p>