intro
This article illustrates how to save form data with Zend_Db_Table_Row and Zend_Form. You can download the source code of this article from here.
1.Create bootstrap.php and config.ini
Create the file
Create the file
Please modify
In this article, we will use the following sql.
application/bootstrap.php
with the following content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php set_include_path('../library' . PATH_SEPARATOR . get_include_path()); define('APP_BASE', '../application'); define('CONFIG_PATH', APP_BASE . '/config.ini'); require_once 'Zend/Controller/Front.php'; require_once 'Zend/Layout.php'; require_once 'Zend/Db.php'; require_once 'Zend/Config/Ini.php'; require_once 'Zend/Db/Table/Abstract.php'; $layout = Zend_Layout::startMvc(); //$layout->getView()->baseUrl = '/yourBaseUrl'; $config = new Zend_Config_Ini(CONFIG_PATH, 'staging'); $params = $config->database->params->toArray(); $params['options'][Zend_Db::CASE_FOLDING] = Zend_Db::CASE_LOWER; $dbAdapter = Zend_Db::factory($config->database->adapter, $params); Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter); Zend_Controller_Front::run(APP_BASE . '/controllers'); |
Create the file
application/config.ini
with the following content.Please modify
database.params.*
properly.
1 2 3 4 5 6 |
[staging] database.adapter = pdo_mysql database.params.host = localhost database.params.username = db_user database.params.password = db_password database.params.dbname = db_name |
In this article, we will use the following sql.
1 2 3 4 5 6 7 8 9 |
CREATE TABLE profiles ( id integer auto_increment NOT NULL PRIMARY KEY, user_id integer NOT NULL, first_name varchar(100) NOT NULL, last_name varchar(100) NOT NULL, email varchar(200) NOT NULL, url varchar(200) NOT NULL ); |
2.Create DAO with Zend_Db_Table
Create the file
application/models/Profiles.php
with the following content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<?php require_once 'Zend/Db/Table/Abstract.php'; require_once 'Zend/Form.php'; class Profiles extends Zend_Db_Table_Abstract { protected $_name = 'profiles'; public function getForm() { $form = new Zend_Form(); $first_name = $form->createElement('text', 'first_name'); $first_name->setLabel('First Name') ->setRequired(true) ->addFilter('stringTrim') ->addValidator('stringLength', false, array(1,100)); $last_name = $form->createElement('text', 'last_name'); $last_name->setLabel('Last Name') ->setRequired(true) ->addFilter('stringTrim') ->addValidator('stringLength', false, array(1,100)); $email = $form->createElement('text', 'email'); $email->setLabel('E-Mail') ->setRequired(true) ->addFilter('stringTrim') ->addValidator('emailAddress', false) ->addValidator('stringLength', false, array(1,200)); $url = $form->createElement('text', 'url'); $url->setLabel('URL') ->setRequired(false) ->addFilter('stringTrim') ->addValidator('stringLength', false, array(1,200)); $form->addElements(array( $first_name, $last_name, $email, $url )); return $form; } public function findByUserId($user_id) { $select = $this->select()->where('user_id = ?', $user_id); return $this->fetchAll($select); } } |
3.Create Controller
Create the file
application/constorllers/IndexController.php
with the following content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?php require_once 'Zend/Controller/Action.php'; require_once APP_BASE . '/models/Profiles.php'; class IndexController extends Zend_Controller_Action { protected $_user_id = 1; protected function prepareForm($form) { $form->setAction($this->_request->getBaseUrl() . '/index/index') ->setMethod('post'); $submit = $form->createElement('submit', 'save'); $submit->setLabel('Save'); $form->addElement($submit); return $form; } public function indexAction() { $profiles = new Profiles(); $row =$profiles->findByUserId($this->_user_id)->current(); if (!$row) { $row = $profiles->createRow(array('user_id' => $this->_user_id)); } $form = $profiles->getForm(); if ($this->getRequest()->isPost()) { if ($form->isValid($_POST)) { // save profile $row->setFromArray($form->getValues()); $row->save(); } } else { // set default values $form->setDefaults($row->toArray()); } $this->view->form = $this->prepareForm($form); } } |
4.Create View
Create the file
application/views/scripts/index/index.phtml
with the following content.
1 2 |
<h1><?= $this->translate('Your Profile') ?></h1> <?= $this->form ?> |
5.Check
Let’s access the web server and check that the form data is stored.
References
The following is about generation of Zend_Form from table information.