 Keterangan Contoh controller berikut mimiliki fasilitas untuk menambah, mengedit, menghapus, dan melihat data Persiapan awal silahkan lihat posting-posting sebelumnya, karena artikel ini tidak membahas dari awal, tapi langsung ke bagian controller dan view script untuk CRUD Quick Way langsung saja...tidak banyak teori On Controller <?php class PaymentMethodController extends Zend_Controller_Action { function init() { if (! Zend_Auth::getInstance ()->hasIdentity ()) { $this->_redirect ( '/default/auth' ); } Zend_Registry::set ( 'table', new tableCrpm ( ) ); Zend_Registry::set ( 'redirect', '/default/paymentmethod' ); $this->view->judul = 'Payment Method'; } function indexAction() { $table = Zend_Registry::get ( 'table' ); $this->view->table = $table->fetchAll ( null, $this->_request->getParam ( 'order' ) ); }
function deleteAction() { $id = ( string ) $this->_request->getParam ( 'id' ); $table = Zend_Registry::get ( 'table' ); $where = 'id = "' . $id . '"'; $table->delete ( $where ); $this->_redirect ( Zend_Registry::get ( 'redirect' ) ); } function formAction() { $mode = $this->_request->getParam ( 'mode' ); //nggo ngecek new opo edit $sesi = new Zend_Session_Namespace ( 'Default' ); //nggo edit tok $form = $this->getForm (); $form->submit->setLabel ( ucwords ( $mode ) ); $this->view->judul = $this->view->judul . ' - ' . ucwords ( $mode ); $this->view->form = $form; if ($this->_request->isPost ()) { $formData = $this->_request->getPost (); if ($form->isValid ( $formData )) { $table = Zend_Registry::get ( 'table' ); //cek new opo edit if ($mode == 'new') { $row = $table->createRow (); } else if ($mode == 'edit') { $row = $table->fetchRow ( 'id="' . $sesi->id . '"' ); } //end cek new opo edit $row->id = $form->getValue ( 'id' ); $row->title = $form->getValue ( 'title' ); $row->save (); $this->_redirect ( Zend_Registry::get ( 'redirect' ) ); } else { $form->populate ( $formData ); } } else if ($mode == 'edit') { //menampilkan data awal jika edit belum post $id = $this->_request->getParam ( 'id' ); $sesi->id = $id; //nggo edit tok $table = Zend_Registry::get ( 'table' ); $table = $table->fetchRow ( 'id="' . $id . '"' ); $form->populate ( $table->toArray () ); } } function getForm() { $id = new Zend_Dojo_Form_Element_TextBox ( 'id' ); $id->setLabel ( 'ID' ); $title = new Zend_Dojo_Form_Element_TextBox ( 'title' ); $title->setLabel ( 'Title' ); $submit = new Zend_Dojo_Form_Element_SubmitButton ( 'submit' ); $form = new Zend_Dojo_Form ( ); $form->setMethod ( post )->addElement ( $id )->addElement ( $title ) ->addElement ( $submit ); return $form; } } On View Script Index <?php echo "<h2> $this->judul - List </h2>"; ?> <table> <caption></caption> <thead> <tr> <th>No</th> <th><a href="/x/<?php echo $this->url(array('controller'=>'paymentmethod', 'action'=>'index', 'order'=>'id'));?>">ID</a></th> <th><a href="/x/<?php echo $this->url(array('controller'=>'paymentmethod', 'action'=>'index', 'order'=>'title'));?>">Title</a></th> <th> </th> </tr> </thead> <tbody> <?php $i=0; foreach($this->table as $x) : $i++; if ($i & 1 == 1) { $telo=''; } else { $telo='class ="odd"'; } ?> <tr <?php echo $telo ?>> <td><?php echo $i; ?></td> <td><?php echo $this->escape($x->id);?></td> <td><?php echo $this->escape($x->title);?></td> <td> <a href="/x/<?php echo $this->url(array('controller'=>'paymentmethod', 'action'=>'form', 'mode'=>'edit', 'id'=>$x->id));?>">Edit</a> <a href="/x/<?php echo $this->url(array('controller'=>'paymentmethod', 'action'=>'delete', 'id'=>$x->id));?>">Delete</a> </td> </tr> <?php endforeach; ?> </tbody> <tfoot> <tr> <th colspan="4"><a href="/x/<?php echo $this->url( array('controller'=>'paymentmethod', 'action'=>'form','mode'=>'new'));?>"> Add new payment method</a></th> </tr> </tfoot> </table> On View Script Form <?php $this->dojo()->enable(); echo "<h2> $this->judul </h2>"; echo $this->form; Question? Comments welcome
|