Sunday, October 14, 2018

Migrations Tutorial For Beginner in CakePHP - Windows 10

In the previous post we have installed and configured cakePhp with postgreesql. So in this post we'll gonna learn about migrations in CakePhp. Simply put , migrations is all about creating, dropping, changing and removing database or table in cakePhp. So if we wanto create a table, it's really suggested to create it from migrations file because it will track every table that we created and it will be easier for another developer to create their own local environment with this migrations file only by using "bin\cake migrations migrate". 
In cakePhp, we can see all of available commands in migrations by using "bin\cake migrations". It will show 
Also we can see the migrations history in phinxlog table like this :
Ok... Let's create our first migration by running this command "bin\cake migrations create MyFirstMigration" 
So we named our first migration as MyFirstMigration and then we can see it in config/migrations/xxxxxxxxx_MyFirstMigration.php as shown in the picture below :

Next let's put something onto function change ()
So... From that function we will create a table named maryadi_table. It will contain a column name as string, description as text, level as integer, created and modified as datetime. The ID column will be added automatically and it will be primary autoincrement. 
Before we run migrate, we can check the migration status first like this :
We can see MyFirstMigration there... Next let's do the migration
We can see the result in our database :
Ok....Neext...
Let's try another way of creating migration file. We can use "bake" command as follows
So we created new migration file named MySecondMigration which has some column namely name as string, description as text and so on. The result is :
There is one thing to note here. If we want to create new table using bake, we need to use "Createxxxxx" at the front of our migration name. Also we can see that our table name is "my_second_migration". Okiee...Next we'll gonna add a new column to an existing table. In order to do that, we can use "....... AddxxxxToyyyy....". xxxx is the new table that will be added and yyyy is our current existing table. Example is shown below :

So by running that migration we'll gonna add power column to maryadi_table. 
As you might notice that at the end of each migration code, there is a $table->create and $table->update. That means creating and updating the table yeaaah...and rightnow we already have two migration files which is not yet migrated as shown below :
So let's run our migrations then and see the result as shown below:
Okiee... after learning how to add. Next we'll learn how to remove column. For removing column we can use a convension name like "RemovexxxxxxFromyyyyyyy". Example :

After running that migration, we'll see that power column is now gone.
Well... there are a lot of commands that's available for us to be used. But I won't cover it all in post. But I hope this post gives your a little bit way of thinking how to use migrations command in cakePhp so you can explore another commands by yourself ehehee... :-)
Okie that's all thank you so much... 

1 comment:

  1. Actually, I am facing some difficulties to understand the meaning of the blog. If you have any short video film related to your blog, then I would request you to share here. It would be great help.
    CakePHP Development Company

    ReplyDelete