Inside this article we will understand CakePHP 4 How To Seed Specific Database Seeder. Article contains classified information about seeding seeders to database table.
Seeders are those files which generates “test data” for database table. Database Seeding means Inserting Sample data inside database tables. This is usually we do to Test application.
Suppose, application contains multiple database seeder files. We want to run a single seeder in CakePHP 4. This article will help to get this understanding.
Learn More –
- CakePHP 4 How To Check Migrations Status Tutorial
- CakePHP 4 How To Create Migrations For Database
- CakePHP 4 How To Rename Table Using Migration
- CakePHP 4 How To Seed Data in Database Table Tutorial
Le’s get started.
CakePHP 4 Installation
To create a CakePHP project, run this command into your shell or terminal. Make sure composer should be installed in your system.
$ composer create-project --prefer-dist cakephp/app:~4.0 mycakephp
Above command will creates a project with the name called mycakephp.
Database Connection
Open app_local.php file from /config folder. Search for Datasources. Go to default array of it.
You can add your connection details here to connect with your database. It will be like this –
//... 'Datasources' => [ 'default' => [ 'host' => 'localhost', /* * CakePHP will use the default DB port based on the driver selected * MySQL on MAMP uses port 8889, MAMP users will want to uncomment * the following line and set the port accordingly */ //'port' => 'non_standard_port_number', 'username' => 'root', 'password' => 'sample@123', 'database' => 'mydatabase', /* * If not using the default 'public' schema with the PostgreSQL driver * set it here. */ //'schema' => 'myapp', /* * You can use a DSN string to set the entire configuration */ 'url' => env('DATABASE_URL', null), ], //... //...
You can pass host, username, password and database.
Successfully, you are now connected with the database.
Create Data Seeders
To create data seeders, run these command to terminal.
Seeder #1
$ bin/cake bake seed Products
It will create a file ProductsSeed.php inside /config/Seeds folder.
Seeder #2
$ bin/cake bake seed Students
It will create a file StudentsSeed.php inside /config/Seeds folder.
Now, we can see we have two different Seeders. For now these are only for demonstration. You can consider your application seeders.
Suppose we have all code to data seed in these files.
Let’s see how to migrate a specific migration i.e StudentsSeed.php
Run Seeder
Run all application seeders we use this given command.
$ bin/cake migrations seed
This command will run all the seeder from Seeds folder.
Specific Seeder
To run a specific seeder file we need to use –seed flag along with command.
Syntax
$ bin/cake migrations seed --seed <SeederClassName>
Example
$ bin/cake migrations seed --seed StudentsSeed
Above command only seed StudentsSeed seeder file.
We hope this article helped you to learn about CakePHP 4 How To Seed Specific Database Seeder Tutorial in a very detailed way.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.