Showing posts with label yii framework. Show all posts
Showing posts with label yii framework. Show all posts

Thursday, June 20, 2013

yii framework tutorial - database MVC

MVC = Model View Controller
Model = berisi data, fungsi dan perhitungan = object + method2nya
View = template, theme
Controller = lem perekat View + Model

1. create table
CREATE TABLE IF NOT EXISTS `departemen` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nama` varchar(40) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `nama` (`nama`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `karyawan` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `dept_id` int(11) NOT NULL,
  `nama` varchar(40) NOT NULL,
  `lahir` date NOT NULL,
  `email` varchar(40) DEFAULT NULL,
  `ext` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `dept_id` (`dept_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

2. generate model dan class
Masuk ke
http://localhost/datarumah/index.php/gii

isi password gii anda

Pilih: Model Generator

isi table Name : departemen (isi * = semua table)
Tekan : Preview/Generate

Pilih Crud Generator

isi Model Class : Departemen
Tekan : Preview/Generate

Coba : http://localhost/datarumah/index.php/departemen

Model di turunkan dari Active Record
rules( ) adalah method terpenting utk validasi -> returnnya array

3.buka model /protected/models/Karyawan.php
tambahkan : array('email','email'),
utk validasi email

lihat detail di : http://www.yiiframework.com/doc/api/1.1/CModel

bila lahir mau diisi null, tambahkan:
/**
* @return null if value is empty string
*/
public function empty2null($value)
{
return $value===” ? null : $value;
}
array(‘lahir’, ‘filter’, ‘filter’=>array($this, ‘empty2null’)),

4.Buka View






yii framework tutorial - fast

1.download, extract di ROOT, di folder yii
[server]
--www = ROOT
     -- yii = CREATE MANUAL
          -- demos
          -- framework
          -- requirements
--etc

akses : www.domain.com/yii/requirements
pastikan yang dipakai = Passed

2.keluarkan folder yii  ke luar WEBROOT = utk security
[server]
--www = ROOT
-- yii
    -- demos
    -- requirements
    -- framework
--etc

3.Bila xampp di c:\xampp, maka CEK PHP bisa JALAN dengan :
buka COMMAND, ketik:   c:\xampp\php\php.exe -v

4.di COMMAND,jalankan yiic utk generate webapp

[path]php.exe     [path]yiic   webapp       [path ke ROOT]
contoh:
     c:\xampp\php\php.exe      [server]yii\framework\yiic  webapps     [server]/www

ada konfirmasi, ketik : yes, susunan folder menjadi :
--www = ROOT
      -- assets
      -- css
      -- images
      -- themes
      --protected
--yii
--etc


5. cek: www.domain.com, maka muncul basic yii

6. MORE SECURE: pindah folder protected ke luar ROOT
--www
      -- assets
      -- css
      -- images
      -- themes
--yii
--protected
--etc

edit di index.php, baris ke 5
$config=dirname(__FILE__).'/protected/config/main.php';
menjadi
$config=dirname(__FILE__).'../protected/config/main.php';

7. buka ../protected/config/main.php
ganti name = Aplikasi Baru

8. cek www.domain.com, lihat name nya

9. di main.php itu juga, aktifkan:
gii
urlManager
db  = pakai MySQL
log, pakai CWebLogRoute (baris 78)

10. cek domain.com, maka dibagian bawah muncul log yg indah :)