Seeding


Overview

Similarly to Laravel, WP Bones includes the ability to seed your database with data using seed classes. All seed classes are stored in the database/seeders directory. The seeder classes will be loaded and executed when the plugin is activated.

Writing Seeders

A seeder class only contains one method by default: run. You have to define the table name as well. Below is an example of a seeder class used in the wpbones/geolocalizer package.

Currently, there is no way to write seeders using php bones. We are working on that.

<?php
// remember that your namespace will be different
use WPKirk\WPBones\Database\Seeder;
 
// you have to return an instance of WPKirk\WPBones\Database\Seeder
return new class extends Seeder {
  /**
   * It will be converted to lowercase and with the WordPress prefix.
   *
   * @var string The table name.
   */
  protected $tablename = "countries";
 
  // the seeder run method is called automatically
  // here you can do everything you need to do
  public function run()
  {
      // for example, here we are going to truncate the table
      $this->truncate();
 
      // and then we are going to insert some data
      $this->insert(
      "(id, zone, country, isocode, currency, symbol, symbol_html, code, tax, continent, status)
      VALUES
      	(1,'','Mauritania','MR','Mauritanian Ouguiya','','','MRO',0.00,'africa','publish')");
  }
}

Currently, the seeder is executed when the plugin is activated. If you want to execute the seeder just one time, you can set the runOnce property to true.

<?php
// remember that your namespace will be different
use WPKirk\WPBones\Database\Seeder;
 
// you have to return an instance of WPKirk\WPBones\Database\Seeder
return new class extends Seeder {
  /**
   * It will be converted to lowercase and with the WordPress prefix.
   *
   * @var string The table name.
   */
  protected $tablename = "my_plugin_products";
 
  // Run the database seeds just once
  protected $runOnce = true;
 
  // the seeder run method is called automatically
  // here you can do everything you need to do
  public function run()
  {
      // since the seeder is executed only one time, we can remove the truncate method
      //$this->truncate();
 
      // and then we are going to insert some data
      $this->insert(
      "(id, zone, country, isocode, currency, symbol, symbol_html, code, tax, continent, status)
      VALUES
      	(1,'','Mauritania','MR','Mauritanian Ouguiya','','','MRO',0.00,'africa','publish')");
  }
}

WordPress prefix

As you know, the real name of the table my_plugin_products is {prefix}_my_plugin_products. Usually, {prefix} is wp_ for default. Anyway, you don’t need to use the WordPress prefix, WP Bones will do that for you.

Remove the WordPress prefix

Starting from version 1.7.0, you can remove the WordPress prefix. You may use the usePrefix property in the migration class:

<?php
// remember that your namespace will be different
use WPKirk\WPBones\Database\Seeder;
 
// you have to return an instance of WPKirk\WPBones\Database\Seeder
return new class extends Seeder {
  /**
   * It will be converted to lowercase and with the WordPress prefix.
   *
   * @var string The table name.
   */
  protected $tablename = "my_plugin_products";
 
  // Run the database seeds just once
  protected $runOnce = true;
 
  // Remove the WordPress prefix
  protected $usePrefix = false;
 
  // the seeder run method is called automatically
  // here you can do everything you need to do
  public function run()
  {
      // since the seeder is executed only one time, we can remove the truncate method
      //$this->truncate();
 
      // and then we are going to insert some data
      $this->insert(
      "(id, zone, country, isocode, currency, symbol, symbol_html, code, tax, continent, status)
      VALUES
      	(1,'','Mauritania','MR','Mauritanian Ouguiya','','','MRO',0.00,'africa','publish')");
  }
}

In this case the table name will be my_plugin_products.

Use a model

You can use a model to seed your database.

<?php
// remember that your namespace will be different
use WPKirk\WPBones\Database\Seeder;
 
// you have to use your model
use WPKirk\Models\MyPluginProducts;
 
// you have to return an instance of WPKirk\WPBones\Database\Seeder
return new class extends Seeder {
  /**
   * It will be converted to lowercase and with the WordPress prefix.
   *
   * @var string The table name.
   */
  protected $tablename = "my_plugin_products";
 
  // the seeder run method is called automatically
  // here you can do everything you need to do
  public function run()
  {
    // insert by using the model class
    MyPluginProducts::insert([
        ['name' => 'iMac', 'price' => '100000'],
        ['name' => 'iPhone', 'price' => '20000'],
        ['name' => 'iPad', 'price' => '30000'],
        ['name' => 'iPod', 'price' => '10000'],
      ]
    );
  }
}

Remove the WordPress prefix

If you want to remove the WordPress prefix, and you are using a model, you have to set the usePrefix property to false in the seeder class.

The seeder
<?php
// remember that your namespace will be different
use WPKirk\WPBones\Database\Seeder;
 
// you have to use your model
use WPKirk\Models\MyPluginBooks;
 
// you have to return an instance of WPKirk\WPBones\Database\Seeder
return new class extends Seeder {
  /**
   * It will be converted to lowercase and with the WordPress prefix.
   *
   * @var string The table name.
   */
  protected $tablename = "my_plugin_books";
 
  // Remove the WordPress prefix
  protected $usePrefix = false;
 
  // the seeder run method is called automatically
  // here you can do everything you need to do
  public function run()
  {
    // insert by using the model class
    MyPluginBooks::insert([
        ['name' => 'iMac', 'price' => '100000'],
        ['name' => 'iPhone', 'price' => '20000'],
        ['name' => 'iPad', 'price' => '30000'],
        ['name' => 'iPod', 'price' => '10000'],
      ]
    );
  }
}

In the Model class, you have to set to false the second parameter of the DB::getTableName method.

The model
<?php
namespace WPKirk\Models;
 
use Illuminate\Database\Eloquent\Model;
use WPKirk\WPBones\Database\DB;
 
class EloquentBook extends Model
{
  /**
   * Disable Illuminate timestamp columns.
   *
   * @var bool
   */
  public $timestamps = false;
 
  /**
   * Get the table associated with the model.
   */
  public function getTable(): string
  {
    return DB::getTableName('MyPluginBooks', false);
  }
}