CakePHP 4 Run Specific Seeder in Non Shell Environment

Reading Time: 3 minutes
915 Views

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 –

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.

use Migrations\Migrations;

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 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.