KAEDE Hack blog

JavaScript 中心に ライブラリなどの使い方を解説する技術ブログ。

CakePHP Intro and Migration

cake3 usage

f:id:kei_s_lifehack:20190927015743j:plain

public tutorial

https://book.cakephp.org/3.0/en/tutorials-and-examples/cms/articles-controller.html

install

Use Composer.

getcomposer.org

curl -s https://getcomposer.org/installer | php

Composer (version 1.9.0) successfully installed to: /Users/kaede/code/cake3/composer.phar
php composer.phar create-project --prefer-dist cakephp/app hoge
- Installing cakephp/plugin-installer (1.1.1): Downloading (100%)

When completed, Directory will be like this:

▾ interns/
    ▸ bin/
    ▸ config/
    ▸ logs/
    ▸ plugins/
    ▸ src/
    ▸ tests/
    ▸ tmp/
    ▸ vendor/
    ▸ webroot/
        composer.jsongg
        composer.lock
        index.php
        phpunit.xml.dist
        README.md
composer.phar
README.md

start server

bin/cake server

runs app in http://localhost:8765/
Defalt Page is this:

f:id:kei_s_lifehack:20190926144456p:plain
cui server

f:id:kei_s_lifehack:20190926144536p:plain
8765 default

At first, your cakephp project is not connected to Database.

(img)

Writing setting file and making a database enables connection.

connect Database(DB)

Rewrite app.php

In Datasources:
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'username' => 'root',
'password' => '',
'database' => 'yourDBname',

create DB

Download XAMPP or LAMPP and create your DB.

www.mysql.com

www.apachefriends.org

f:id:kei_s_lifehack:20190926144713p:plain
xampp app

open XAMPP and start all.

mysql -u root
create database hoge;

You see SQL is connected now.

(img)

Manage table

www.ritolab.com

create table

CakePHP can make tables by simple commands

qiita.com

bin/cake bake migration CreateTableName  
columnName:string columnName:text created modified  

You can make sure your table was made by MySQL console.

f:id:kei_s_lifehack:20190926144837p:plain
tables

This makes 20190924_migration.php, but this did not made the table yet.

execute

You have to execute this by:

bin/cake migrations migrate

This made tables of the migration definition.

delete table

bin/cake bake migration DropTableName  

add a column to table

bin/cake bake migration AddColumnNameToTableName columnName:type
bin/cake bake migration AddPriceToTableName price:decimal

undo the migration

bin/cake migrations rollback

can reset.

delete a column

bin/cake bake migration RemovePriceFromTableName price

Make Controller

dev.to

bin/cake bake all tableNames

cmd can make proper files:
controller, model, ctps, and the functions index, add, edit, view,

See the CRUD in the browser

Conclusion

This article introduced:
making DB by SQL,
creating table and columns by migration,
creating controllers by bake all.

This made minimum CRUD system.