Tutorial kali ini adalah lanjutan dari Instalasi CakePHP di Fedora 18, kita akan tetap menggunakan database demodb. Untuk studi kasusnya kita akan membuat data Mahasiswa.

Login ke database melalui terminal

mysql -u root -p

pindah ke database demodb; dan tambahkan table mahasiswa

use demodb;
CREATE TABLE IF NOT EXISTS `mahasiswa` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nama` varchar(100) NOT NULL,
  `nim` varchar(16) NOT NULL,
  `kelas` varchar(8) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

kedua perintah diatas outputnya kurang lebih seperti gambar dibawah ini
mysql client linux

pembuatan Create Read Update Delete (CRUD) ini akan menggunakan fitur scaffolding di CakePHP, dan menggunakan bantuan bake. Merujuk pada instalasi CakePHP sebelumnya, maka letak folder cakephp berada di

/var/www/html/cakephp/

pindah ke direktori app/Console

cd /var/www/html/cakephp/app/Console

eksekusi perintah dibawah ini untuk memulai

./cake bake

Pembuatan Model

Untuk pertama langkah yang akan dijalankan bisa dilihat step by stepnya dibawah ini, disini adalah tahap pembuatan Modem untuk tabel Mahasiswa

[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[F]ixture
[T]est case
[Q]uit
What would you like to Bake? (D/M/V/C/P/F/T/Q) 
> M
---------------------------------------------------------------
Bake Model
Path: /var/www/html/cakephp/app/Model/
---------------------------------------------------------------
Use Database Config: (default/test) 
[default] > 
Possible Models based on your current database:
1. Mahasiswa
Enter a number from the list above,
type in the name of another model, or 'q' to exit  
[q] > 1
A displayField could not be automatically detected
would you like to choose one? (y/n) 
> y
1. id
2. nama
3. nim
4. kelas
Choose a field from the options above:  
> 2
Would you like to supply validation criteria 
for the fields in your model? (y/n) 
[y] > n
Would you like to define model associations
(hasMany, hasOne, belongsTo, etc.)? (y/n) 
[y] > n
 
---------------------------------------------------------------
The following Model will be created:
---------------------------------------------------------------
Name:       Mahasiswa
DB Table:   `demodb`.`mahasiswa`
---------------------------------------------------------------
Look okay? (y/n) 
[y] >  
 
Baking model class for Mahasiswa...
 
Creating file /var/www/html/cakephp/app/Model/Mahasiswa.php
Wrote `/var/www/html/cakephp/app/Model/Mahasiswa.php`
PHPUnit is not installed. Do you want to bake unit test files anyway? (y/n) 
[y] > n

Pembuatan Controller

tahap ini adalah pembuatan controller untuk tabel mahasiswa, setelah eksekusi diatas selesai, anda akan kembali ke tampilan pilihan menu untuk di bake, langkah demi langkahnya akan ditampilkan dibawah ini, silahkan di ikuti

Welcome to CakePHP v2.3.1 Console
---------------------------------------------------------------
App : app
Path: /var/www/html/cakephp/app/
---------------------------------------------------------------
Interactive Bake Shell
---------------------------------------------------------------
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[F]ixture
[T]est case
[Q]uit
What would you like to Bake? (D/M/V/C/P/F/T/Q) 
> C
---------------------------------------------------------------
Bake Controller
Path: /var/www/html/cakephp/app/Controller/
---------------------------------------------------------------
Use Database Config: (default/test) 
[default] > 
Possible Controllers based on your current database:
---------------------------------------------------------------
 1. Mahasiswas
Enter a number from the list above,
type in the name of another controller, or 'q' to exit  
[q] > 1
---------------------------------------------------------------
Baking MahasiswasController
---------------------------------------------------------------
Would you like to build your controller interactively? (y/n) 
[y] > y
Would you like to use dynamic scaffolding? (y/n) 
[n] > y
 
---------------------------------------------------------------
The following controller will be created:
---------------------------------------------------------------
Controller Name:
	Mahasiswas
public $scaffold;
---------------------------------------------------------------
Look okay? (y/n) 
[y] >  
 
Baking controller class for Mahasiswas...
 
Creating file /var/www/html/cakephp/app/Controller/MahasiswasController.php
Wrote `/var/www/html/cakephp/app/Controller/MahasiswasController.php`
PHPUnit is not installed. Do you want to bake unit test files anyway? (y/n) 
[y] > n

Sekarang buka alamat http://localhost/cakephp/mahasiswas atau http://localhost/cakephp/index.php/mahasiswas tampilannya seperti dibawah ini

tabel mahasiswa
tambahkan beberapa data agar CRUD-nya kelihatan.
data mahasiswa

Leave a comment

Your email address will not be published. Required fields are marked *