如何在yii2中创建数据库种子?

如何在yii2中创建数据库种子?,yii,yii2,yii-extensions,seeding,faker,Yii,Yii2,Yii Extensions,Seeding,Faker,我是Yii框架的新手。 我想种子我的数据库,就像它可以在拉威尔框架使用伪造。 我试过了,但没有提供太多细节。 如果有人能详细帮助我,我将不胜感激。请参阅yii2应用程序高级测试中的at和faker实现。在项目中,您还可以在控制台php-yii-fixture/load中编写代码,以在数据库中加载种子,并php-yii-fixture/generate-all以通过faker生成种子 yii.php应该在controllerMap数组中具有正确的fixture控制器: [ 'control

我是Yii框架的新手。 我想种子我的数据库,就像它可以在拉威尔框架使用伪造。 我试过了,但没有提供太多细节。 如果有人能详细帮助我,我将不胜感激。

请参阅yii2应用程序高级测试中的at和faker实现。在项目中,您还可以在控制台
php-yii-fixture/load
中编写代码,以在数据库中加载种子,并
php-yii-fixture/generate-all
以通过faker生成种子

yii.php
应该在
controllerMap
数组中具有正确的
fixture
控制器:

[
    'controllerMap' => [
        'fixture' => [
            'class' => 'yii\console\controllers\FixtureController',
            'namespace' => 'common\ActiveRecords'
        ]
    ]
]

下面是我在commands文件夹下创建的
SeedController.php
文件:

// commands/SeedController.php
namespace app\commands;

use yii\console\Controller;
use app\models\Users;
use app\models\Profile;

class SeedController extends Controller
{
    public function actionIndex()
    {
        $faker = \Faker\Factory::create();

        $user = new Users();
        $profile = new Profile();
        for ( $i = 1; $i <= 20; $i++ )
        {
            $user->setIsNewRecord(true);
            $user->user_id = null;

            $user->username = $faker->username;
            $user->password = '123456';
            if ( $user->save() )
            {
                $profile->setIsNewRecord(true);
                $profile->user_id = null;

                $profile->user_id = $user->user_id;
                $profile->email = $faker->email;
                $profile->first_name = $faker->firstName;
                $profile->last_name = $faker->lastName;
                $profile->save();
            }
        }

    }
}
//commands/SeedController.php
名称空间app\命令;
使用yii\console\Controller;
使用app\models\Users;
使用app\models\Profile;
类SeedController扩展控制器
{
公共职能行动索引()
{
$faker=\faker\Factory::create();
$user=新用户();
$profile=新配置文件();
对于($i=1;$i setIsNewRecord(true);
$user->user\u id=null;
$user->username=$faker->username;
$user->password='123456';
如果($user->save())
{
$profile->setIsNewRecord(true);
$profile->user\u id=null;
$profile->user\u id=$user->user\u id;
$profile->email=$faker->email;
$profile->first_name=$faker->firstName;
$profile->last_name=$faker->lastName;
$profile->save();
}
}
}
}

并使用
yii seed
命令来运行控制器。

如果不需要任何验证,也可以使用
batch insert
,只需将数组传递给自定义控制台命令即可

<?php
namespace app\commands;
use Yii;
use yii\console\Controller;
use yii\console\ExitCode;
use yii\db\Command;

class SeedController extends Controller
{
    
       

   
    public function actionUsers()
    {
        Yii::$app->db->createCommand()->batchInsert('users',
        [ 
            'username',
            'password',            
            'createdAt' ,
            'updatedAt'
            ],
            [

               ['user1', 'yourhashedpass', new \yii\db\Expression('NOW()'),new \yii\db\Expression('NOW()')],
['user2', 'yourhashedpass', new \yii\db\Expression('NOW()'),new \yii\db\Expression('NOW()')],
 
                
        ])->execute();
 return ExitCode::OK;

    }
}

请检查此选项->您可以基于扩展构建脚本。感谢您的回复。我实际上希望使用虚拟数据为我的数据库添加种子。就像可以使用Faker来完成一样。Hi Salem。感谢您的回复。您能为示例提供步骤吗?感谢您的回复。我创建了新文件YiiBasic\tests\unit\templates\fixtures\以下是内容:@FaisalQureshi您应该为
generate
方法配置伪造生成器模板文件。(示例:
php yii fixture/templates--templatePath='@app/path/to/my/custom/templates'
)。对于种子,我建议不要使用faker,最好用更正的外键、已知密码等填充数据库确定的数据。这实际上是创建自己种子的一个很好的方法。可能已经想到了这一点,干得好!