I am new to Yii framework. I want to seed my database like it can be done in Laravel framework using Faker. I tried this http://www.yiiframework.com/forum/index.php/topic/59655-how-to-seed-yii2-database/ but it does not provide much details. I would really appreciate if someone can help me out with the steps in details.
Asked
Active
Viewed 1.0k times
11
-
Please check this one -> http://stackoverflow.com/questions/17114708/how-to-seed-in-yii – Mihriban Minaz Jan 25 '16 at 15:24
-
You can build [migration](http://www.yiiframework.com/doc-2.0/guide-db-migrations.html) scripts based on [yii2-faker](https://github.com/yiisoft/yii2-faker) extension. – Salem Ouerdani Jan 25 '16 at 15:27
-
Thanks for the reply. I am actually looking to seed my database with dummy data. Like it can be done using Faker. – Faisal Qureshi Jan 25 '16 at 15:29
-
Hi Salem. Thanks for your reply. Can you please provide the example with steps? – Faisal Qureshi Jan 25 '16 at 15:35
3 Answers
12
Creating console command and using Faker inside the console command controller to seed the database worked for me.
Following is the SeedController.php
file which I created under commands folder:
// 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();
}
}
}
}
And used yii seed
command to run the controller.

Faisal Qureshi
- 231
- 1
- 2
- 9
-
1This is actually quite a good method of creating your own seeds. Could have thought of this, Good job! – melledijkstra Nov 21 '16 at 19:15
-
What is the purpose of that for loop? I don't see you using it anywhere. If you call a model outside a loop it will only create one record in the database. – Hamfri Aug 29 '21 at 13:38
6
See at fixtures and faker realization in yii2-app-advanced tests. In project you also can write in console php yii fixture/load
to load seeds in database and php yii fixture/generate-all
to generate seed by faker.
yii.php
should have right fixture
controller in controllerMap
array:
[
'controllerMap' => [
'fixture' => [
'class' => 'yii\console\controllers\FixtureController',
'namespace' => 'common\ActiveRecords'
]
]
]

Onedev_Link
- 1,981
- 13
- 26
-
Thanks for the reply. I created the new file YiiBasic\tests\unit\templates\fixtures\users.php. Following is the content: $faker->firstName, 'password' => 123456 ]; ///////////////////////////////////////////////////////////// And after running 'php yii fixture/generate users' command. I get the following error in my command prompt: The following fixtures templates were NOT found: * users No fixtures template files matching input conditions were found under the pa C:\xampp\htdocs\YiiBasic/tests/codeception/unit/templates/fixtures – Faisal Qureshi Jan 25 '16 at 16:21
-
@FaisalQureshi you should configure faker generator template files for `generate` method. (example: `php yii fixture/templates --templatePath='@app/path/to/my/custom/templates'`). For seeds i recommend don't use faker, it is better to fill database determined data with corrected foreign keys, known passwords, etc... – Onedev_Link Jan 27 '16 at 10:55
0
You can also use batch insert
if you do not want any validations and simply pass arrays to your custom console command like.
<?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;
}
}
And run it by
php yii seed/users

Krishnadas PC
- 5,981
- 2
- 53
- 54