Act*_*ele 2 php phpunit unit-testing yii
在每次测试开始时截断数据库的最佳解决方案是什么?我有带有Foreigh键的InnoDB引擎,无法截断表。
If you extend CDbTestCase and use the built in public $fixtures property to specify your fixture files, it will handle this automatically.
However, if you built your own fixture generating system, or otherwise want to truncate a table, you can use the following.
$this->getFixtureManager()->checkIntegrity(false);
$this->getFixtureManager()->truncateTable('table_name');
$this->getFixtureManager()->checkIntegrity(false);
Run Code Online (Sandbox Code Playgroud)
This again assumes you are extends CDbTestCase for your unit test files. If you are not, then you can directly remove the integrity checks like so:
Yii::app()->db->createCommand('set foreign_key_checks=0')->execute();
//do whatever, including truncating
Yii::app()->db->createCommand('set foreign_key_checks=1')->execute();
Run Code Online (Sandbox Code Playgroud)
That will also temporarily disable the foreign key checks.
只是为Yii2添加一个参考 (因为没有容易找到的例子):
$this->db->createCommand()->checkIntegrity(false)->execute();
$this->truncateTable('table_name');
$this->db->createCommand()->checkIntegrity(true)->execute();
Run Code Online (Sandbox Code Playgroud)
Yii 可以截断:
Yii::app()->db->createCommand('TRUNCATE tbl_name')->execute();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3103 次 |
| 最近记录: |