Home > General > Implementing Add/Update/Delete/View Screens

Implementing Add/Update/Delete/View Screens

intro
This article illustrates how to use the add/update/delete/view action controller created in the previous article. You can download the source code from here.
You can see the screens from the demonstration site.

1.Create bootstrap.php and config.ini
Create the file application/bootstrap.php with the following content.
Please modify yourBaseUrl properly.

<?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 computers
(
    id integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    manufacturer varchar(100) NOT NULL,
    model varchar(100)        NOT NULL,
    spec varchar(200)         NOT NULL
);
2.Create Models
Create the file application/models/Computers.php with the following content.

<?php
require_once 'Zend/Db/Table/Abstract.php';
 
class Computers extends Zend_Db_Table_Abstract
{
    protected $_name = 'computers';
}

Create the file application/models/forms/ComputerForm.php with the following content.

<?php
require_once 'Zend/Form.php';
 
class ComputerForm extends Zend_Form
{
    public function __construct()
    {
        parent::__construct();
        $manufacturer = $this->createElement('text', 'manufacturer');
        $manufacturer->setLabel('manufacturer')
                     ->setRequired(true)
                     ->addFilter('stringTrim')
                     ->addValidator('stringLength', false, array(1,100));
        $model = $this->createElement('text', 'model');
        $model->setLabel('model')
              ->setRequired(true)
              ->addFilter('stringTrim')
              ->addValidator('stringLength', false, array(1,100));
        $spec = $this->createElement('text', 'spec');
        $spec->setLabel('spec')
            ->setRequired(true)
            ->addFilter('stringTrim')
            ->addValidator('stringLength', false, array(1,200));
 
        $this->addElements(array(
            $manufacturer, $model, $spec
        ));
    }
}
3.Create Controller
Create the file application/constorllers/ComputerController.php with the following content.
This class extends My_Controller_Simple created in the previous article.

<?php
require_once 'My/Controller/Simple.php';
require_once APP_BASE . '/models/Computers.php';
require_once APP_BASE . '/models/forms/ComputerForm.php';
 
class ComputerController extends My_Controller_Simple
{
    protected $_session_name = "Computer";
    protected $_table_class = "Computers";
    protected $_form_class = "ComputerForm";
 
    public function preDispatch()
    {
        $this->view->name = "computer";
    }
 
    public function indexAction()
    {
        $this->_forward('list');
    }
}
4.Create Views
Create the file application/views/scripts/computer/list.phtml with the following content.

 
<h1><?= $this->translate($this->name . '.title.list') ?></h1>
 
<?php if (!$this->list) : ?>
<span><?= $this->translate($this->name . 'label.no_data') ?></span>
<?php else : ?>
<a href="<?=$this->url(array('action'=>'add'))?>">
<?= $this->translate($this->name . '.label.new') ?></a>
<table>
<?php   $this->header = true; ?>
<?php   foreach($this->list as $row) : ?>
<?php     $values = $row->toArray(); ?>
<?php     if ($this->header) : ?>
<tr>
<?php       foreach($values as $key => $value) : ?>
<th><?= $this->translate($key) ?></th>
 
<?php       endforeach; ?>
<th><?= $this->translate('detail') ?></th>
<th><?= $this->translate('edit') ?></th>
<th><?= $this->translate('delete') ?></th>
 
<?php       $this->header = false; ?>
</tr>
 
<?php     endif; ?>
<tr>
<?php       foreach($values as $key => $value) : ?>
<td><?= $this->escape($value) ?></td>
 
<?php       endforeach; ?>
<td><a href="<?=$this->url(array('action'=>'detail'))?>?id=<?=$row->id?>">
  <?= $this->translate('detail') ?></a></td>
<td><a href="<?=$this->url(array('action'=>'update'))?>?id=<?=$row->id?>">
  <?= $this->translate('edit') ?></a></td>
<td><a href="<?=$this->url(array('action'=>'delete'))?>?id=<?=$row->id?>">
  <?= $this->translate('delete') ?></a></td>
</tr>
 
<?php   endforeach; ?>
</table>
 
<span><?= $this->translate($this->name . '.label.max') ?></span>
<?php endif; ?>

Create the file application/views/scripts/computer/detail.phtml with the following content.

 
<h1><?= $this->translate($this->name . '.title.detail') ?></h1>
 
<?php if ($this->values) : ?>
<dl>
<?php foreach($this->values as $key => $value) : ?>
<dt><?= $this->translate($key) ?></dt>
<dd><?= $this->escape($value) ?></dd>
 
<?php endforeach; ?>
</dl>
 
<?php endif; ?>
<?php if ($this->form) : ?>
<?= $this->form ?>
<?php endif; ?>

Create the file application/views/scripts/computer/form.phtml with the following content.

 
<h1><?= $this->translate($this->name . '.title.' . $this->action) ?></h1>
 
<?= $this->form ?>

Create the file application/views/scripts/computer/finish.phtml with the following content.

 
<h1><?= $this->translate($this->name . '.title.finish.' . $this->action) ?></h1>
 
<a href="<?=$this->url(array('action'=>'list'))?>">
<?= $this->translate('back') ?></a>

5.Check
Access the web server and check add/update/delete/view screens.
History
Date Content
2008/4/26 Views were modified to use URL helper.

Comments:0

Comment Form
Remember personal info

Trackbacks:0

Trackback URL for this entry
http://www.oplabo.com/article/25/trackback
Listed below are links to weblogs that reference
Implementing Add/Update/Delete/View Screens from Open Programming Laboratory

Home > General > Implementing Add/Update/Delete/View Screens

Japanese
Search
Feeds

Return to page top