CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY
DEFINER VIEW `vbuku` AS
DELIMITER ;
2.
Script cakePHP
Menggunakan Cake PHP versi 2, letakkan folder di xampp -> htdocs
kemudian rename menjadi nama"perpustakaan"
Membuat Model
Terletak di perpustakaan - app -model bikin model degan nama sebagai berikut :
Bahasa.php
<?php
class Bahasa extends
AppModel {
var
$nama='Bahasa';
var
$useTable='bahasa';
var
$hasAndBelongsToMany=array('Buku');
var
$displayField='nama';
}
?>
|
Pengarang.php
<?php
class Pengarang
extends AppModel {
var
$nama='Pengarang';
var
$useTable='pengarang';
var
$displayField='nama';
var
$hasAndBelongsToMany=array('Buku');
}
?>
|
|
Penerbit.php
<?php
class Penerbit
extends AppModel{
var
$name='Penerbit';
var
$useTable='penerbit';
var
$hasMany=array ('buku');
var
$displayField='nama';
}
?>
|
|
Vbuku.php
<?php
class Vbuku extends
AppModel{
var
$name='Vbuku';
var
$useTable='vbuku';
}
?>
|
|
Membuat Controller
Terletak di perpustakaan - app -controller bikin controller dengan nama sebagai berikut :
BahasasController.php
<?php
class
BahasasController extends AppController {
var
$name='Bahasas';
var $uses=array('Bahasa');
var $scaffold;
}
?>
|
|
|
|
|
PenerbitsController.php
<?php
class
BahasasController extends AppController {
var $name='Bahasas';
var
$uses=array('Bahasa');
var $scaffold;
}
?>
BukusController.php
<?php
class BukusController
extends AppController {
var
$name='Bukus';
var
$uses = array('Vbuku','Buku','Penerbit','Pengarang','Bahasa');
function
index(){
$data
= $this->Vbuku->find('all');
$this->set('buku',
$data);
}
function
add() {
if
(!empty($this->data)) {
if
($this->Buku->save($this->data)) {
$this->Session->setFlash('Buku
telah disimpan');
$this->redirect(array('action'=>'index'),
null, true);
}
else {
$this->Session->setFlash('buku
belum disimpan.Coba lagi!.');
}
}
$penerbit
= $this->Penerbit->find('list', array());
$this->set('penerbit',
$penerbit);
$pengarang
= $this->Pengarang->find('list', array());
$this->set('pengarangs',
$pengarang);
$bahasa
= $this->Bahasa->find('list', array());
$this->set('bahasa',
$bahasa);
}
function
edit($id=null) {
if
(!empty($this->data)) {
if
($this->Buku->save($this->data)) {
$this->Session->setFlash('Buku
telah disimpan');
$this->redirect(array('action'=>'index'),
null, true);
}
else {
$this->Session->setFlash('buku
belum disimpan.Coba lagi!.');
}
}else
{
$this->data
= $this->Buku->findById($id);
}
$penerbit
= $this->Penerbit->find('list', array());
$this->set('penerbit',
$penerbit);
$pengarang
= $this->Pengarang->find('list', array());
$this->set('pengarangs',
$pengarang);
$bahasa
= $this->Bahasa->find('list', array());
$this->set('bahasa',
$bahasa);
}
function
delete($id =null) {
if
(!$id) {
$this->Session->setFlash('id
Buku tidak valid');
$this->redirect(array('action'=>'index'),
null, true);
}
if
($this->Buku->delete($id)) {
$this->Session->setFlash('Buku
#'.$id.' dihapus.');
$this->redirect(array('action'=>'index'),
null, true);
}
}
}
?>
Membuat View
Terletak di perpustakaan - app -view bikin view dengan nama sebagai berikut :
Index.ctp
<h2>Daftar Buku</h2>
<?php echo $this->Html->link('Tambah
Buku',array('action'=>'add')); ?>
<?php if(empty($buku)): ?>
<p>tidak ada
daftar buku</p>
<?php else: ?>
<br/>
<table>
<tr>
<th>Judul</th>
<th>Pengarang</th>
<th>No.Buku</th>
<th>Tahun</th>
<th>Edisi</th>
<th>Penerbit</th>
<th>Kota
Penerbit</th>
<th>Bahasa</th>
<th>Aksi</th>
</tr>
<?php foreach
($buku as $buku): ?>
<tr>
<td><?php
echo $buku['Vbuku']['judul'] ?></td>
<td><?php
echo $buku['Vbuku']['pengarang'] ?></td>
<td><?php
echo $buku['Vbuku']['no_buku'] ?></td>
<td><?php
echo $buku['Vbuku']['tahun_terbit'] ?></td>
<td><?php
echo $buku['Vbuku']['edisi'] ?></td>
<td><?php
echo $buku['Vbuku']['penerbit'] ?></td>
<td><?php
echo $buku['Vbuku']['kota'] ?></td>
<td><?php
echo $buku['Vbuku']['bahasa'] ?></td>
<td>
<?php
echo $this->Html->link('Edit',
array('action'=>'edit',$buku['Vbuku']['id'])); ?>
<?php
echo $this->Html->link('Delete', array('action'=>'delete',
$buku['Vbuku']['id']), null, 'Anda yakin akan menghapus buku ini?');
?>
</td>
</tr>
<?php
endforeach; ?>
</table>
<?php endif;
?>
|
add.ctp
<?php echo
$this->Form->create('Buku',array('url'=>'add'));?>
<fieldset>
<legend>Tambah Buku</legend>
<?php
echo $this->Form->input('judul');
echo $this->Form->input('Pengarang',array( 'type' =>
'select', 'multiple' => 'checkbox' ));
echo $this->Form->input('no_buku');
echo $this->Form->input('tahun_terbit');
echo $this->Form->input('edisi');
echo $this->Form->input('penerbit_id', array('div' =>
'typeselect','options' => $penerbit,'empty' => false));
echo $this->Form->input('bahasa_id', array('div' =>
'typeselect','options' => $bahasa,'empty' => false));
?>
</fieldset>
<?php echo $this->Form->end('Simpan');?>
<?php echo $this->Html->link('Daftar semua
buku',array('action'=>'index')); ?>$buku['Vbuku']['id']), null, 'Anda
yakin akan menghapus buku ini?'); ?>
</td>
</tr>
<?php endforeach;
?>
</table>
<?php endif;
?>
edit.ctp
<?php echo $this->Form->create('Buku',array('url'=>'edit'));?>
<fieldset>
<legend>Ubah Buku</legend>
<?php
echo $this->Form->hidden('id');
echo $this->Form->input('judul');
echo $this->Form->input('Pengarang',array( 'type' =>
'select', 'multiple' => 'checkbox' ));
echo $this->Form->input('no_buku');
echo $this->Form->input('tahun_terbit');
echo $this->Form->input('edisi');
echo $this->Form->input('penerbit_id', array('div' =>
'typeselect','options' => $penerbit,'empty' => false));
echo $this->Form->input('bahasa_id', array('div' =>
'typeselect','options' => $bahasa,'empty' => false));
?>
</fieldset>
<?php echo $this->Form->end('Simpan');?>
<?php echo $this->Html->link('Daftar semua
buku',array('action'=>'index')); ?>
Hasilnya:
Gambar 1.1. CakePHP
2.6.2 telah aktif