Thursday 12 April 2012

zend CRUD action example

hi


starting off with create action / add action
 class ExampleController extends Zend_Controller_Action{  
 public function createAction(){  
    $form = // create a form or call a model form class  
    if ($this->getRequest()->isPost()) { // processing the form after the submission  
        if ($form->isValid($this->_request->getPost())) // check wheather the form data is valid  
    {  
 // do stuff  
         }  
                    }  
 }  
 $this->view->form=$form; // send the form to the view  
 }  
 //update action and modified action  
 class ExampleController extends Zend_Controller_Action{  
 public function updateAction(){  
     $id = $this->getRequest()->getParam('id');  
    $form = // create a form or call a model form class  
   if ($id > 0) {  
 if ($this->getRequest()->isPost()){  
      if ($form->isValid($this->_request->getPost())){  
            //if valid do some stuff  
 }else{  
   //reload the form with error message  
 }  
 }else{  
      //if the form not submitted load the form with default values  
 }  
 }  
 $this->view->form=$form; // send the form to the view  
 }  
 }  
 // view action  
 class ExampleController extends Zend_Controller_Action{  
 public function viewAction(){  
  $id = $this->getRequest()->getParam('id');  
 if($id>0){  
 // display content in the view template file  
 }  
 }  
 }  
 // delete acion  
 class ExampleController extends Zend_Controller_Action{  
 public function deleteAction(){  
    $id = $this->getRequest()->getParam('id');  
    if ($sid > 0) {  
    //do some stuff  
    $this->db->delete("id = $id");  
    $this->_redirect('');//redirection  
    }  
   }  
 }