Apply for Zend Framework Certification Training

Zend Framework 2




Step 1  manage module.config to accept id   module.config.php
'route'    => '/[:controller][/:action][/:id]',

Step 2  create file in model  EmployeeTable.php
public function getEmployee($id)
{
        $id  = (int) $id;
	$rowset = $this->tableGateway->select(array('id' => $id));
	$row = $rowset->current();
	return $row;
			
}
public function saveEmployee(Employee $employee)
{
	$data = array('name'=>$employee->name,'address'=>$employee->address);
	$id= (int)$employee->id;
	if ($id == 0)
	{
		$this->tableGateway->insert($data);
	}else {
             if ($this->getEmployee($id)) {
                 $this->tableGateway->update($data, array('id' => $id));
             } else {
	                 throw new \Exception('Employee id does not exist');
	      }
       }
			
}

Step 3 Create model file      Employee.php

public function getArrayCopy()
{
	return get_object_vars($this);
}
Step 4  Create edit.phtml file to display Data	edit.phtml
< ?php
            $form = $this->form;
            echo $this->form()->openTag($form);
            echo $this->formHidden($form->get('id'));
            echo $this->formRow($form->get('name'));
            echo $this->formRow($form->get('address'));
            echo $this->formSubmit($form->get('submit'));
            echo $this->form()->closeTag();
?>
Step 5  modify IndexController.php
public function editAction()
{
        $id = (int) $this->params()->fromRoute('id', 0);
    	$employee = $this->getEmployeeTable()->getEmployee($id);
    	$form = new RegistrationForm();
    	$form->bind($employee);
    	$request = $this->getRequest();
    	if ($request->isPost()) 
    	{
    	     $form->setData($request->getPost());
    	     if ($form->isValid()) {
    		$this->getEmployeeTable()->saveEmployee($employee);
    		// Redirect to list of albums
    		return $this->redirect()->toRoute('application');
    	     }
    	}
    	//die();
    	return array('id'=>$id,'form' => $form);
}
                                    

< First Last >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top