Table of Contents
Inside this article we will understand CakePHP 4 Migrations Rollback in Non Shell Environment. Article contains classified information about executing migrations rollback for database.
Non Shell Environment means executing functions without using shell / terminal etc.
Migrations are those files which creates table schema for application. In CakePHP 4, we have a class available Migrations. We will use methods of this class to run migrations, check migration status, migrations rollback in non shell environment.
Rollback is the process to drop or remove table schema from database.
Learn More –
- CakePHP 4 Remove Column From Table Using Migration
- CakePHP 4 Rollback Migrations To Specific Migration Version
- CakePHP 4 Run Migrations in Non Shell Environment
- How To Check CakePHP Version in a CakePHP Project
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.
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.
To Run migrations rollback in non shell environment, follow these steps.
- Use & Load Migrations Class
- Call Migrations 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 Migration Methods
To check migration’s status like they executed or not, we have a status() method. It returns an array of all migrations and their status.
There are two types of status flags in migrations.
“up” status means migration migrated and will be available in phinxlog table. “down” status means migration rollback and table removed from database.
$status = $migrations->status();
To Rollback Migrations file we use rollback() method.
$migrate = $migrations->rollback();
It will return true if success. If an error occurred, an exception will be thrown. It drop tables step by step to it’s previous versions.
We hope this article helped you to learn about CakePHP 4 Migrations Rollback 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.