PHP code example of diecoding / yii2-seeder
1. Go to this page and download the library: Download diecoding/yii2-seeder library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
diecoding / yii2-seeder example snippets
// ...
'controllerMap' => [
// ...
'seeder' => [
'class' => \diecoding\seeder\SeederController::class,
],
// ...
],
// ...
// ...
'controllerMap' => [
// ...
'seeder' => [
'class' => \diecoding\seeder\SeederController::class,
/** @var string the default command action. */
'defaultAction' => 'seed',
/** @var string seeder path, support path alias */
'seederPath' => '@console/seeder',
/** @var string seeder namespace */
'seederNamespace' => 'console\seeder',
/**
* @var string this class look like `$this->seederNamespace\Seeder`
* default seeder class run if no class selected,
* must instance of `\diecoding\seeder\TableSeeder`
*/
'defaultSeederClass' => 'Seeder',
/** @var string tables path, support path alias */
'tablesPath' => '@console/seeder/tables',
/** @var string seeder table namespace */
'tableSeederNamespace' => 'console\seeder\tables',
/** @var string model namespace */
'modelNamespace' => 'common\models',
/** @var string path view template table seeder, support path alias */
'templateSeederFile' => '@vendor/diecoding/yii2-seeder/src/views/Seeder.php';
/** @var string path view template seeder, support path alias */
'templateTableFile' => '@vendor/diecoding/yii2-seeder/src/views/TableSeeder.php';
],
// ...
],
// ...
namespace console\seeder\tables;
use common\models\User;
use common\models\Province;
use diecoding\seeder\TableSeeder;
class UserTableSeeder extends TableSeeder
{
// public $truncateTable = false;
// public $locale = 'en_US';
/**
* Default execution
*
* @return void
*/
public function run()
{
$province = Province::find()->all();
$count = 100;
for ($i = 0; $i < $count; $i++) {
$this->insert(User::tableName(), [
'province_id' => $this->faker->randomElement($province)->id,
'email' => $this->faker->email,
'name' => $this->faker->name,
'phone' => $this->faker->phoneNumber,
'company' => $this->faker->company,
'street' => $this->faker->streetName,
'zip_code' => $this->faker->postcode,
'city' => $this->faker->city,
'created_at' => $this->faker->dateTime(),
'updated_at' => $this->faker->dateTime(),
'status' => User::STATUS_USER_ACTIVE
]);
}
}
}
public $truncateTable = false;
public $truncateTable = true;
// ...
// truncate table
$this->disableForeignKeyChecks();
$this->truncateTable(/* table names */);
$this->enableForeignKeyChecks();
public $locale = 'en_US';
/**
* Default Seeder
*/
class Seeder extends TableSeeder
{
/**
* Default execution
*
* @return void
*/
public function run()
{
ModelTableSeeder::create()->run();
}
}