30/08/08

Populate a Zend_Form select with database returned data

The code speaks for itself:

$category = new Zend_Form_Element_Select('category');
$category->setLabel('Category')
         ->setRequired(true);
 
$table = new Category();
foreach ($table->fetchAll() as $c) {
    $category->addMultiOption($c->id, $c->name);
}

“By default, this element registers an InArray validator which validates against the array keys of registered options. You can disable this behavior by either calling setRegisterInArrayValidator(false), or by passing a false value to the registerInArrayValidator configuration key.”
Zend framework Manual.

If you want to order the way the categories are displayed you could do this in your model.

The database table:

CREATE TABLE IF NOT EXISTS `category` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(128) collate utf8_unicode_ci NOT NULL,
  `order` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=4 ;
 
INSERT INTO `category` (`id`, `name`, `order`) VALUES
(1, 'Category Three Order 3rd', 3),
(2, 'Category One Order 1st', 1),
(3, 'Category Two Order 2nd', 2);

The model:

< ?php
/** Zend_Db_Table_Abstract */
require_once 'Zend/Db/Table/Abstract.php';
 
class Category extends Zend_Db_Table_Abstract
{
    protected $_name = 'category';
 
    public function findForSelect()
    {
    	$select = $this->select();
    	$select->order('order');
    	return $this->fetchAll($select);
    }
}

Populate the select:

$category = new Zend_Form_Element_Select('category');
$category->setLabel('Category')
         ->setRequired(true);
 
$table = new Category();
foreach ($table->findForSelect() as $c) {
    $category->addMultiOption($c->id, $c->name);
}

28/08/08

Good OOP Practice / Zend framework

An interesting post on nabble yesterday by Matthew Weier O’Phinney on the difference between:

if ($this->getRequest()->isPost()) {
     // process
}
 
if ($this->_request->isPost()) {
     // process
}
 
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // process
}

Sometimes, he explains

“the obvious and simple solutions simply are not portable, or would circumvent custom logic the developer may need to utilize.”

You could use $this->_request

“However, if you ever modify getRequest() in your class or in a custom base controller class, then you may be accessing the wrong property or overriding necessary business logic. For this reason, we recommend using getRequest() to grab the request object.”

“Next, using isPost() is more portable than using $_SERVER['REQUEST_METHOD']. The reasons are that your web server may or may not populate this environment variable, and for testing. With testing, we allow you to specifically set the request method — $_SERVER is never modified in this case. This gives you the ability to test your applications without needing a web server involved.”

02/08/08

Web Development Tools

Xampp

Xampp is a one stop shop for an all in one package of everything you need to get started with your local development environment.

Download and install Xampp

Eclipse PDT

Eclipse is free.

Zend has an all in one package with the Zend Debugger plugin available for download.

Download Eclipse PDT from zend.com and install

Firefox

Download Firefox and install