Creating Simple CRUD Application Using CakePHP


Untuk membuat aplikasi CRUD (Create-Read-Update-Delete) secara otomatis, sebagai contoh buatlah 1 buah database di mysql dengan nama dbcakephp, kemudian didalam database tersebut buatlah 2 buah table yaitu CATEGORIES dan POSTS. Table CATEGORIES akan berisi data-data kategori, sedangkan di table POSTS akan berisi data-data postingan dengan menampilkan kategorinya. Format foreign key yaitu namatableforeignsingular_id. Konvention penamaan table bisa dilihat di NAMING CONVENTION CAKEPHP. Script buat table bisa dilihat dibawah ini :

CREATE TABLE `categories` (
`id` int(10) NOT NULL auto_increment,
`name` varchar(50) default NULL,
`created` datetime default NULL,
`updated` datetime default NULL,
`keterangan` varchar(50) default NULL,
PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=19 DEFAULT CHARSET=utf8
CREATE TABLE `posts` (
`id` int(10) NOT NULL auto_increment,
`title` varchar(100) default NULL,
`content` text,
`created` datetime default NULL,
`updated` datetime default NULL,
`category_id` int(10) default NULL,
PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
CATEGORIES POSTS

Setelah kita selesai membuat database dan 2 table tersebut, buatlah 1 buah project dengan nama ‘newcake3’. Pembuatan projek bisa dilihat di MEMBUAT PROJEK BARU MENGGUNAKAN FITUR ‘BAKE’. Setelah itu bukalah command prompt, untuk melakukan generate controller, model dan views. Dicontoh ini kita akan mengenerate untuk table CATEGORIES :

C:\xampp2\htdocs\newcake3>cake bake all
PHP Warning:  PHP Startup: Invalid library (maybe not a PHP library) 'php_pdo_oc
i8.dll'  in Unknown on line 0?
Welcome to CakePHP v1.3.7 Console
---------------------------------------------------------------
App : newcake3
Path: C:\xampp2\htdocs\newcake3
---------------------------------------------------------------
---------------------------------------------------------------
Bake All
---------------------------------------------------------------
Possible Models based on your current database:
1. Book
2. Category
3. Post
4. Student
Enter a number from the list above,
type in the name of another model, or 'q' to exit
[q] > 2
 

Baking model class for Category…

Creating file C:\xampp2\htdocs\newcake3\models\category.php
Wrote `C:\xampp2\htdocs\newcake3\models\category.php`
Category Model was baked.
SimpleTest is not installed. Do you want to bake unit test files anyway? (y/n)
[y] >

You can download SimpleTest from http://simpletest.org

Baking test fixture for Category…

Creating file C:\xampp2\htdocs\newcake3\tests\fixtures\category_fixture.php
Wrote `C:\xampp2\htdocs\newcake3\tests\fixtures\category_fixture.php`
Bake is detecting possible fixtures..

Creating file C:\xampp2\htdocs\newcake3\tests\cases\models\category.test.php
Wrote `C:\xampp2\htdocs\newcake3\tests\cases\models\category.test.php`

Creating file C:\xampp2\htdocs\newcake3\controllers\categories_controller.php
Wrote `C:\xampp2\htdocs\newcake3\controllers\categories_controller.php`
Category Controller was baked.
SimpleTest is not installed. Do you want to bake unit test files anyway? (y/n)
[y] >

You can download SimpleTest from http://simpletest.org
Bake is detecting possible fixtures..

Creating file C:\xampp2\htdocs\newcake3\tests\cases\controllers\categories_contr
oller.test.php
Wrote `C:\xampp2\htdocs\newcake3\tests\cases\controllers\categories_controller.t
est.php`

Creating file C:\xampp2\htdocs\newcake3\views\categories\index.ctp
Wrote `C:\xampp2\htdocs\newcake3\views\categories\index.ctp`

Creating file C:\xampp2\htdocs\newcake3\views\categories\view.ctp
Wrote `C:\xampp2\htdocs\newcake3\views\categories\view.ctp`

Creating file C:\xampp2\htdocs\newcake3\views\categories\add.ctp
Wrote `C:\xampp2\htdocs\newcake3\views\categories\add.ctp`

Creating file C:\xampp2\htdocs\newcake3\views\categories\edit.ctp
Wrote `C:\xampp2\htdocs\newcake3\views\categories\edit.ctp`
Category Views were baked.
Bake All complete


Setelah mengenerate table ‘CATEGORIES’, kita akan mengenerate untuk table ‘POSTS’. Tampilan command prompt seperti dibawah ini :

C:\xampp2\htdocs\newcake3>cake bake all
PHP Warning:  PHP Startup: Invalid library (maybe not a PHP library) 'php_pdo_oci8.dll' in Unknown on line 0?
Welcome to CakePHP v1.3.7 Console
---------------------------------------------------------------
App : newcake3
Path: C:\xampp2\htdocs\newcake3
---------------------------------------------------------------
---------------------------------------------------------------
Bake All
---------------------------------------------------------------
Possible Models based on your current database:
1. Book
2. Category
3. Post
4. Student
Enter a number from the list above,
type in the name of another model, or 'q' to exit
[q] > 3
 

Baking model class for Post…

Creating file C:\xampp2\htdocs\newcake3\models\post.php
Wrote `C:\xampp2\htdocs\newcake3\models\post.php`
Post Model was baked.
SimpleTest is not installed. Do you want to bake unit test files anyway? (y/n)
[y] >

You can download SimpleTest from http://simpletest.org

Baking test fixture for Post…

Creating file C:\xampp2\htdocs\newcake3\tests\fixtures\post_fixture.php
Wrote `C:\xampp2\htdocs\newcake3\tests\fixtures\post_fixture.php`
Bake is detecting possible fixtures..

Creating file C:\xampp2\htdocs\newcake3\tests\cases\models\post.test.php
Wrote `C:\xampp2\htdocs\newcake3\tests\cases\models\post.test.php`

Creating file C:\xampp2\htdocs\newcake3\controllers\posts_controller.php
Wrote `C:\xampp2\htdocs\newcake3\controllers\posts_controller.php`
Post Controller was baked.
SimpleTest is not installed. Do you want to bake unit test files anyway? (y/n)
[y] >

You can download SimpleTest from http://simpletest.org
Bake is detecting possible fixtures..

Creating file C:\xampp2\htdocs\newcake3\tests\cases\controllers\posts_controller
.test.php
Wrote `C:\xampp2\htdocs\newcake3\tests\cases\controllers\posts_controller.test.p
hp`

Creating file C:\xampp2\htdocs\newcake3\views\posts\index.ctp
Wrote `C:\xampp2\htdocs\newcake3\views\posts\index.ctp`

Creating file C:\xampp2\htdocs\newcake3\views\posts\view.ctp
Wrote `C:\xampp2\htdocs\newcake3\views\posts\view.ctp`

Creating file C:\xampp2\htdocs\newcake3\views\posts\add.ctp
Wrote `C:\xampp2\htdocs\newcake3\views\posts\add.ctp`

Creating file C:\xampp2\htdocs\newcake3\views\posts\edit.ctp
Wrote `C:\xampp2\htdocs\newcake3\views\posts\edit.ctp`
Post Views were baked.
Bake All complete


Setelah selesai, arahkan url ke http://localhost/newcake3/categories untuk menambahkan data kategori atau arahkan url ke http://localhost/newcake3/posts untuk menambahkan data post lengkap dengan isi category yang terhubung dari data category. Jika kita perhatikan, table dibagian bawah merupakan informasi operasi sql query yang dilakukan oleh CakePHP. Ditampilkan secara otomatis oleh cake karena secara default project berada dalam modus development. Ini berguna untuk melakukan tracing, optimizing, dll, dalam tahap development. Namun jika ingin menghilangkannya sangat mudah ubah saja konfigurasi nya di ...config\core.php dan ganti nilainya menjadi 0 atau 1 seperti berikut : Configure::write('debug', 1);
Trus bagaimana caranya jika kita mengetikkan url http://localhost/newcake3 akan langsung mengarah ke controller categories atau posts?? Caranya cukup simple, dengan modifikasi file ...config\router.php seperti berikut :
//Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/', array('controller' => 'categories', 'action' => 'index'));

Tampilan Data 'Categories'

Tampilan Data 'Categories'

Tampilan Data 'POSTS'

Tampilan Data 'POSTS'

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s