<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ryan's Blog &#187; php</title>
	<atom:link href="http://www.rmauger.co.uk/topics/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rmauger.co.uk</link>
	<description>Randomness will get you everywhere.</description>
	<lastBuildDate>Fri, 03 Sep 2010 13:02:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Speaking at the first ever PHP South West</title>
		<link>http://www.rmauger.co.uk/2010/09/speaking-at-the-first-ever-php-south-west/</link>
		<comments>http://www.rmauger.co.uk/2010/09/speaking-at-the-first-ever-php-south-west/#comments</comments>
		<pubDate>Fri, 03 Sep 2010 12:48:14 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[php community]]></category>
		<category><![CDATA[phpsw]]></category>
		<category><![CDATA[talks]]></category>

		<guid isPermaLink="false">http://www.rmauger.co.uk/?p=140</guid>
		<description><![CDATA[Last Wednesday (1st September 2010) I had the privilege of giving the first ever talk at PHPSW, a new PHPUG for developers in the South West, and Wales. PHPSW has been put together kindly by Stuart Herbert, a great guy who I have had the pleasure of meeting briefly at past conferences and seeing give his talks [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Ryan gives the first PHPSW talk by akrabat, on Flickr" href="http://www.flickr.com/photos/akrabat/4953777240/"><img class="alignright" src="http://farm5.static.flickr.com/4083/4953777240_94c14578ec_m.jpg" alt="Ryan gives the first PHPSW talk" width="240" height="160" /></a>Last Wednesday (1st September 2010) I had the privilege of giving the first ever talk at PHPSW, a new PHPUG for developers in the South West, and Wales. PHPSW has been put together kindly by <a title="Stuart Herbert" href="http://www.stuartherbert.com/">Stuart Herbert</a>, a great guy who I have had the pleasure of meeting briefly at past conferences and seeing give his talks on <a title="Twittex - Stuart Herbert" href="http://www.slideshare.net/stuartherbert/twittex-from-idea-to-live-in-seven-days-presentation" target="_blank">Twittex</a> and <a title="Stuart Herbert - Living With Frameworks" href="http://www.slideshare.net/stuartherbert/living-with-frameworks">Living With Frameworks</a>.</p>
<p>Our venue for the evening was Bush House, a nice place with perfectly sized meeting rooms for the 15 guys who attended.</p>
<p>The talk I gave, was a rehersal for my talk at <a href="http://conference.phpnw.org.uk/phpnw10/">PHPNW10</a>, which is entitled &#8216;<a title="My talk at PHPNW10" href="http://conference.phpnw.org.uk/phpnw10/schedule/zend-framework-getting-to-grips/">Zend Framework: Getting to grips</a>&#8216;, and is aimed at  developers who have made it past the first steps of Zend Framework, and are moving on into the realms of developing their understanding, and exploring the quirks and nuances.</p>
<p><img class="alignleft" src="http://farm5.static.flickr.com/4150/4953185753_c0e607a30b_m.jpg" alt="The audience!" width="240" height="172" /></p>
<p>My talk went down well (at-least, nobody fell asleep), besides a few mistakes on my part when putting my slides together, and gave me some valuable experience to ensure that things go smoothly at PHPNW10, and a huge thanks has to be said to everyone there once more for all the feedback, you&#8217;ve given me a much needed confidence boost!</p>
<p>Afterwards, it was time to head downstairs to <a id="link_C_2" href="pplink&amp;ei=JOqATO7SAoOTjAfVydXQDA&amp;sig2=_yJcM1lu4s-goPobpXLRfw">Arnolfini‎</a><img src="http://maps.gstatic.com/intl/en_ALL/mapfiles/transparent.png" alt="" />, where we all got to mingle and socialise, and discuss all things geeky and wonderful, which is quite a refreshing experience having only experienced such pleasures when attending conferences.</p>
<p>It was also a pleasure to see a few guys who I have encountered over time on IRC, and also to see and appearance from <a title="Rob Allen" href="http://akrabat.com/">Rob Allen</a>, author of <a title="Zend Framework In Action" href="http://www.zendframeworkinaction.com">Zend Framework in Action</a>, whom I have come to regard as a friend over the years from both IRC and bumping into at conferences over the last few years, and was kind enough to give me permission to use the pics he took for this post. Another pleasant surprise was the presence of <a href="http://deglos.com/">Marcus Degalos</a>, who will be hosting the <a title="Framework Shootout, PHPNW10" href="http://conference.phpnw.org.uk/phpnw10/schedule/framework-shootout/">framework shootout at PHPNW10</a>, and is a Bristol resident, so hopefully we can expect to see much more of him at future meetings.</p>
<p>If you&#8217;re in the south west or wales, and you&#8217;re a php developer, come and <a title="PHPSW mailing list" href="http://groups.google.com/group/phpsw">sign up to the mailing list</a>, and get yourself along to the next meeting. See you there! The next scheduled PHPSW meeting will be on Wednesday 13th October 2010 (Venue TBA), so we can pick apart and share our experiences of PHPNW10!</p>
<p>(Before anyone asks to see the slides, they will be available after PHPNW10)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rmauger.co.uk/2010/09/speaking-at-the-first-ever-php-south-west/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Keeping your HTML valid with Zend Framework, Tidy and Firebug</title>
		<link>http://www.rmauger.co.uk/2010/01/keeping-your-html-valid-with-zend-framework-tidy-and-firebug/</link>
		<comments>http://www.rmauger.co.uk/2010/01/keeping-your-html-valid-with-zend-framework-tidy-and-firebug/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 13:49:09 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[html validation]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.rmauger.co.uk/?p=119</guid>
		<description><![CDATA[With Zend Framework there is an easy way to ensure that you always create valid HTML in your applications. This involves the use of a simple Front Controller Plugin, and the php Tidy component. Valid HTML is important for a great many reasons, the most important of which is ensuring consistency across all of your [...]]]></description>
			<content:encoded><![CDATA[<p>With Zend Framework there is an easy way to ensure that you always create valid HTML in your applications. This involves the use of a simple Front Controller Plugin, and the php Tidy component.</p>
<p>Valid HTML is important for a great many reasons, the most important of which is ensuring consistency across all of your visitors browsers. The first step to making sure that your site appears correctly on all the browsers is to ensure that your HTML is valid. Even if the goons at Microsoft continue to ignore the standards and do their own thing, if you at least ensure your html passes validation, then fixing things for Internet Explo(r|it)er of all its versions is a far easier task, and usually possible with a few simple extra styling rules in your CSS.<br />
<span id="more-119"></span></p>
<h2>What is a front controller plugin</h2>
<p>A front controller plugin is like an observer for the front controller. It provides several events which can be hooked at the various stages of the dispatch cycle. These events are</p>
<ul>
<li>routeStartup: Called before routing takes place</li>
<li>routeShutdown: Called after routing has occured, but before the dispatcher is invoked</li>
<li>dispatchLoopStartup: Called before the dispatchloop begins, essentially the same as routeShutdown</li>
<li>preDispatch: Called before an action is dispatched (also before preDispatch of your controller, and after its init)</li>
<li>postDispatch: Called after an action is dispatched</li>
<li>dispatchLoopShutdown: Called when the dispatching is complete</li>
</ul>
<p>A simple example of a Front Controller plugin is to automatically change the layout based upon the active module.</p>
<p>For this, you may do something as follows:</p>
<pre>class Lupi_Controller_Plugin_ModuleLayout extends Zend_Controller_Plugin_Abstract
{
    public function routeShutdown(Zend_Controller_Request_Abstract $request) {
        // Changes the Layout based on the module name
        $modulename = $request-&gt;getModuleName();
        $layout = Zend_Layout::getMvcInstance();
        $layout-&gt;setLayoutPath("../application/modules/{$modulename}/layouts")
                     -&gt;setLayout($modulename);
    }
}</pre>
<h2>What is Tidy</h2>
<blockquote><p>Tidy is a binding for the Tidy HTML clean and repair utility which    allows you to not only clean and otherwise manipulate HTML documents,    but also traverse the document tree.[source:<a title="PHP Tidy Extension introduction" href="http://uk.php.net/manual/en/intro.tidy.php" target="_blank">http://uk.php.net/manual/en/intro.tidy.php</a>]</p></blockquote>
<p>The Tidy extension for PHP provides numerous functions for assisting a developer with coding performant, valid, and accessible HTML.</p>
<p>It does this through a variety of methods, but generally it works in 3 steps. configure, parse and repair.</p>
<pre>class Lupi_Filter_Tidy implements Zend_Filter_Interface
{
    /**
     * @var tidy
     */
    protected $_tidy;

    /**
     * @var tidy
     */
    protected $_encoding = 'UTF-8';

    /**
     * @var array
     */
    protected $_config = array('indent' =&gt; true,
                                         'output-xhtml' =&gt; true,
                                         'wrap' =&gt; false,
                                         'show-body-only' =&gt; true);

    /**
     * Filter the content with Tidy.
     *
     * @return string
     */
    public function filter($content)
    {
        $tidy = $this-&gt;getTidy($content);
        $tidy-&gt;cleanRepair();
        return (string) $tidy;
    }

    /**
     * Gets the Tidy object
     */
    public function getTidy($string)
    {
        if (!is_string($string)) {
            throw new InvalidArguementException('Expected string, got: ' . get_type($string));
        }

        if (null === $this-&gt;_tidy) {
            $this-&gt;_tidy = new tidy();
        }

        $this-&gt;_tidy-&gt;parseString($string, $this-&gt;_config, $this-&gt;_encoding);
        return $this-&gt;_tidy;
    }
}</pre>
<p>The above example is a simple filter, which will correct and escape a HTML fragment, suitable for use in a Zend_Form where you wish to accept user input. (NOTE: Though Tidy does a damn  good job of cleaning up input, what it does not do is guarantee that the output will be XSS safe).</p>
<p>HTML Tidy has a huge number of options available, one of my favourites being &#8220;bare&#8221; which removes MS-Word specific attributes and styling which will ruin your output. (No, no matter how many times you tell your clients not to paste straight from word into your editor, they will still do it!). I won&#8217;t even try to explain all the options here, so instead, find a <a title="A complete list of HTML Tidy options" href="http://tidy.sourceforge.net/docs/quickref.html" target="_blank">full list of the options available for tidy</a> on its sourceforge site.</p>
<h2>Using Tidy with a Front Controller Plugin</h2>
<p>Ok, So you can use tidy for filtering user input, what about using it to effectivly clean my documents and ensure my output is always valid?</p>
<p>In the previous example, I set the &#8220;show-body-only&#8221; option, which will force the tidy component to only output the body of the document. This is needed because tidy would have added a doctype, html, head and body tags around the user input. this does set to an automatic option, and should only return the complete document if it detected a body tag, but why risk the user sticking a body tag in there?</p>
<p>For the next example, we have a Front controller plugin, which allows us to filter the output html of our application, so that we know we always have valid output.</p>
<pre>&lt;?php

class Lupi_Controller_Plugin_TidyOutput extends Zend_Controller_Plugin_Abstract
{
    /**
     * @var tidy|null
     */
     protected $_tidy;

    /**
     * @var array
     */
    protected static $_tidyConfig = array('indent'            =&gt; true,
                                          'indent-attributes' =&gt; true,
                                          'output-xhtml'      =&gt; true,
                                          'drop-proprietary-attributes' =&gt; true,
                                          'wrap'              =&gt; 120,
                                          );
    /**
     * @var string
     */
    protected static $_tidyEncoding = 'UTF8';

    public static function setConfig(array $config)
    {
        self::$_tidyConfig = $config;
    }

    public static function setEncoding($encoding)
    {
         if (!is_string($encoding)) {
             throw new InvalidArgumentException('Encoding must be a string');
         }
         self::$_tidyEncoding = $encoding;
    }

    protected function getTidy($string = null)
    {
        if (null === $this-&gt;_tidy) {
            if (null === $string) {
                $this-&gt;_tidy = new tidy();
            } else {
                $this-&gt;_tidy = tidy_parse_string($string,
                                                 self::$_tidyConfig,
                                                 self::$_tidyEncoding);
            }
        }
        return $this-&gt;_tidy;
    }

    public function dispatchLoopShutdown()
    {
        $response = $this-&gt;getResponse();
        $tidy     = $this-&gt;getTidy($response-&gt;getBody());
        $tidy-&gt;cleanRepair();
        $response-&gt;setBody((string) $tidy);
    }
}
</pre>
<p>When you are using this, it is a good idea where possible to use full page static caching, so your not fixing the same errors over and over again!</p>
<h2>Instant feedback with Firebug + Firephp for assisting development</h2>
<p>Ok, so now we have valid HTML, thanks to a filter. How does this help with actual development? well in short, it doesn&#8217;t, as we have no feedback about what its actually fixed. So onto the next step, getting some nice reporting, in a real handy manner. For this, we will use FirePHP, so that all the information we need is sent to the console on every request. This information can even include automated accessibility testing (really handy for government funded work, which usually has requirements on meeting accessibility standards).</p>
<p>So, firstly you need to set up your FirePHP Logger, this is a simple task, simply add the following method to your applications bootstrap:</p>
<pre>protected function _initWildFire()
{
    //Don't use in production!
    if (APPLICATION_ENV != 'development') {
       return;
    }
    $this-&gt;bootstrap('db');
    $db = Zend_Db_Table::getDefaultAdapter();
    $profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
    $profiler-&gt;setEnabled(true);
    $db-&gt;setProfiler($profiler);
    $writer = new Zend_Log_Writer_Firebug();
    $logger = new Zend_Log($writer);
    Zend_Registry::set('logger', $logger);
}
</pre>
<p>This simply sets up a logger, and also does something else useful, it adds a Profiler to the default database adapter, which will log all your queries for you. A little out of the scope of this post, but useful, so I left it in there for you.</p>
<p>The last 3 lines are really the important bit, they set up the write and log component which we will be using to send messages to FirePHP, so we can see validation errors and warnings in our FireBug console! Setting the logger in the registry here is also handy, so we can actually get our logger from anywhere without any hassle. you may also return it from the init method, and use the invoke args to get it.</p>
<p>Now for the really useful bit, the plugin itself.</p>
<pre>class Lupi_Controller_Plugin_TidyOutput extends Zend_Controller_Plugin_Abstract
{
    /**
     * @var tidy|null
     */
    protected $_tidy;

    /**
     * @var array
     */
    protected static $_tidyConfig = array('indent'            =&gt;true,
                                          'indent-attributes' =&gt; true,
                                          'output-xhtml'      =&gt; true,
                                          'drop-proprietary-attributes' =&gt; true,
                                          'wrap'              =&gt; 120,
    );

    protected static $_diagnose = true;

    /**
     * @var string
     */
    protected static $_tidyEncoding = 'UTF8';

    /**
     * Switch diagnosing HTML mode
     */
    public static function setDiagnose($diagnose = true)
    {
        self::$_diagnose = (bool) $diagnose;
    }

    public static function setConfig(array $config)
    {
        self::$_tidyConfig = $config;
    }

    public static function setEncoding($encoding)
    {
        if (!is_string($encoding)) {
            throw new InvalidArgumentException('Encoding must be a string');
        }
        self::$_tidyEncoding = $encoding;
    }

    protected function getTidy($string = null)
    {
        if (null === $this-&gt;_tidy) {
            if (null === $string) {
                $this-&gt;_tidy = new tidy();
            } else {
                $this-&gt;_tidy = tidy_parse_string($string,
                                                 self::$_tidyConfig,
                                                 self::$_tidyEncoding);
            }
        }
        return $this-&gt;_tidy;
    }

    public function dispatchLoopShutdown()
    {
        $response = $this-&gt;getResponse();
        $tidy     = $this-&gt;getTidy($response-&gt;getBody());

        if ('development' === APPLICATION_ENV) {
            if (true === self::$_diagnose ) {
                $tidy-&gt;diagnose();
                $lines = array_reverse(explode("\n", $tidy-&gt;errorBuffer));
                array_shift($lines);
                foreach ($lines as $line) {
                    Lupi_Logger::log($line, Zend_Log::INFO);
                }
            }
        }
        $tidy-&gt;cleanRepair();
        $response-&gt;setBody((string) $tidy);
    }
}
</pre>
<p>So, hows it work? Well its essentially the same as the previous plugin, except I have added a section to the dispatchLoopShutdown method, and added a static method to enable / disable the logging output (sometimes it can get in the way!).</p>
<p>The reason for splitting the diagnosis output by line and sending it over multiple log calls, is because the Firebug console will not respect the newline characters, and instead tried to display it all on one line, making it hard to read. splitting it over multiple entries makes things much tidier. reversing the array also makes things a little easier to read!</p>
<p>In closing, heres some examples of the output for you:</p>
<div id="attachment_124" class="wp-caption alignleft" style="width: 310px"><a href="http://www.rmauger.co.uk/wp-content/uploads/2010/01/noerrors.jpg"><img class="size-medium wp-image-124 " title="noerrors" src="http://www.rmauger.co.uk/wp-content/uploads/2010/01/noerrors-300x123.jpg" alt="An example of the Tidy output for valid xhtml" width="300" height="123" /></a><p class="wp-caption-text">An example of the Tidy output for valid xhtml</p></div>
<div id="attachment_123" class="wp-caption alignleft" style="width: 310px"><a href="http://www.rmauger.co.uk/wp-content/uploads/2010/01/errors.jpg"><img class="size-medium wp-image-123 " title="errors" src="http://www.rmauger.co.uk/wp-content/uploads/2010/01/errors-300x123.jpg" alt="An example of the Tidy output with some errors in it" width="300" height="123" /></a><p class="wp-caption-text">An example of the Tidy output with some errors in it</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.rmauger.co.uk/2010/01/keeping-your-html-valid-with-zend-framework-tidy-and-firebug/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Massive zend framework poster \ cheatsheet</title>
		<link>http://www.rmauger.co.uk/2009/08/massive-zend-framework-poster-cheatsheet/</link>
		<comments>http://www.rmauger.co.uk/2009/08/massive-zend-framework-poster-cheatsheet/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 11:14:39 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.rmauger.co.uk/2009/08/massive-zend-framework-poster-cheatsheet/</guid>
		<description><![CDATA[Massive zend framework poster \ cheatsheet Originally uploaded by bittarman The other day, a wonderful thing arrived in the post, fresh from Björn Schotte of mayflower. The poster covers many of the commonly used components, and their parameters. I have had it on the wall for about 2 weeks now, and it has become something [...]]]></description>
			<content:encoded><![CDATA[<div style="float: right; margin-left: 10px; margin-bottom: 10px;">
<a href="http://www.flickr.com/photos/bittarman/3791750118/" title="photo sharing"><img src="http://farm3.static.flickr.com/2597/3791750118_47f1daa6d2_m.jpg" alt="" style="border: solid 2px #000000;" /></a><br />
<br />
<span style="font-size: 0.9em; margin-top: 0px;"><br />
<a href="http://www.flickr.com/photos/bittarman/3791750118/">Massive zend framework poster \ cheatsheet</a><br />
<br />
Originally uploaded by <a href="http://www.flickr.com/people/bittarman/">bittarman</a><br />
</span>
</div>
<p>The other day, a wonderful thing arrived in the post, fresh from Björn Schotte of mayflower.</p>
<p>The poster covers many of the commonly used components, and their parameters. I have had it on the wall for about 2 weeks now, and it has become something of a crutch already, and makes it very handy to quickly check, for example whether it&#8217;s dispatchLoopShutdown or dispatchLoopShutDown in a FC plugin, or what the parameters for the headLink helper are (two things I always have to double check!)</p>
<p>Mayflower is a partner of Zend, and offers many training and  consulting services for PHP companies, as well as developing their own software solutions, such as the &#8220;Chorizo!&#8221; security auditing suite, and consulting for the popular lightweight webserver &#8220;lighttpd&#8221;.</p>
<p>Check out the mayflower site at <a href="http://www.mayflower.de/en">http://www.mayflower.de/en</a>, and drop Björn Schotte an email if you are interested in a poster of your own, he may have some left!<br />
<br clear="all" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rmauger.co.uk/2009/08/massive-zend-framework-poster-cheatsheet/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Creating simple, extendible CRUD, using Zend Framework</title>
		<link>http://www.rmauger.co.uk/2009/06/creating-simple-extendible-crud-using-zend-framework/</link>
		<comments>http://www.rmauger.co.uk/2009/06/creating-simple-extendible-crud-using-zend-framework/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 20:30:16 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.rmauger.co.uk/?p=66</guid>
		<description><![CDATA[The Form Creating a nice, easy to maintain form, starts with a form class. Creating your forms procedurally in your controller/actions is horrid. please don&#8217;t do it. To start with creating your form classes, you need your own namespace in your library. If you don&#8217;t have this, register one. This can be done by adding [...]]]></description>
			<content:encoded><![CDATA[<h3>The Form</h3>
<p>Creating a nice, easy to maintain form, starts with a form class. Creating your forms procedurally in your controller/actions is horrid. please don&#8217;t do it.</p>
<p>To start with creating your form classes, you need your own namespace in your library. If you don&#8217;t have this, register one. This can be done by adding an _initAutoloading method to your Bootstrap. below is a short example. its not comprehensive (you can also do this in your ini i believe, but I use php configuration files similar to <a title="DASPRiD's blog. native cachable configuration" href="http://www.dasprids.de/blog/2009/05/08/writing-powerful-and-easy-config-files-with-php-arrays" target="_blank">DASPRiD</a>&#8216;s, and i&#8217;m not trying to show how to set up autoloading here.)<span id="more-66"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Bootstrap <span style="color: #000000; font-weight: bold;">extends</span> Zend_Application_Bootstrap
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//...</span>
    <span style="color: #009933; font-style: italic;">/**
    * Initilise autoloader and library namespaces
    */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> _initAutoloading<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$loader</span> <span style="color: #339933;">=</span> Zend_Loader_Autoloader<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$loader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerNamespace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'My_'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">//...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Your next task, create a form. This is pretty simple. I will demonstrate using just a few fields. Create a file in your library called My/Form.php</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> My_Form <span style="color: #000000; font-weight: bold;">extends</span> Zend_Form
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
    * Set up form fields, filtering and validation
    */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMethod</span><span style="color: #009900;">&#40;</span>Zend_Form<span style="color: #339933;">::</span><span style="color: #004000;">METHOD_POST</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//Username</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$uname</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Text<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$uname</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLabel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Username'</span><span style="color: #009900;">&#41;</span>
              <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValidator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Db_NoRecordExists'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'table'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span>
                                                               <span style="color: #0000ff;">'field'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
              <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValidator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Alnum'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'allowWhiteSpace'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//Email</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Text<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLabel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Email'</span><span style="color: #009900;">&#41;</span>
              <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValidator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Db_NoRecordExists'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'table'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span>
                                                               <span style="color: #0000ff;">'field'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
              <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValidator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'EmailAddress'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//First name</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$firstname</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Text<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'firstname'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$firstname</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLabel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'First name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//Last name</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$lastname</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Text<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lastname'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$lastname</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLabel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Last name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now that your basic form is created, you need to add a method to perform your CRUD. I&#8217;m not actually going to cover the D (delete) as thats realy simple, and doesn&#8217;t require the form, which is the focus of this post.</p>
<p>This method should take an array for the post data, and a Zend_Db_Table_Row to provide the save functionality. In this example the DB columns have the same names as the form fields, this means we can set values with less code. As we are using Zend_Db, there should be no injection problems with this method, as everything is automagically quoted.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">//...</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> process <span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span> <span style="color: #000088;">$post</span><span style="color: #339933;">,</span> Zend_Db_Table_Row  <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDefaults</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// If the id (primary key) is null then this is a new row, else it is an existing record</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">!==</span> <span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Record already exists, exclude it from db record validation.</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValidator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Db_NoRecordExists'</span><span style="color: #339933;">,</span>
                                 <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>
                                 <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'table'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span>
                                          <span style="color: #0000ff;">'field'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span>
                                          <span style="color: #0000ff;">'exclude'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'field'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span>
                                                                      <span style="color: #0000ff;">'value'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValidator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Db_NoRecordExists'</span><span style="color: #339933;">,</span>
                                 <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>
                                 <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'table'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span>
                                         <span style="color: #0000ff;">'field'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'email'</span><span style="color: #339933;">,</span>
                                         <span style="color: #0000ff;">'exclude'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'field'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span>
                                                                    <span style="color: #0000ff;">'value'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            try <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFromArray</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValues</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDescription</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'There was an error saving your details'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<h2>The Controller / Action</h2>
<p>Now that we have created our nice form (which is capable of CR and U)  now we need to use it from within out controller and model to perform the update or insert and interact with the user.</p>
<p>For this, you need 3 actions in your controller, Create, update, and delete (the delete I will not cover for the before mentioned reasons).</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> UserController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> newAction <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_helper<span style="color: #339933;">-&gt;</span><span style="color: #004000;">ViewRenderer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setScriptAction</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'userform'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$users</span> <span style="color: #339933;">=</span> My_Users<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$users</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNewUserForm</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_helper<span style="color: #339933;">-&gt;</span><span style="color: #004000;">flashMessenger</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'New User Created'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_helper<span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirector</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">gotoUrlAndExit</span><span style="color: #009900;">&#40;</span><span style="color: #009933; font-style: italic;">/** confirmation url here **/</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$user</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> editAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_helper<span style="color: #339933;">-&gt;</span><span style="color: #004000;">ViewRenderer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setScriptAction</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'userform'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">===</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_getParam<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            throw <span style="color: #000000; font-weight: bold;">new</span> Exception <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tampered URI'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$users</span> <span style="color: #339933;">=</span> My_Users<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$users</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getEditUserForm</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_helper<span style="color: #339933;">-&gt;</span><span style="color: #004000;">flashMessenger</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Details Saved'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_helper<span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirector</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">gotoUrlAndExit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user/edit/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$user</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To explain what all this is about, Both actions do pretty similar things (infact this can be consolidated into a single action, but for clarity and ease of replication, I am using two).</p>
<p>There are two things which are pretty important in this, one is the use of the redirector, and the other is the checking of the ID before it is used. In my opinion when an id is passed in a url which is invalid, then a 404 should be raised. So in my error controllers i look for a variety of exception types so that i can debug, but only output a 404 for these in production.</p>
<p>Now looking more closely at the action code, we have calls to getxxxxUserForm() on our models. The return value of this is what we wish to inspect, as you could see in the process method we created earlier, we return boolean true, only when the record saves correctly.  So we do a strict check for this boolean value, and if it is true, we know that we can safely redirect our user. The redirect is an important step, it stops the browser trying to post the data again if the user clicks refresh after their record is created / updated. And the final note is that the flashMessenger is used to pass a message to inform that user that their action was completed on.</p>
<p>Also worth noting is that i have set both actions to use the same action name in the viewRenderer. This allows you to consolidate this common script into one. (repeat after me, Dont Repeat Yourself!). The view script for this is pretty simple.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;div class=&quot;messages&quot;&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span>Zend_Controller_Action_HelperBroker<span style="color: #339933;">::</span><span style="color: #004000;">getStaticHelper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'flashMessenger'</span><span style="color: #009900;">&#41;</span>
             <span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessages</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;p&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">escape</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h2>The Model</h2>
<p>In the model, we now need two simple methods to tie the lot together. Now some people might (read: Will) argue that the model should be performing validation, I would argue that with this method, the Model *is* performing the validation, you are simply making use of a library class to perform this function, much in the same way you use Zend_Db_Row. Feel free to flame about this below, but I&#8217;m sticking with this, and it provides clean easy seperation, and provides a good clean mechanism to provide user feedback.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> My_Users
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
    * @var Zend_Db_Table_Abstract
    */</span>
    protected <span style="color: #000088;">$_table</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">//....</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
    * Retrieves a new user record, and processes a form against it.
    *
    * @param array $post
    * @return boolean|Zend_Form Boolean true if a save successfully occurs, a populated form on all other conditions
    */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getNewUserForm<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span> <span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> My_Form<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">process</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_table<span style="color: #339933;">-&gt;</span><span style="color: #004000;">createRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
    * Retrieves a user record, and processes a form against it.
    *
    * @param array $post
    * @return boolean|Zend_Form Boolean true if a save successfully occurs, a populated form on all other conditions
    */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getEditUserForm<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span> <span style="color: #000088;">$post</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_table<span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchRow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$table</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id = ?'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> My_Form<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">process</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Well, this really doesn&#8217;t need much explanation. this simply provides glue between the parts above. You can make these a little more complex, and infact you probably should (I do!) by sanitizing your input from the controller in the form of the user id, $id, in the edit method.</p>
<h2>In conclusion</h2>
<p>This post has partly been inspired by comments in #zftalk and in part, by the search for a good way to deal with forms, and I look forward to any feedback. Before there is any cirisitm of the validation and filtering in the form, I have kept is minimal to illustrate how the form element deals with two validators of the same name (by overwriting the first instance) and how that can be leveraged to your advantage, and also to keep the code minimal to outline the actual process, and not how to add validators. for that go and <a title="Zend Frame work manau" href="http://framework.zend.com/manual/en" target="_blank">RTFM</a>! <img src='http://www.rmauger.co.uk/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.rmauger.co.uk/2009/06/creating-simple-extendible-crud-using-zend-framework/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Zend Framework, Zend_Dojo and  Dojo Tree</title>
		<link>http://www.rmauger.co.uk/2009/02/zend-framework-zend_dojo-and-dojo-tree/</link>
		<comments>http://www.rmauger.co.uk/2009/02/zend-framework-zend_dojo-and-dojo-tree/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 20:10:30 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[Dojo]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.rmauger.co.uk/?p=18</guid>
		<description><![CDATA[Working on a new CMS at work, I needed to allow a user to insert a page at a point in the site tree. And i thought to myself, well, Dojo has a tree widget, and Zend Framework supports dojo, it can&#8217;t be a difficult task. Unfortunately I was unable to find a &#8216;simple&#8217; method [...]]]></description>
			<content:encoded><![CDATA[<p>Working on a new CMS at work, I needed to allow a user to insert a page at a point in the site tree. And i thought to myself, well, Dojo has a tree widget, and Zend Framework supports dojo, it can&#8217;t be a difficult task.</p>
<p>Unfortunately I was unable to find a &#8216;simple&#8217; method of doing this already documented, so I experimented a little, and threw away some very in-elegant ideas, and finally came up with the following technique, after discovering the <a title="Zend dojo data envelopes" href="http://framework.zend.com/manual/en/zend.dojo.data.html" target="_blank">Zend_Dojo_Data</a>.</p>
<p><span id="more-18"></span></p>
<p>Firstly, I added a method to my tree model to walk the tree, and build an array suitable for turning into the required structure for the json.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> SiteTree <span style="color: #000000; font-weight: bold;">extends</span> Zend_Db_Table_Abstract
<span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//....</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _walkTree<span style="color: #009900;">&#40;</span><span style="color: #990000;">Array</span> <span style="color: #000088;">$root</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$path</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$root</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #000088;">$root</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBranchChildren</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$arrNodes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$root</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$branch</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$thisPath</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$branch</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'branch_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$path</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$branch</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'branch_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$branch</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'parent'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'0'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$branch</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$arrNodes</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span>       <span style="color: #339933;">=</span> <span style="color: #000088;">$branch</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$arrNodes</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'label'</span><span style="color: #009900;">&#93;</span>    <span style="color: #339933;">=</span> <span style="color: #000088;">$branch</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'branch_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$arrNodes</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'path'</span><span style="color: #009900;">&#93;</span>     <span style="color: #339933;">=</span> <span style="color: #000088;">$thisPath</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$arrNodes</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'iconClass'</span><span style="color: #009900;">&#93;</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">'folder'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$arrNodes</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'children'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$branches</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBranchChildren</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$branch</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$arrNodes</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'children'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_walkTree<span style="color: #009900;">&#40;</span><span style="color: #000088;">$branches</span><span style="color: #339933;">,</span> <span style="color: #000088;">$thisPath</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$i</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$arrNodes</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//....</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This function recursively maps the database into a nested array. Please note, this function will cause many database queries on deep tree structures, so I really would advise you cache the return of this function, so to keep queries to a minimum.</p>
<p>The line &#8220;$key = ($branch['parent'] == &#8217;0&#8242;) ? $branch['id'] : $i;&#8221; is important. For Zend_Dojo_Data to output the correctly formatted json, you need to have an associative array for the top level (root) nodes, and indexed arrays for all of their children.</p>
<p>Next, this data needs to be turned into json for the tree. To do this, I simply created a new action to use as the source for the json, and inserted the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> jsonNewpageAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    Zend_Dojo<span style="color: #339933;">::</span><span style="color: #004000;">enableView</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Zend_Layout<span style="color: #339933;">::</span><span style="color: #004000;">getMvcInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">disableLayout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$tree</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SiteTree<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$treeObj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Dojo_Data<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tree</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTree</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$treeObj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLabel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'label'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$treeObj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMetadata</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'path'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tree</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$treeObj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toJson</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Setting the Label is important here, it is used to determine which attribute of each node is its label, next the Metadata attribute &#8220;path&#8221; I used for my customisation of the tree for my own needs, which you will see later. The view script for this action simply echo&#8217;s the tree value.</p>
<p>The next step is to create the view to output your tree. this part is where the real fun begins, as you can get carried away with tweaking the dojo widget if your not careful.</p>
<p>The following lines were added to the action with my form on it. (The tree is not part of the form, I simple made mine interact with a read only element inside it)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">Zend_Dojo<span style="color: #339933;">::</span><span style="color: #004000;">enableView</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dojo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">requireModule</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dojo.data.ItemFileReadStore'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dojo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">requireModule</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dijit.Tree'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dojo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">requireModule</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dojo.parser'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This will add the require statements to the view, so that the required dojo parts are available.</p>
<p>And in the view itself, you need at least the following:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div dojoType=&quot;dojo.data.ItemFileReadStore&quot;  url=&quot;/admin/pages/json-newpage&quot; jsid=&quot;jsonData&quot;&gt;&lt;/div&gt;
&lt;div dojoType=&quot;dijit.tree.ForestStoreModel&quot; jsId=&quot;treeModel&quot; store=&quot;jsonData&quot; rootId=&quot;treeRoot&quot; rootLabel=&quot;Home&quot; childrenAttrs=&quot;children&quot; &gt;&lt;/div&gt;
&nbsp;
&lt;div dojoType=&quot;dijit.Tree&quot; model=&quot;treeModel&quot; labelAttrs=&quot;label&quot; &gt;&lt;/div&gt;</pre></div></div>

<p>The first div, is the part which will read your json from the first action I listed. If you have only a single root node, you may get away without the second div, which is the ForestStoreModel. The ForestStoreModel had me scratching my head for a while, as I had no idea I needed it, and I kept finding errors in my FireBug console. Eventually I found <a title="The dojo model - An explanation of the ForestStoreModel" href="http://dojotoolkit.org/2008/02/24/dijit-tree-and-dojo-data-dojo-1-1-model" target="_blank">this</a> page on the dojo site.</p>
<p>If you were to put all this together now, you would probably end up with a working tree. BUT, it can be improved. In my tree, all the leaves would actually represent folders, but as default they look like little documents, not good at all. So I asked around in IRC, and was told I needed to google getIconClass, which put me on to writing this,</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div dojoType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;dijit.Tree&quot;</span> model<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;treeModel&quot;</span> labelAttrs<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;label&quot;</span> iconClass<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;dijitFolderOpened&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;dojo/method&quot;</span> event<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;getIconClass&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #b1b100;">return</span>  <span style="color: #0000ff;">'dijitFolderClosed'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>

<p>As you can see, a script is inserted INSIDE the container of the tree, and the event attribute tells the script which even it is responsible for overriding.</p>
<p>If you run this now, you will find that all of the folders in the tree are now closed. Not good, as the nice opening and closing folders as you expand/contract elements are lost. This can be changed by adding another attribute to the script:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;dojo/method&quot;</span> event<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;getIconClass&quot;</span> args<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;item, opened&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>opened ? <span style="color: #0000ff;">'dijitFolderOpened'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'dijitFolderClosed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></pre></div></div>

<p>The args attribute allows arguments from the item firing the event to be passed to the script. so a simple ternary, and you can switch between opened and closed folders.</p>
<h2>Update</h2>
<p>You may also find <a title="Codeutopia.net Zend Framework and Dojo Tree's with Checkboxes" href="http://codeutopia.net/blog/2008/11/21/creating-a-dojo-dijittree-with-checkboxes/" target="_blank">this</a> post at codeutopia.net useful if you are working with tree&#8217;s in Dojo / Zend Framework</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rmauger.co.uk/2009/02/zend-framework-zend_dojo-and-dojo-tree/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
