- 2008-04-15 (Tue) 7:00
- Zend_Db
application/bootstrap.php with the following content.
<?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.
[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.
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 ); INSERT INTO profiles (user_id, first_name, last_name, email, url) VALUES (1, 'Myfirstname', 'Mylastname', 'my@emailaddress', 'http://myurl/my/page');
application/models/Profiles.php with the following content.
<?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); } }
application/constorllers/IndexController.php with the following content.
<?php require_once 'Zend/Controller/Action.php'; require_once 'Zend/Session/Namespace.php'; require_once APP_BASE . '/models/Profiles.php'; class IndexController extends Zend_Controller_Action { protected $_user_id = 1; protected function prepareForm($form, $label, $back) { $form->setAction($this->_request->getBaseUrl() . '/index/index') ->setMethod('post'); $submit = $form->createElement('submit', $label); $submit->setLabel($label); $form->addElement($submit); if ($back) { $back = $form->createElement('submit', 'back'); $back->setLabel('Back'); $form->addElement($back); } return $form; } public function indexAction() { // get default form values $session = new Zend_Session_Namespace('index'); $profiles = new Profiles(); $form = $profiles->getForm(); $label = 'confirm'; $back = false; if ($this->getRequest()->isPost()) { if (isset($_POST['confirm'])) { if ($form->isValid($_POST)) { // confirm $session->values = $form->getValues(); $this->view->profile = $form->getValues(); $form = new Zend_Form(); $label = 'save'; $back = true; } } else if (isset($_POST['save'])) { // save profile $row =$profiles->findByUserId($this->_user_id)->current(); if (!$row) { $row = $profiles->createRow(array('user_id' => $this->_user_id)); } $values = $session->values; $session->values = null; $row->setFromArray($values); $row->save(); return $this->render('finish'); } else if (isset($_POST['back'])) { // restore values if (isset($session->values)) { $form->setDefaults($session->values); $session->values = null; } } } else { // set default values $values = array(); $row =$profiles->findByUserId($this->_user_id)->current(); if (isset($session->values)) { $values = $session->values; } else if ($row) { $values = $row->toArray(); } $form->setDefaults($values); } $this->view->form = $this->prepareForm($form, $label, $back); } }
application/views/scripts/index/index.phtml with the following content.
<h1><?= $this->translate('Your Profile') ?></h1> <?php if ($this->profile) : ?> <dl> <?php foreach($this->profile as $key => $value) : ?> <dt><?= $this->translate($key) ?></dt> <dd><?= $this->escape($value) ?></dd> <?php endforeach; ?> </dl> <?php endif; ?> <?= $this->form ?>
Create the file application/views/scripts/index/finish.phtml with the following content.
<h1><?=$this->translate('Profile Edited')?> <h1> <a href="<?= $this->baseUrl?>/index/index"><?= $this->translate('Back') ?></a>
- Newer: Creating Add/Update/Delete/View Action Controller
- Older: Creating Fields Comparison Validator for Zend_Form
Comments:2
- lmcosorio 08-04-24 (Thu) 8:05
-
Hi,
I’m getting the error:
Fatal error: Call to a member function getMessage() on a non-object
on error.phtmlthanks
- oplabo 08-04-24 (Thu) 13:29
-
Sorry. I’ve modified error controller(please see history of Error Handling of Zend Framework).
I updated the source code of this article.
And I guess It’s 404 error.
Did you change properly baseUrl of bootstrap.php?Thanks
Trackbacks:0
- Trackback URL for this entry
- http://www.oplabo.com/article/23/trackback
- Listed below are links to weblogs that reference
- Creating Confirmation Screen using Zend_Session from Open Programming Laboratory