user1134181
user1134181

Reputation:

How to load Yii 2 fixture?

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: []'.

My fixture:

<?php

namespace tests\unit\fixtures;

use yii\test\ActiveFixture;

class IdentityDocumentFixture extends ActiveFixture
{
    public $modelClass = 'backend\modules\persons\models\IdentityDocument';
}

To load fixture, I use this command from @app/tests: yii fixture/load IdentityDocument

C:\php-projects\ais\tests\codeception\bin>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

As a result, I get this error message:

Exception 'yii\base\InvalidParamException' with message 'Table not found: []' in C:\php-projects\ais\vendor\yiisoft\yii2\db\mssql\QueryBuilder.php:180

Stack trace:

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}

How to load fixture? The test database exists, the data sources are configured correctly.


[Update]

The real name of the table is tbl_identitydocument:

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]

Here is the method tableName() of the model IdentityDocument:

public static function tableName()
{
    return '{{%identitydocument}}';
}

Model code completely:

<?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' => Module::t('ML', 'ID'),
            'Name' => Module::t('ML', 'Name'),
            'FISID' => Module::t('ML', 'FISID'),
        ];
    }
}

Under the debugger I can see that method IdentityDocument::tableName() returns:

$tableNameTmp = IdentityDocument::tableName();

I see that $tableNameTmp is equal {{%identitydocument}} (verbatim, returns this string)


[Update]

Under the debugger I can see the following. In the class TestCase.php there is a method setUp():

/**
 * @inheritdoc
 */
protected function setUp()
{
    parent::setUp();
    $this->mockApplication();
    $this->unloadFixtures();
    $this->loadFixtures();
}

The exception is thrown here: $this->unloadFixtures():

enter image description here

At the screenshot it is visible that initScript referenced to the path @app/tests/fixtures/initdb.php but there is no such file.. Also the schemas array contains a single element "".


[Update]

I used the instructions written by @slinstj here - Run fixture/load User fails to load User data. In my local project (advanced pattern and MySQL rdbms) the fixture is loaded successfully:

enter image description here

Upvotes: 2

Views: 4770

Answers (1)

sdlins
sdlins

Reputation: 2295

1 - Check if you have set the tablePrefix param for yii\db\Connection:

'components' => [
    'db' => [
        'class' => '\yii\db\Connection',
        'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',
        'tablePrefix' => 'tbl_', // <<<<<<<<
    ],
],

Further infomation from docs:

$tablePrefix:

The common prefix or suffix for table names. If a table name is given as {{%TableName}}, then the percentage character % will be replaced with this property value. For example, {{%post}} becomes {{tbl_post}}.

OR

2 - Set a dataFile path:

According to this:

// In your IdentityDocumentFixture, set this:
public $dataFile = __DIR__ . '/path/to/your/data-file.php';

Upvotes: 1

Related Questions