- 2008-04-20 (Sun) 7:00
- Zend_Form
intro
This article shows multi page add/update/delete/view Action Controller. It works only on 1:1 relation.
1.Create Controller
Create the file
library/My/Controller/MultiPage.php with the following content.
<?php require_once 'My/Controller/Simple.php'; require_once 'My/Form/MultiPage.php'; require_once 'Zend/Session/Namespace.php'; class My_Controller_MultiPage extends My_Controller_Simple { protected $_session_name = 'my_controller_multipage'; protected $_table_form_classes = array(); protected $_form = null; public function getForm() { if (!$this->_form) { $form = new My_Form_MultiPage($this->_session_name); foreach ($this->_table_form_classes as $table_class => $form_class) { $form->addForm($table_class, new $form_class()); } $this->_form = $form; } return $this->_form; } protected function prepareForm($form, $label, $back) { if (method_exists($form, 'prepareForm')) { return $form->prepareForm($this->_getFormActionUrl(), $label); } return parent::prepareForm($form, $label, $back); } public function getTable() { if (!$this->_form) { $this->_form = $this->getForm(); } $table_class = $this->_form->getCurrentFormName(); if ($table_class) { return new $table_class(); } return null; } protected function _clearSession() { parent::_clearSession(); $this->getForm()->clear(); } protected function _handleNextEvent() { $session = $this->getSession(); $form = $this->getForm(); $action_label = $this->_request->getActionName(); if ($form->isValid($_POST)) { if ($form->isConfirm()) { $this->view->values = $form->getValues(); $this->view->form = $form->prepareForm($this->_getFormActionUrl(), $action_label); return 'detail'; } } $this->view->title = $form->getCurrentFormName(); $this->view->form = $form->prepareForm($this->_getFormActionUrl(), $action_label); return 'form'; } protected function _handleBackEvent() { $form = $this->getForm(); $form->back(); $this->view->form = $form->prepareForm($this->_getFormActionUrl(), 'next'); return 'form'; } protected function _executeFind($table, &$id) { $row = $table->find($id)->current(); if ($row) { $id = array(); return $this->_getValues($row, $id); } return null; } protected function _executeInsert($table, $values) { $dependant = false; foreach ($values as $table_name => $table_values) { $table = new $table_name(); if ($dependant) { $this->_makeReference($table, $prev_table, $prev_id, $table_values, $prev_values); } $prev_id = $table->insert($table_values); $prev_table = $table_name; $prev_values = $table_values; $dependant = true; } } protected function _executeUpdate($table, $id, $values) { $dependant = false; foreach ($values as $table_name => $table_values) { $table = new $table_name(); $current_id = $id[$table_name]; $row = $table->find($current_id)->current(); if (!$row) { $this->_makeReference($table, $prev_table, $prev_id, $table_values, $prev_values); $row = $table->createRow(); } $row->setFromArray($table_values); $row->save(); $prev_id = $current_id; $prev_table = $table_name; $prev_values = $table_values; $dependant = true; } } protected function _executeDelete($table, $id) { foreach ($id as $table_name => $current_id) { $table = new $table_name(); $row = $table->find($current_id)->current(); $row->delete(); } } protected function _getValues($row, &$ids) { $values = array(); $ids = array(); $dependant = false; foreach ($this->_table_form_classes as $table_class => $form_name) { if ($depandant) { $row = $row->findDependentRowset($table_class)->current(); } if ($row) { $values[$table_class] = $row->toArray(); $ids[$table_class] = $this->_getRowId($table_class, $row); $depandant = true; } else { $depandant = false; } } return $values; } protected function _makeReference($table, $ref_table_name, $ref_row_id, &$values, &$ref_values) { $referenceMap = $table->getReference($ref_table_name); for ($i = 0; $i < count($referenceMap['columns']); $i++) { $column = $referenceMap['columns'][$i]; $ref_column = $referenceMap['ref_columns'][$i]; if (isset($ref_values[$ref_column])) { $values[$column] = $ref_values[$ref_column]; } else { $values[$column] = $ref_row_id; } } } protected function _getRowId($table_class, $row) { if (isset($row->id)) { return $row->id; } return null; } }
Next
Next article will show how to use this controller.
History
| Date | Content |
|---|---|
| 2008/4/20 | Published |
| 2008/4/26 | The controller was modified for modification of My_Form_MultiPage. |
- Newer: Implementing Multi Page Add/Update/Delete/View Screens
- Older: Using Multi Page Form that extends Zend_Form
Comments:2
- Nicolas 08-05-09 (Fri) 17:12
-
hi, i think the download link is not the good one: the lib\My\Controller folder doesn’t exist.
- oplabo 08-05-09 (Fri) 23:03
-
Hello
You can download the library folder of controllers and multi page form
from the following article.
Trackbacks:0
- Trackback URL for this entry
- http://www.oplabo.com/article/28/trackback
- Listed below are links to weblogs that reference
- Creating Multi Page Add/Update/Delete/View Action Controller from Open Programming Laboratory