bin/cake bake seed Items
Creating file /var/www/html/config/Seeds/ItemsSeed.php
Wrote `/var/www/html/config/Seeds/ItemsSeed.php`
seederファイルを編集します。
<?php
declare(strict_types=1);
use Migrations\AbstractSeed;
/**
* Items seed.
*/
class ItemsSeed extends AbstractSeed
{
/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeds is available here:
* https://book.cakephp.org/phinx/0/en/seeding.html
*
* @return void
*/
public function run()
{
// itemsテーブルを指定
$table = $this->table('items');
// テーブルのデータを空にする
$table->truncate();
// データ
$data = [
[
'id' => 1,
'name' => 'iphone',
'price' => 120000,
'published' => 1,
'created_at' => '2022-03-07 12:00:00',
'updated_at' => null,
],
[
'id' => 2,
'name' => 'Mac',
'price' => 200000,
'published' => 1,
'created_at' => '2022-03-07 13:00:00',
'updated_at' => null,
],
[
'id' => 3,
'name' => 'book',
'price' => 2000,
'published' => 0,
'created_at' => '2022-03-07 14:00:00',
'updated_at' => null,
],
];
// itemsテーブルにデータをインサート
$table->insert($data)->save();
}
}
seederファイルを実行します。
$ bin/cake migrations seed
using migration paths
- /var/www/html/config/Migrations
using seed paths
- /var/www/html/config/Seeds
using migration paths
- /var/www/html/config/Migrations
using seed paths
- /var/www/html/config/Seeds
using environment default
using adapter mysql
using database cakephp4_tests
== ItemsSeed: seeding
== ItemsSeed: seeded 0.1615s
All Done. Took 0.1872s
コメント