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;
}
* @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