Month: January 2009

Using Zend_Form, Zend_Config_Ini And Zend_Db_Table

Ok I am going to write a little bit about Zend_Form.
Yes I know a lot of small tutorials already exist but this one can
show you another trick 🙂

My prefered way of using Zend_Form is to use it with a config.ini file.
It gives you a nice overview of special settings in a structured way. I love that.

One thing the manual does not explain is how to use it in addition with for instance dynamic population/value setting of a Zend_Form_Element object.

Say you have a select field that needs population. You cannot do that manually in the config file every time the database gets updated. There should be a better way. And actually there is.

Small example stripped to the bare minimum follows:

formConfig.ini

 [time]

time.action = "/time/save"
time.method = "post"
time.accept-charset = "utf-8"

time.elements.deliverytime_id.type = "select"
time.elements.deliverytime_id.options.label = "Delivery Time: "

time.elements.createsubmit.type = "submit"
time.elements.createsubmit.options.label = "create new"

Above we have a regular config.ini file ready to be parsed with Zend_Config_Ini.
For more information on how to use this with Zend_Form see Using a Zend_Config object

Time.php

<?php
class Time extends Zend_Db_Table {
    protected $_name = 'time';
}

Time.php is the Model for our time Table. This way we can easily do table manipulation without writing
any sql. This for potential portability to another database and your convencience.

TimeController.php

<?php
class TimeController extends Zend_Controller_Action {
    protected $time;
    protected $formTime;
    
    public function indexAction() {
        $this->view->formTime = $this->formTime;
    }
    
    public function init() {
        $this->time = new Time();
        $formConfig = new Zend_Config_Ini(APP_CONFIG.'/formCms.ini', 'time');
        $this->formTime = new Zend_Form($formConfig->time);
        $this->setupDeliveryTimesInFormSelect();
    }
    
    private function setupDeliveryTimesInFormSelect() {
        $select = $this->formTime->getElement('deliverytime_id');
    
        $deliveryTimes = $this->Time->fetchAll()->toArray();
        foreach($deliveryTimes as $timeOption) {
            $select->addMultiOption($timeOption['id'], $timeOption['time']);
        }
    }
}

This is a regular Controller. This is just stripped example code just to make you understand everything with more clarity. The key is in setupDeliveryTimesInFormSelect(). Here we get the form element from our $this->formTime.  It was already generated in the init() function.
Because all things are static in the config file we are going to populate it with options using the addMultiOption(). The values that are being used in the foreach loop are returned by a Zend_Db_Table object generated in Time.php.

The result of this code if you render your index view is a form with a submit button and a select box with options/values pairs set straight from a database. Because it is a select,  the submitted values are compared to the values in the form. Other form element types like text etc should have their submitted values validated and or filtered. You can find plenty of examples on how to set those options in the config.ini file on the net.

Happy Coding

Nick Belhomme
Your PHP5 Zend Certified Engineer

phpGG Frontend Special was da bomb

Hello again,

Yesterday I attended the phpGG Frontend Special  , phpGG first great conference and I must say it was very good organized. I had to be on the road for more than four hours but it was worth every second of it.

Like on most conferences you were welcomed with your preferable drink, for the record mine is orange juice. I saw a lot of net workers drinking milk. 🙂 After some networking/ chitchatting  the presentations began right on time.

The presentations were professionally introduced by Stefan Koopmanschap and Kana Yeh and were all on the topic of front end.

The first presentation was from Bram Veenhof about MS Silverlight. It was interesting but not really PHP oriented.

The second one speaker was a charismatic speaker with great humor and very much knowledge about the Adobe Flex and Air platforms, Mihai Corlan.

He was able to grab my interest with live demos and coding examples. In a matter of minutes he had a working application using Flex and PHP. Awesome. I really see this technology used in one of my next CMS projects. He explained about datagrids and how flex uses them to interact with a Mysql Database through PHP on a different or same server.

After so much interesting information it was time for a lunch break and I spoke with Mihai and he explained first hand the new technology behind the Text Layout Framework in Flex 3.2 or AIR 1.5. It looks great, I urge you to check it out if you are interested in anything Flash, Flex or Air related.

After a sandwich or two it was time to have presentations introducing some user groups.  Juliette Reinders Folmer introduced  PHP Women ,  Sander van Lambalgen introduced Fronteers and of course the group phpGG, frontend special organizer itself, was introduced by Stefan. He even threw in some surprise gifts. Including several elephpants, ZCE study guides and even two tickets to the upcoming PHP Conference in London.

Now my favorite presentation took of. Ibuildings own Javascript evangelist Boy Baukema. He talked about of course Javascript and what made it interesting for me was the fact that he made comparisons between upcoming new features in PHP 5.3 and already existing features in Javascript like Lambda functions and Closure. I will post a link towards the presentation soon.

Robert Jan Verkade founder of Eend.nl  talked about why PHP developers should be nice to frontend and visa versa. Personally I do not have any bad experiences on this subject but I can imagine a lot of developers do. He said communication is key. Isn’t this so true for all parts of life! But if communication is faulty on your work floor he spoke about some key principles what you can do to minimize conflicts.

And that is a wrap.  I would like to thank all the speakers and the organization phpGG for  making my Saturday a very good one. I hope to see more of these days organized in the future. If not for the great presentations, than for the wonderful networking you can do. It is always fun to see new and familiar faces.
I hope to be able to post links to the presentations somewhere this week. In the meantime you can checkout Flickr, there are some pictures definitely going to be posted there under the tag: phpggfs

Warm winter greetings from Belgium,
Nick Belhomme
Your PHP5 Zend Certified Engineer

[UPDATE]

Ok as promised  here are the slides:
* Flex and AIR for PHP programmers by Mihai Corlan

*  Javascript: 8 Reasons every PHP developer should love it by Boy Baukema

* phpWomen