Sunday, July 30, 2017

Bake and migration

Contents

CakePHP
1. Setting up CakePHP3 with CentOS

2. Bake and Migration


4. Add a calendar page

5. Use "contain"

6. Use find('list')

What is migration?


Simply speaking, migration is used to create new tables or to change tables from php files with simple operations. Or to share such changes with other members through migration files. For example, run these commands:
$ cd /vagrant/cake
$ bin/cake bake migration CreateProducts name:string description:text created modified
(Related cakephp 3 cookbook page.)

You will see a new migration file is added in cake/config/Migrations.

From this migration file, new table will be created. Now run the following command:
(If you can't run this command, maybe your permission in the shared folder is not properly configured. See this post to fix the issue.)
$ bin/cake migrations migrate

Then new table is created automatically.

You can check if the new table is really created:
$ mysql -uroot -proot
mysql> show databases;
mysql> use test;
mysql> show tables;


You can see the new table "products" is added. As you see, if you can use migration command, you can create a new table easily from the migration command and migration file. Migration files can be shared in a team, so you can share how tables should be created and changed (or deleted) through migration files in a team.

Bake


Now from this table, we will create a website template. Use the following bake command.
$ bin/cake bake all products

This means "Create all of the three: controller, model, template (view) files." If you want to create each of them separately, run these:
$ bin/cake bake controller products
$ bin/cake bake model products
$ bin/cake bake template products

Then template files are automatically created.

Now access your cakephp 3 website from this URL:
http://192.168.33.10/cake/products/

You will see the basic files & functions were automatically generated:


From the "New Products" page, you can add new product to your database. If you change the design a little, it could be used as a website to store & maintain product information. If there is a customer who wants a simple website that stores & maintains products' information, you can create such website very quickly with cakephp 3 (without bugs as long as you don't touch it...Although I'm sure we must touch it to customize something).

Now you can see, from a single and simple migration file, you can create basic website with simple commands. You can add other pages to your cakephp 3 by using other migration files.

Conclusion


What is migration used for? --- To create or change or delete tables from migration files and to share the changes with other members though the migrations files.
What is bake used for? --- To create basic features (Controller, Model, Template) from a table.