Table of Contents
Inside this article we will understand CakePHP 4 Run Specific Seeder in Non Shell Environment. Article contains classified information about generating seeders for database table.
Non Shell Environment means executing functions without using shell / terminal etc.
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.
Learn More –
- CakePHP 4 How To Check All Routes of Application
- CakePHP 4 How To Check Migrations Status Tutorial
- CakePHP 4 How To Create Migrations For Database
- CakePHP 4 How To Rename Table Using Migration
Let’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.
Run Specific Seeder
Suppose you have many data seeders available in your application directory in /confirg/Seeds folder.
To Run Seeders in Non Shell Environment we need to follow these steps –
- Use & Load Migration Class
- Use Migration Methods
Use & Load Migrations Class
Inside you controller or wherever you are running migrations, you can import this class.
Next, we need to create an instance / object of this class to use it’s methods.
$migrations = new Migrations();
Call Seed Method
To seed data seeders file we use seed() method. This you can use in non shell environment.
$dataSeeder = $migrations->seed();
Above code will execute all seeders available inside /config/Seeds folder.
If you want to seed only a specific seeder file in non shell environment, you can use like this.
$dataSeeder = $migrations->seed([ "seed" => "ProductsSeed" ]);
We hope this article helped you to learn about CakePHP 4 Run Specific Seeder in Non Shell Environment Tutorial in a very detailed way.
Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.