如何加载 Yii 2 fixture?
How to load Yii 2 fixture?
我使用一个简单的装置来填充测试数据库中的 table。但是当我尝试加载它时,检查完整性代码抛出异常 'Table not found: []'.
我的固定装置:
<?php
namespace tests\unit\fixtures;
use yii\test\ActiveFixture;
class IdentityDocumentFixture extends ActiveFixture
{
public $modelClass = 'backend\modules\persons\models\IdentityDocument';
}
为了加载夹具,我使用来自@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
因此,我收到此错误消息:
Exception 'yii\base\InvalidParamException' with message 'Table not found: []' in
C:\php-projects\ais\vendor\yiisoft\yii2\db\mssql\QueryBuilder.php:180
堆栈跟踪:
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}
如何加载fixture?测试数据库存在,数据源配置正确
[更新]
table的真名是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]
这里是模型IdentityDocument
的方法tableName()
:
public static function tableName()
{
return '{{%identitydocument}}';
}
模型代码完整:
<?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'),
];
}
}
在调试器下我可以看到那个方法IdentityDocument::tableName()
returns:
$tableNameTmp = IdentityDocument::tableName();
我看到$tableNameTmp
等于{{%identitydocument}}
(逐字,returns这个字符串)
[更新]
在调试器下我可以看到以下内容。在classTestCase.php
中有一个方法setUp()
:
/**
* @inheritdoc
*/
protected function setUp()
{
parent::setUp();
$this->mockApplication();
$this->unloadFixtures();
$this->loadFixtures();
}
这里抛出异常:$this->unloadFixtures()
:
在屏幕截图中可以看到 initScript
引用了路径 @app/tests/fixtures/initdb.php
但是没有这样的文件.. 还有 schemas
数组包含单个元素 ""
.
[更新]
我使用了@slinstj here - Run fixture/load User
fails to load User data写的指令。在我的本地项目(高级模式和 MySQL rdbms)中,夹具已成功加载:
1 - 检查您是否为 yii\db\Connection
:
设置了 tablePrefix
参数
'components' => [
'db' => [
'class' => '\yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'tbl_', // <<<<<<<<
],
],
来自 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}}.
或
2 - 设置 dataFile
路径:
根据this:
// In your IdentityDocumentFixture, set this:
public $dataFile = __DIR__ . '/path/to/your/data-file.php';
我使用一个简单的装置来填充测试数据库中的 table。但是当我尝试加载它时,检查完整性代码抛出异常 'Table not found: []'.
我的固定装置:
<?php
namespace tests\unit\fixtures;
use yii\test\ActiveFixture;
class IdentityDocumentFixture extends ActiveFixture
{
public $modelClass = 'backend\modules\persons\models\IdentityDocument';
}
为了加载夹具,我使用来自@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
因此,我收到此错误消息:
Exception 'yii\base\InvalidParamException' with message 'Table not found: []' in C:\php-projects\ais\vendor\yiisoft\yii2\db\mssql\QueryBuilder.php:180
堆栈跟踪:
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}
如何加载fixture?测试数据库存在,数据源配置正确
[更新]
table的真名是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]
这里是模型IdentityDocument
的方法tableName()
:
public static function tableName()
{
return '{{%identitydocument}}';
}
模型代码完整:
<?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'),
];
}
}
在调试器下我可以看到那个方法IdentityDocument::tableName()
returns:
$tableNameTmp = IdentityDocument::tableName();
我看到$tableNameTmp
等于{{%identitydocument}}
(逐字,returns这个字符串)
[更新]
在调试器下我可以看到以下内容。在classTestCase.php
中有一个方法setUp()
:
/**
* @inheritdoc
*/
protected function setUp()
{
parent::setUp();
$this->mockApplication();
$this->unloadFixtures();
$this->loadFixtures();
}
这里抛出异常:$this->unloadFixtures()
:
在屏幕截图中可以看到 initScript
引用了路径 @app/tests/fixtures/initdb.php
但是没有这样的文件.. 还有 schemas
数组包含单个元素 ""
.
[更新]
我使用了@slinstj here - Run fixture/load User
fails to load User data写的指令。在我的本地项目(高级模式和 MySQL rdbms)中,夹具已成功加载:
1 - 检查您是否为 yii\db\Connection
:
设置了 tablePrefix
参数
'components' => [
'db' => [
'class' => '\yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'tbl_', // <<<<<<<<
],
],
来自 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}}.
或
2 - 设置 dataFile
路径:
根据this:
// In your IdentityDocumentFixture, set this:
public $dataFile = __DIR__ . '/path/to/your/data-file.php';