<?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>Phil Brown&#039;s Web Development Blog &#187; PHP</title>
	<atom:link href="http://blog.philipbrown.id.au/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.philipbrown.id.au</link>
	<description></description>
	<lastBuildDate>Mon, 24 May 2010 05:11:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Zend Framework and Doctrine Part 1</title>
		<link>http://blog.philipbrown.id.au/2009/11/zend-framework-and-doctrine-part-1/</link>
		<comments>http://blog.philipbrown.id.au/2009/11/zend-framework-and-doctrine-part-1/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 23:24:40 +0000</pubDate>
		<dc:creator>Phil</dc:creator>
				<category><![CDATA[Doctrine]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[application resource plugin]]></category>
		<category><![CDATA[application.ini]]></category>

		<guid isPermaLink="false">http://morecowbell.net.au/?p=136</guid>
		<description><![CDATA[I&#8217;ve begun to investigate the Doctrine ORM library and how to integrate it into Zend Framework applications. I figure this post and any subsequent ones on the topic can grow into some sort of discovery series.
So, my first post is going to be a simple one &#8211; bootstrapping with Doctrine 1.2.
I figured the most robust [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve begun to investigate the Doctrine ORM library and how to integrate it into Zend Framework applications. I figure this post and any subsequent ones on the topic can grow into some sort of discovery series.</p>
<p>So, my first post is going to be a simple one &#8211; bootstrapping with Doctrine 1.2.</p>
<p>I figured the most robust and consistent way to get Doctrine into my applications was to create an application resource plugin so without further delay, here it is.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// library/Tolerable/Application/Resource/Doctrine.php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Tolerable_Application_Resource_Doctrine <span style="color: #000000; font-weight: bold;">extends</span> Zend_Application_Resource_ResourceAbstract
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * @var Doctrine_Manager
     */</span>
    protected <span style="color: #000088;">$_manager</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var Doctrine_Cache_Interface
     */</span>
    protected <span style="color: #000088;">$_cacheDriver</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var string|array
     */</span>
    protected <span style="color: #000088;">$_cache</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var array
     */</span>
    protected <span style="color: #000088;">$_connections</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>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Set cache options
     * 
     * @param string|array $cache
     * @return Tolerable_Application_Resource_Doctrine
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setCache<span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</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>_cache <span style="color: #339933;">=</span> <span style="color: #000088;">$cache</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>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @return string|array
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getCache<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_cache<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Set connection configuration
     * 
     * @param array $connections
     * @return Tolerable_Application_Resource_Doctrine
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setConnections<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span> <span style="color: #000088;">$connections</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>_connections <span style="color: #339933;">=</span> <span style="color: #000088;">$connections</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>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Initialise and return Doctrine_Manager instance
     * 
     * @return Doctrine_Manager
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getManager<span style="color: #009900;">&#40;</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: #009900; font-weight: bold;">null</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_manager<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$manager</span> <span style="color: #339933;">=</span> Doctrine_Manager<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;">$manager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span>Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">ATTR_MODEL_LOADING</span><span style="color: #339933;">,</span>
                                   Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">MODEL_LOADING_CONSERVATIVE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$manager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span>Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">ATTR_VALIDATE</span><span style="color: #339933;">,</span>
                                   Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">VALIDATE_ALL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$manager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span>Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">ATTR_AUTO_ACCESSOR_OVERRIDE</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$manager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span>Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">ATTR_AUTOLOAD_TABLE_CLASSES</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$manager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span>Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">ATTR_USE_DQL_CALLBACKS</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$manager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span>Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">ATTR_AUTO_FREE_QUERY_OBJECTS</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$cacheDriver</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_getCacheDriver<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$manager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span>Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">ATTR_QUERY_CACHE</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cacheDriver</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_manager <span style="color: #339933;">=</span> <span style="color: #000088;">$manager</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_manager<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Initialise and return query cache driver if configured
     * 
     * @return Doctrine_Cache_Interface|null
     */</span>
    protected <span style="color: #000000; font-weight: bold;">function</span> _getCacheDriver<span style="color: #009900;">&#40;</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: #009900; font-weight: bold;">null</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_cacheDriver<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCache</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: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</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: #339933;">!</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'driver'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</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> Zend_Application_Resource_Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Missing Doctrine cache driver'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000088;">$driver</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'driver'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'driver'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$driver</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$options</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$options</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: #009900;">&#125;</span>
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">class_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$driver</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                Zend_Loader<span style="color: #339933;">::</span><span style="color: #004000;">loadClass</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$driver</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_cacheDriver <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000088;">$driver</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_cacheDriver<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Initialise any configured connections
     * 
     * @return void
     */</span>
    protected <span style="color: #000000; font-weight: bold;">function</span> _initConnections<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_connections <span style="color: #b1b100;">as</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$options</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: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dsn'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'charset'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$charset</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'charset'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'charset'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$charset</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'utf8'</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Attempt to find &quot;dsn&quot; or &quot;connection_string&quot; keys</span>
            <span style="color: #666666; font-style: italic;">// falling back to an array of connection options</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dsn'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$adapter</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dsn'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'connection_string'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$adapter</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'connection_string'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$adapter</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$options</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$adapter</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$connection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span>Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">ATTR_USE_NATIVE_ENUM</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$connection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCharset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$charset</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Initialise and return Doctrine manager
     * 
     * @return Doctrine_Manager
     */</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>_initConnections<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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getManager</span><span style="color: #009900;">&#40;</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>This class provides a reusable component to use in any ZF 1.8+ application. To enable the plugin, add the following to your <code>application.ini</code> file.</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">; Add the Doctrine and Tolerable namespaces to the autoloader</span>
autoloaderNamespaces<span style="">&#91;</span><span style="">&#93;</span> <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Doctrine_&quot;</span>
autoloaderNamespaces<span style="">&#91;</span><span style="">&#93;</span> <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Tolerable_&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">; Add our custom application resource plugin path to the plugin loader</span>
pluginPaths.Tolerable_Application_Resource <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Tolerable/Application/Resource&quot;</span></pre></div></div>

<p>Configure the Doctrine query cache (if desired)</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">; Simple examples, no driver options</span>
resources.doctrine.cache <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Doctrine_Cache_Apc&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">; or with options</span>
resources.doctrine.cache.driver <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Doctrine_Cache_Apc&quot;</span>
resources.doctrine.cache.option <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> ...</span>
resources.doctrine.cache.option <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> ...</span>
resources.doctrine.cache.option <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> ...</span></pre></div></div>

<p>See <a href="http://www.doctrine-project.org/documentation/manual/1_0/en/caching">here</a> for drivers and options.</p>
<p>Configure the connections</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">; Simple connection strings</span>
resources.doctrine.connections<span style="">&#91;</span><span style="">&#93;</span> <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;mysql://user1:pass@localhost/db1&quot;</span>
resources.doctrine.connections<span style="">&#91;</span><span style="">&#93;</span> <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;mysql://user2:pass@localhost/db2&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">; or specify connection name and options</span>
resources.doctrine.connections.default.dsn     <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;mysql://user1:pass@localhost/db1&quot;</span>
resources.doctrine.connections.default.charset <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;utf8&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">; or specify Doctrine connection parameters</span>
resources.doctrine.connections.default.scheme <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;mysql&quot;</span>
resources.doctrine.connections.default.user   <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;user1&quot;</span>
resources.doctrine.connections.default.pass   <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;pass&quot;</span>
resources.doctrine.connections.default.host   <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;localhost&quot;</span>
resources.doctrine.connections.default.path   <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;db1&quot;</span></pre></div></div>

<p>This is assuming that your custom (Tolerable in these examples) and Doctrine libraries are under your application&#8217;s &#8220;library&#8221; path. If not, simply add them to the application&#8217;s include path configuration, for example</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">includePaths.library <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> APPLICATION_PATH </span><span style="color: #933;">&quot;/../library&quot;</span>
includePaths.doctrine <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;/path/to/doctrine/lib&quot;</span>
includePaths.custom <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;/path/to/custom/library&quot;</span></pre></div></div>

<p>A lot of the code in the plugin has been taken from Matthew Weier O&#8217;Phinney&#8217;s blog post on <a href="http://weierophinney.net/matthew/archives/220-Autoloading-Doctrine-and-Doctrine-entities-from-Zend-Framework.html">Autoloading Doctrine and Doctrine entities from Zend Framework</a>.</p>
<h3>2009-11-19 Update</h3>
<p>Some extra inspiration came from Juozas Kaziukenas&#8217; post on <a href="http://dev.juokaz.com/php/zend-framework-and-doctrine-part-2">Zend Framework and Doctrine. Part 2</a> as well as the twitter-verse and now the plugin handles multiple connections and caching.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.philipbrown.id.au/2009/11/zend-framework-and-doctrine-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Overriding Zend form element default decorators, for good!</title>
		<link>http://blog.philipbrown.id.au/2009/10/overriding-zend-form-element-default-decorators-for-good/</link>
		<comments>http://blog.philipbrown.id.au/2009/10/overriding-zend-form-element-default-decorators-for-good/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 01:55:22 +0000</pubDate>
		<dc:creator>Phil</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[decorators]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[Zend_Form]]></category>
		<category><![CDATA[Zend_Form_Element]]></category>

		<guid isPermaLink="false">http://morecowbell.net.au/?p=131</guid>
		<description><![CDATA[I&#8217;m sure everybody who has used Zend_Form has, at one time or another, wished they could change the default form element decorators in one fell swoop, simply and efficiently.
Up until now, I&#8217;ve been using one of two methods to override the default &#8220;definition list&#8221; style; set each element&#8217;s decorator scheme on instantiation or, use the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sure everybody who has used Zend_Form has, at one time or another, wished they could change the default form element decorators in one fell swoop, simply and efficiently.</p>
<p>Up until now, I&#8217;ve been using one of two methods to override the default &#8220;definition list&#8221; style; set each element&#8217;s decorator scheme on instantiation or, use the form&#8217;s &#8220;setElementDecorators&#8221; method to reset the scheme for nominated elements. No longer!</p>
<p>Using just a few simple classes, I&#8217;ll show you how to permanently alter the default decorator scheme, thus simplifying your code and making it ultimately more flexible.</p>
<p>For the following example, I&#8217;ll assume a decorator scheme like the following</p>

<div class="wp_syntax"><div class="code"><pre class="txt" style="font-family:monospace;">form
  fieldset
    ol
      li
        label
        element
      li
        button</pre></div></div>

<p>Lets start by extending Zend_Form_Element. I&#8217;d also like to take this opportunity to introduce my new &#8220;Tolerable&#8221; library.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// library/Tolerable/Form/Element.php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Tolerable_Form_Element <span style="color: #000000; font-weight: bold;">extends</span> Zend_Form_Element
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * Load default decorators
     *
     * @return void
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadDefaultDecorators<span style="color: #009900;">&#40;</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadDefaultDecoratorsIsDisabled</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$decorators</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDecorators</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: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$decorators</span><span style="color: #009900;">&#41;</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;">addDecorator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ViewHelper'</span><span style="color: #009900;">&#41;</span>
                 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDecorator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Errors'</span><span style="color: #009900;">&#41;</span>
                 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDecorator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Description'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'p'</span><span style="color: #339933;">,</span>
                                                     <span style="color: #0000ff;">'class'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'description'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDecorator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Label'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'requiredSuffix'</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;">-&gt;</span><span style="color: #004000;">addDecorator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HtmlTag'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'li'</span><span style="color: #339933;">,</span>
                                                 <span style="color: #0000ff;">'id'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'-element'</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: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Don&#8217;t forget to add your library namespace to the auto loader</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">; application/configs/application.ini</span>
&nbsp;
autoloaderNamespaces.1 <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Tolerable_&quot;</span></pre></div></div>

<p>Zend_Form_Element_Submit also needs some treatment</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// library/Tolerable/Form/Element/Submit.php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Tolerable_Form_Element_Submit <span style="color: #000000; font-weight: bold;">extends</span> Zend_Form_Element_Submit
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * Default decorators
     *
     * Uses only 'Submit' and 'li' decorators by default.
     * 
     * @return void
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadDefaultDecorators<span style="color: #009900;">&#40;</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadDefaultDecoratorsIsDisabled</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$decorators</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDecorators</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: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$decorators</span><span style="color: #009900;">&#41;</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;">addDecorator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tooltip'</span><span style="color: #009900;">&#41;</span>
                 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDecorator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ViewHelper'</span><span style="color: #009900;">&#41;</span>
                 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDecorator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HtmlTag'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'li'</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: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We&#8217;ll also need to provide our own display group class to support the desired scheme</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// library/Tolerable/Form/DisplayGroup.php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Tolerable_Form_DisplayGroup <span style="color: #000000; font-weight: bold;">extends</span> Zend_Form_DisplayGroup
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * Load default decorators
     * 
     * @return void
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadDefaultDecorators<span style="color: #009900;">&#40;</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadDefaultDecoratorsIsDisabled</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$decorators</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDecorators</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: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$decorators</span><span style="color: #009900;">&#41;</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;">addDecorator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Description'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'li'</span><span style="color: #339933;">,</span>
                                                     <span style="color: #0000ff;">'class'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'group_description'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDecorator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FormElements'</span><span style="color: #009900;">&#41;</span>
                 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDecorator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HtmlTag'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ol'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDecorator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Fieldset'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>as well as a custom form class</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// library/Tolerable/Form.php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Tolerable_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;">/**
     * Default display group class
     * @var string
     */</span>
    protected <span style="color: #000088;">$_defaultDisplayGroupClass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Tolerable_Form_DisplayGroup'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Constructor
     *
     * Add custom prefix path before parent constructor
     *
     * @param mixed $options
     * @return void
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addPrefixPath</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tolerable_Form'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Tolerable/Form'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</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;">/**
     * Load the default decorators
     *
     * @return void
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadDefaultDecorators<span style="color: #009900;">&#40;</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadDefaultDecoratorsIsDisabled</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$decorators</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDecorators</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: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$decorators</span><span style="color: #009900;">&#41;</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;">addDecorator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FormElements'</span><span style="color: #009900;">&#41;</span>
                 <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDecorator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Form'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now that we&#8217;ve laid the way for our custom decorator scheme, there&#8217;s only one item remaining and I can&#8217;t say that I&#8217;m proud but hey, it works.</p>
<p>All front facing (HTML) Zend form elements extend the empty, abstract class Zend_Form_Element_Xhtml. We&#8217;re going to override this class in its current form. This method works well if the Zend Framework is not in the application&#8217;s &#8220;library&#8221; which should have a higher &#8220;include path&#8221; setting than the framework itself. Otherwise, you&#8217;ll just have to override the existing Zend Framework file.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// library/Zend/Form/Element/Xhtml.php</span>
&nbsp;
abstract <span style="color: #000000; font-weight: bold;">class</span> Zend_Form_Element_Xhtml <span style="color: #000000; font-weight: bold;">extends</span> Tolerable_Form_Element
<span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That&#8217;s it. Get ready to start writing leaner, meaner forms</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Tolerable_Form<span style="color: #339933;">;</span>
<span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'foo'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'label'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Foo'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'required'</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;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'textarea'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'bar'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'label'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Bar'</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'submit'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'submit_btn'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'label'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Submit'</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDisplayGroup</span><span style="color: #009900;">&#40;</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'foo'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'bar'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'submit_btn'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'baz'</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'legend'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'My Form'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.philipbrown.id.au/2009/10/overriding-zend-form-element-default-decorators-for-good/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP suExec and custom php.ini files</title>
		<link>http://blog.philipbrown.id.au/2009/08/php-suexec-and-custom-php-ini-files/</link>
		<comments>http://blog.philipbrown.id.au/2009/08/php-suexec-and-custom-php-ini-files/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 00:13:59 +0000</pubDate>
		<dc:creator>Phil</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[.htaccess]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[custom]]></category>
		<category><![CDATA[php.ini]]></category>
		<category><![CDATA[PHPRC]]></category>
		<category><![CDATA[phpSuExec]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[suExec]]></category>

		<guid isPermaLink="false">http://morecowbell.net.au/?p=107</guid>
		<description><![CDATA[Lately, I&#8217;ve noticed more and more shared web hosts making the switch to running PHP under suExec. The benefits of this are:

PHP scripts run as the owning user
No more file system permission juggling
Scripts are generally sandboxed to the owner&#8217;s home directory

Unfortunately, this poses a problem for any custom PHP configuration changes as the usual .htaccess [...]]]></description>
			<content:encoded><![CDATA[<p>Lately, I&#8217;ve noticed more and more shared web hosts making the switch to running PHP under suExec. The benefits of this are:</p>
<ul>
<li>PHP scripts run as the owning user</li>
<li>No more file system permission juggling</li>
<li>Scripts are generally sandboxed to the owner&#8217;s home directory</li>
</ul>
<p>Unfortunately, this poses a problem for any custom PHP configuration changes as the usual <code>.htaccess</code> <code>php_flag</code> and <code>php_value</code> properties are no longer supported. The recommended method of setting a custom configuration is to place a <code>php.ini</code> file under each directory where required.</p>
<p>This method works fine for any properties set in the custom <code>php.ini</code> file however there appears to be one devastating omission. <em>Server configuration properties do <strong>not</strong> cascade into the custom file.</em> What this means is that for any property <em>not</em> set in your custom file, PHP will revert to the PHP default.</p>
<p>For example, say your host has enabled PDO (as any decent host should). The instant you introduce a custom <code>php.ini</code> file, PDO will be lost. Sure, you could just include the relevant <code>extension_dir</code> and <code>extension</code> properties but consider this example (from my host)</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000099;">extension_dir</span> <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;/usr/local/lib/php/extensions/no-debug-non-zts-20060613&quot;</span></pre></div></div>

<p>That property is awfully specific and your custom entry wouldn&#8217;t fare too well in the event of an upgrade.</p>
<p>In a perfect world, the host would have compiled PHP with the <code>--with-config-file-scan-dir=/some/dir</code> option that allows an environmentally settable directory to be scanned for additional config files. Unfortunately, this is rarely the case.</p>
<p>My solution is a little hacky and hardly perfect but it certainly gets the job done for me.</p>
<p>1. Create your custom.ini file with the options you want. Mine is <code>/home/user/etc/php.d/custom.ini</code> and looks like this</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000099;">register_globals</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> Off</span>
<span style="color: #000099;">magic_quotes_gpc</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> Off</span></pre></div></div>

<p>Do <strong>not</strong> name this file php.ini.</p>
<p>2. Locate your host&#8217;s <code>php.ini</code> file. This information is available from <code>phpinfo()</code>. In the below examples it is <code>/usr/local/lib/php.ini</code></p>
<p>3. Add a cron job to create a full <code>php.ini</code> file in a location of your choosing (<code>/home/user/etc/php.d</code>) that is the concatenation of the server <code>php.ini</code> file and your <code>custom.ini</code> file. The below example is set to run hourly with any errors displayed on stdout which should be mailed to the crontab owner.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>php.ini <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>php.ini <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>user<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>php.d<span style="color: #000000; font-weight: bold;">/</span>custom.ini <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>user<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>php.d<span style="color: #000000; font-weight: bold;">/</span>php.ini <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'/usr/local/lib/php.ini NOT FOUND'</span></pre></div></div>

<p>The reason for the cron job is to keep your full, custom <code>php.ini</code> file in sync with any server changes. The job will also inform you in the unlikely event that the host <code>php.ini</code> file is moved. </p>
<p>If you&#8217;ve got shell access, you can create this file immediately by running</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>php.ini <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>user<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>php.d<span style="color: #000000; font-weight: bold;">/</span>custom.ini <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>user<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>php.d<span style="color: #000000; font-weight: bold;">/</span>php.ini</pre></div></div>

<p>otherwise, wait for the hour to come around in which case your cron job should have done the work for you.</p>
<p>4. Add the following to your web application&#8217;s <code>.htaccess</code> file</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #00007f;">SetEnv</span> PHPRC /home/<span style="color: #00007f;">user</span>/etc/php.d</pre></div></div>

<p>The <code>PHPRC</code> environment variable informs any PHP scripts of the location of the preferred <code>php.ini</code> file.</p>
<p>Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.philipbrown.id.au/2009/08/php-suexec-and-custom-php-ini-files/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Editor themes for Eclipse PDT &#8211; Obsidian</title>
		<link>http://blog.philipbrown.id.au/2009/07/editor-themes-for-eclipse-pdt-obsidian/</link>
		<comments>http://blog.philipbrown.id.au/2009/07/editor-themes-for-eclipse-pdt-obsidian/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 06:01:50 +0000</pubDate>
		<dc:creator>Phil</dc:creator>
				<category><![CDATA[IDE]]></category>
		<category><![CDATA[colour themes]]></category>
		<category><![CDATA[dark]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[editor]]></category>
		<category><![CDATA[PDT]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://morecowbell.net.au/?p=94</guid>
		<description><![CDATA[My PHP IDE of choice is Eclipse with the PDT plugin.
The thing is, I always feel a little snow blind after a day&#8217;s coding, staring at that white screen all day. So much so that I&#8217;ve undertaken the arduous task of tweaking my colour preferences to match the Obsidian theme found in the more recent [...]]]></description>
			<content:encoded><![CDATA[<p>My PHP IDE of choice is Eclipse with the PDT plugin.</p>
<p>The thing is, I always feel a little snow blind after a day&#8217;s coding, staring at that white screen all day. So much so that I&#8217;ve undertaken the arduous task of tweaking my colour preferences to match the Obsidian theme found in the more recent versions of Notepad++. Why Eclipse can&#8217;t take some lead from Notepad++ and Aptana and make this process much easier, I don&#8217;t know but here&#8217;s the end result.</p>
<p><em>PHP file with Obsidian theme</em><br />
<a href="http://morecowbell.net.au/wp-content/uploads/2009/07/eclipse-obsidian-php.png" rel="lightbox-obsidian"><img src="http://morecowbell.net.au/wp-content/uploads/2009/07/eclipse-obsidian-php-300x157.png" alt="PHP file with Obsidian theme" title="PHP file with Obsidian theme" width="300" height="157" class="alignnone size-medium wp-image-97" /></a></p>
<p><em>Mixed PHP and HTML file with Obsidian theme</em><br />
<a href="http://morecowbell.net.au/wp-content/uploads/2009/07/eclipse-obsidian-mixed.png" rel="lightbox-obsidian"><img src="http://morecowbell.net.au/wp-content/uploads/2009/07/eclipse-obsidian-mixed-300x266.png" alt="Mixed PHP and HTML file with Obsidian theme" title="Mixed PHP and HTML file with Obsidian theme" width="300" height="266" class="alignnone size-medium wp-image-98" /></a></p>
<p>I&#8217;ve also created some preference files so you can play along at home. Please note that whilst I&#8217;ve made sure the preference files only contain colour data, I&#8217;ve only done minor testing but so far, everything has gone well. My setup is Eclipse Galileo with PDT 2.1 however I don&#8217;t think there&#8217;s any version specific stuff in the files.</p>
<p>To install the colour preferences:</p>
<ol>
<li>Download one of the below packages and extract the contents to a temporary directory</li>
<li>Fire up Eclipse</li>
<li>Backup your current preferences by going to <code>File -> Export -> General -> Preferences</code>. Follow the instructions from there to create a preferences file. Make sure you choose &#8220;Export all&#8221;.</li>
<li>Go to <code>File -> Import -> General -> Preferences</code> and select the <code>Obsidian/eclipse.epf</code> file. Click &#8220;Finish&#8221;</li>
</ol>
<p>I&#8217;ve also included some Aptana colourisation files for CSS and JavaScript however I haven&#8217;t yet created preferences for other Aptana editors.</p>
<p>Enjoy and keep watching for more themes.</p>
<p><a href='http://morecowbell.net.au/wp-content/uploads/2009/07/Obsidian.tar.gz'>Obsidian.tar.gz</a><br />
<a href='http://morecowbell.net.au/wp-content/uploads/2009/07/Obsidian.zip'>Obsidian.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.philipbrown.id.au/2009/07/editor-themes-for-eclipse-pdt-obsidian/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Extending Zend Framework Application Resource Plugins</title>
		<link>http://blog.philipbrown.id.au/2009/06/extending-zend-framework-application-resource-plugins/</link>
		<comments>http://blog.philipbrown.id.au/2009/06/extending-zend-framework-application-resource-plugins/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 01:21:50 +0000</pubDate>
		<dc:creator>Phil</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[bootstrap]]></category>
		<category><![CDATA[resource plugins]]></category>

		<guid isPermaLink="false">http://morecowbell.net.au/?p=89</guid>
		<description><![CDATA[With the arrival of Zend Framework version 1.8 came the application resource plugins. These nifty little classes help bootstrap your application&#8217;s resources (views, layouts, database connections, etc). The default behaviour is suitable for most needs however, occasionally, you&#8217;re going to want to perform some extra functionality.
Take for example the following use case &#8211; the standard [...]]]></description>
			<content:encoded><![CDATA[<p>With the arrival of Zend Framework version 1.8 came the application resource plugins. These nifty little classes help bootstrap your application&#8217;s resources (views, layouts, database connections, etc). The default behaviour is suitable for most needs however, occasionally, you&#8217;re going to want to perform some extra functionality.</p>
<p>Take for example the following use case &#8211; the standard DB resource plugin instantiates a database connection based on supplied parameters and registers it with <code>Zend_Db_Table_Abstract::setDefaultAdapter()</code>. What if you want to register the connection in the <code>Zend_Registry</code> as well? You could create a bootstrap method (<code>_initRegistry()</code> for example) and pull the adapter out of <code>Zend_Db_Table_Abstract</code> <em>or</em> you could simply extend the resource plugin.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// library/My/Application/Resource/Db.php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> My_Application_Resource_Db <span style="color: #000000; font-weight: bold;">extends</span> Zend_Application_Resource_Db
<span style="color: #009900;">&#123;</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: #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: #009900;">&#40;</span><span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDbAdapter</span><span style="color: #009900;">&#40;</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>
            Zend_Registry<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dbAdapter'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> parent<span style="color: #339933;">::</span><span style="color: #004000;">init</span><span style="color: #009900;">&#40;</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>This class simply adds the created database adapter to the registry before continuing on with the default behaviour.</p>
<p>To let the application know about your custom plugin, you simply add the plugin path to your <code>application.ini</code> file, eg</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">includePaths.library <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> APPLICATION_PATH </span><span style="color: #933;">&quot;/../library&quot;</span>
pluginPaths.My_Application_Resource <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;My/Application/Resource&quot;</span></pre></div></div>

<p>More reading is available here &#8211; <a href="http://framework.zend.com/manual/en/zend.application.theory-of-operation.html">http://framework.zend.com/manual/en/zend.application.theory-of-operation.html</a></p>
<p>Further examples here &#8211; <a href="http://framework.zend.com/manual/en/zend.application.examples.html">http://framework.zend.com/manual/en/zend.application.examples.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.philipbrown.id.au/2009/06/extending-zend-framework-application-resource-plugins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Changing layouts with Zend ContextSwitch</title>
		<link>http://blog.philipbrown.id.au/2009/02/changing-layouts-with-zend-contextswitch/</link>
		<comments>http://blog.philipbrown.id.au/2009/02/changing-layouts-with-zend-contextswitch/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 03:19:58 +0000</pubDate>
		<dc:creator>Phil</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Context Switch]]></category>
		<category><![CDATA[layout]]></category>

		<guid isPermaLink="false">http://morecowbell.net.au/?p=51</guid>
		<description><![CDATA[Ever since discovering Zend Framework&#8217;s ContextSwitch and AjaxContext action helpers, I&#8217;ve been hooked. What&#8217;s not to love about writing one controller action and having the request dictate the response format.
Whilst this generally does the job for XML, JSON and AJAX HTML snippets where any layout is disabled, sometimes you just need to wrap that content [...]]]></description>
			<content:encoded><![CDATA[<p>Ever since discovering Zend Framework&#8217;s <a href="http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.contextswitch">ContextSwitch and AjaxContext</a> action helpers, I&#8217;ve been hooked. What&#8217;s not to love about writing one controller action and having the request dictate the response format.</p>
<p>Whilst this generally does the job for XML, JSON and AJAX HTML snippets where any layout is disabled, sometimes you just need to wrap that content in <em>something</em>.</p>
<p>I often find myself writing small configuration screens that work nicely in modal style dialogs as opposed to a devoted web page. Generally I&#8217;ll render these screens as inline frames using my favourite <a href="http://jquery.com/">JavaScript library</a> and <a href="http://swip.codylindley.com/DOMWindowDemo.html">DOM Window</a> (a <a href="http://jquery.com/demo/thickbox/">ThickBox</a> successor). The problem with iframes is they require a complete HTML document. ContextSwitch just wasn&#8217;t cutting it as I needed some kind of layout but obviously not the full blown, menus and all default.</p>
<p>The solution was to implement my own custom context, complete with a post-processing callback function to alter the layout script. Here&#8217;s how I&#8217;ve done it.</p>
<p>First, I added a static method to my Bootstrap class to add a suffix to the layout script name should a context be present.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> setLayoutContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$layout</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;">;</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;">$layout</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$layout</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$context</span> <span style="color: #339933;">=</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;">'ContextSwitch'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCurrentContext</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: #009900; font-weight: bold;">null</span> <span style="color: #339933;">!==</span> <span style="color: #000088;">$context</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$layout</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLayout</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$layout</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLayout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$context</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>For example, given the default layout script <code>layout.phtml</code> and context <code>iframe</code>, this method will set the layout script to <code>layout.iframe.phtml</code></p>
<p>Now I just need to add the context to the action helper. Again, in my Bootstrap class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> setupActionHelpers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// This method is called during application bootstrapping</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Add &quot;iframe&quot; context</span>
    <span style="color: #000088;">$iframeContext</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'suffix'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'iframe'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'callbacks'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'post'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'setLayoutContext'</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$contextSwitch</span> <span style="color: #339933;">=</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;">'ContextSwitch'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$contextSwitch</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addContext</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'iframe'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$iframeContext</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Ok, that&#8217;s all the preparation work out of the way. To add this context to a controller action, you simply, well, add it.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action
<span style="color: #009900;">&#123;</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>_helper<span style="color: #339933;">-&gt;</span><span style="color: #004000;">contextSwitch</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addActionContext</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'config'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'iframe'</span><span style="color: #009900;">&#41;</span>
                                     <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAutoDisableLayout</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
                                     <span style="color: #339933;">-&gt;</span><span style="color: #004000;">initContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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> configAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Controller tasks here</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>All that&#8217;s left to do is ensure your links to the context enabled controller action include the correct <code>format</code> parameter, for example <code>/my/config/format/iframe</code> or <code>/my/config?format=iframe</code>.</p>
<p>One thing I&#8217;ve noticed is that the ContextSwitch helper should be configured <em>after</em> your view and layout are initialised.</p>
<p>Thanks to Jeff Carouth for the <a href="http://carouth.com/blog/focus/context-switching-layouts-with-the-zend-framework--26">inspiration</a> for this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.philipbrown.id.au/2009/02/changing-layouts-with-zend-contextswitch/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Zend Framework Forms and magic_quotes_gpc</title>
		<link>http://blog.philipbrown.id.au/2008/10/zend-framework-forms-and-magic_quotes_gpc/</link>
		<comments>http://blog.philipbrown.id.au/2008/10/zend-framework-forms-and-magic_quotes_gpc/#comments</comments>
		<pubDate>Fri, 10 Oct 2008 02:05:45 +0000</pubDate>
		<dc:creator>Phil</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[magic quotes]]></category>
		<category><![CDATA[magic_quotes_gpc]]></category>
		<category><![CDATA[stripslashes]]></category>

		<guid isPermaLink="false">http://morecowbell.net.au/?p=27</guid>
		<description><![CDATA[Introducing the bane of my existence &#8211; PHP&#8217;s magic_quotes_gpc.
I won&#8217;t bore you with the details suffice to say that this is one of PHP&#8217;s greatest failings and I cannot wait until the world adopts PHP6 and this awful blight is removed forever. Until then, we&#8217;ll just have to deal with it.
Now, there are various ways [...]]]></description>
			<content:encoded><![CDATA[<p>Introducing the bane of my existence &#8211; PHP&#8217;s <a href="http://au.php.net/manual/en/info.configuration.php#ini.magic-quotes-gpc">magic_quotes_gpc</a>.</p>
<p>I won&#8217;t bore you with the details suffice to say that this is one of PHP&#8217;s greatest failings and I cannot wait until the world adopts PHP6 and this awful blight is removed forever. Until then, we&#8217;ll just have to deal with it.</p>
<p>Now, there are various ways of disabling this &#8220;feature&#8221; as outlined <a href="http://php.net/manual/en/security.magicquotes.disabling.php">here</a> but what if that isn&#8217;t an option? My particular host of choice, while being excellent in every other regard, has this little blighter enabled and runs PHP as a CGI via the suExec module. This means no <code>php_flag</code> or <code>php_value</code> entries in the <code>.htaccess</code> file and the current implementation of allowing custom <code>php.ini</code> files is totally broken (server <code>php.ini</code> items do not cascade through to the custom one, meaning paths are wrong, extensions aren&#8217;t loaded, etc). This prompted me to implement something in code which is what I&#8217;ll be sharing with you today.</p>
<p>The code solution to magic quotes is to use <code>stripslashes()</code>. The simplest way of getting this into the Zend Framework is to create a filter.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MoreCowbell_Filter_StripSlashes implements Zend_Filter_Interface
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> filter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">get_magic_quotes_gpc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_clean<span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    protected <span style="color: #000000; font-weight: bold;">function</span> _clean<span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'_clean'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</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>This filter allows us to process not only strings but array values as well. Note that the filter only activates if magic quotes is enabled.</p>
<p>All my forms extend my own custom Zend_Form extension which simply provides some shortcuts to my preferred decorator scheme. I&#8217;ll add some extra shortcuts for the new filter.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MoreCowbell_Form <span style="color: #000000; font-weight: bold;">extends</span> Zend_Form
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$elementDecorators</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'ViewHelper'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'Description'</span><span style="color: #339933;">,</span>
        <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Label'</span><span style="color: #339933;">,</span>   <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'requiredSuffix'</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: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Errors'</span><span style="color: #339933;">,</span>  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'placement'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'PREPEND'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HtmlTag'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'li'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$buttonDecorators</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'ViewHelper'</span><span style="color: #339933;">,</span>
        <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HtmlTag'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'li'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$groupDecorators</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'FormElements'</span><span style="color: #339933;">,</span>
        <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HtmlTag'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ol'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'Fieldset'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Configure path to custom plugins</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$elementPrefixPaths</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'filter'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'prefix'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'MoreCowbell_Filter'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'path'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'MoreCowbell/Filter'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Shortcut to default element filters</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$elementFilters</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'StripSlashes'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadDefaultDecorators<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;">setDecorators</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'FormElements'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'Form'</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: #009900;">&#125;</span></pre></div></div>

<p>All that&#8217;s left to do is assign the appropriate configuration settings in your forms and all your input data will be purged of the magic quotes evil. Here&#8217;s an overly simple example of a blog entry form.</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_Blog_Form <span style="color: #000000; font-weight: bold;">extends</span> MoreCowbell_Form
<span style="color: #009900;">&#123;</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;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'blog_title'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'label'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Title'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'required'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'prefixPath'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">elementPrefixPaths</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'decorators'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">elementDecorators</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'filters'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">elementFilters</span>
        <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'textarea'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'blog_post'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'label'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Post'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'required'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'rows'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'cols'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'prefixPath'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">elementPrefixPaths</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'decorators'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">elementDecorators</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'filters'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">elementFilters</span>
        <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'submit'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'submit_btn'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'label'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Submit'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'decorators'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">buttonDecorators</span>
        <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDisplayGroup</span><span style="color: #009900;">&#40;</span>
            <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'blog_title'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'blog_post'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'submit_btn'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'blog'</span><span style="color: #339933;">,</span>
            <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'legend'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Blog Entry'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'decorators'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">groupDecorators</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: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.philipbrown.id.au/2008/10/zend-framework-forms-and-magic_quotes_gpc/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Zend Framework: Automatically fetch the grand total from a limit query</title>
		<link>http://blog.philipbrown.id.au/2008/08/zend-framework-automatically-fetch-the-grand-total-from-a-limit-query/</link>
		<comments>http://blog.philipbrown.id.au/2008/08/zend-framework-automatically-fetch-the-grand-total-from-a-limit-query/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 02:29:56 +0000</pubDate>
		<dc:creator>Phil</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[limit]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[total]]></category>

		<guid isPermaLink="false">http://morecowbell.net.au/?p=3</guid>
		<description><![CDATA[September 11, 2008 &#8211; Updated for Zend Framework 1.6
So you&#8217;ve got a nice limited result set for use in a paginated display. The thing with most pagination widgets is they need to know the grand total for determining the total number of pages.
What you would normally have to do is manually issue a second query [...]]]></description>
			<content:encoded><![CDATA[<p><em>September 11, 2008 &#8211; Updated for Zend Framework 1.6</em></p>
<p>So you&#8217;ve got a nice limited result set for use in a paginated display. The thing with most pagination widgets is they need to know the grand total for determining the total number of pages.</p>
<p>What you would normally have to do is manually issue a second query to fetch the total based on the same parameters as the original query, minus the limit and offset (and order but that&#8217;s beside the point). Wouldn&#8217;t it be nice if this could all be done behind the scenes?</p>
<p>Enter the More Cowbell solution!</p>
<p>For the example, I&#8217;ll be assuming you&#8217;re using an implementation of the Zend_Db_Table_Abstract class and associated Zend_Db_Table_Rowset_Abstract however this solution is reasonably generic and can be easily applied to other situations.</p>
<p>First things first, lets extend the rowset class.</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>
<span style="color: #000000; font-weight: bold;">class</span> MoreCowbell_Db_Table_Rowset <span style="color: #000000; font-weight: bold;">extends</span> Zend_Db_Table_Rowset_Abstract
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * Grande total for limited result sets
     * 
     * @var int
     */</span>
    protected <span style="color: #000088;">$_total</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Constructor
     * 
     * @param array $config
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'total'</span><span style="color: #009900;">&#93;</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>_total <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'total'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_total <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_count<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Returns the grande total
     *
     * @return int
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> total<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_total<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This simple extension allows us to store an arbitrary integer representing the grande total inside the resultset object. Now, on to the meat and potatoes.</p>
<p>Your usual Zend_Db_Table_Abstract implementation is a fairly simple affair; define a table name, primary key(s), etc. We&#8217;re going to add just a little more abstraction for your tables to inherit.</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>
abstract <span style="color: #000000; font-weight: bold;">class</span> MoreCowbell_Db_Table_Abstract <span style="color: #000000; font-weight: bold;">extends</span> Zend_Db_Table_Abstract
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * Classname for rowset
     *
     * @var string
     */</span>
    protected <span style="color: #000088;">$_rowsetClass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'MoreCowbell_Db_Table_Rowset'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Fetches all rows.
     *
     * Honors the Zend_Db_Adapter fetch mode.
     *
     * @param string|array|Zend_Db_Table_Select $where  OPTIONAL An SQL WHERE clause or Zend_Db_Table_Select object.
     * @param string|array                      $order  OPTIONAL An SQL ORDER clause.
     * @param int                               $count  OPTIONAL An SQL LIMIT count.
     * @param int                               $offset OPTIONAL An SQL LIMIT offset.
     * @return Zend_Db_Table_Rowset_Abstract The row results per the Zend_Db_Adapter fetch mode.
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fetchAll<span style="color: #009900;">&#40;</span><span style="color: #000088;">$where</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$order</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$offset</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: #666666; font-style: italic;">// This is all straight out of Zend_Db_Table_Abstract::fetchAll()</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$where</span> instanceof Zend_Db_Table_Select<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$select</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</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;">;</span>
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$where</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;">$this</span><span style="color: #339933;">-&gt;</span>_where<span style="color: #009900;">&#40;</span><span style="color: #000088;">$select</span><span style="color: #339933;">,</span> <span style="color: #000088;">$where</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$order</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;">$this</span><span style="color: #339933;">-&gt;</span>_order<span style="color: #009900;">&#40;</span><span style="color: #000088;">$select</span><span style="color: #339933;">,</span> <span style="color: #000088;">$order</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$offset</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;">$select</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">limit</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span><span style="color: #339933;">,</span> <span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$select</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$where</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$rows</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_fetch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$select</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$data</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: #000088;">$this</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'data'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$rows</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'readOnly'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$select</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isReadOnly</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'rowClass'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_rowClass<span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'stored'</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: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Here's where it gets interesting</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$select</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPart</span><span style="color: #009900;">&#40;</span>Zend_Db_Select<span style="color: #339933;">::</span><span style="color: #004000;">LIMIT_COUNT</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$select</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPart</span><span style="color: #009900;">&#40;</span>Zend_Db_Select<span style="color: #339933;">::</span><span style="color: #004000;">LIMIT_OFFSET</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Limit exists, reset unnecessary parameters and columns and re-issue query returning the count</span>
            <span style="color: #000088;">$select</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reset</span><span style="color: #009900;">&#40;</span>Zend_Db_Select<span style="color: #339933;">::</span><span style="color: #004000;">LIMIT_COUNT</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">reset</span><span style="color: #009900;">&#40;</span>Zend_Db_Select<span style="color: #339933;">::</span><span style="color: #004000;">LIMIT_OFFSET</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">reset</span><span style="color: #009900;">&#40;</span>Zend_Db_Select<span style="color: #339933;">::</span><span style="color: #004000;">ORDER</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">reset</span><span style="color: #009900;">&#40;</span>Zend_Db_Select<span style="color: #339933;">::</span><span style="color: #004000;">COLUMNS</span><span style="color: #009900;">&#41;</span>
                   <span style="color: #339933;">-&gt;</span><span style="color: #004000;">columns</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'COUNT(1)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'total'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchOne</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$select</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #339933;">@</span>Zend_Loader<span style="color: #339933;">::</span><span style="color: #004000;">loadClass</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_rowsetClass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_rowsetClass<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>That&#8217;s it! I opted to reiterate a large chunk of Zend_Db_Table_Abstract::fetchAll() simply because we&#8217;d have to build the Zend_Db_Table_Select object anyway so why do it twice.</p>
<p>When you create your table objects, extend MoreCowbell_Db_Table_Abstract instead of Zend_Db_Table_Abstract. You can then use your table object as usual with the added bonus of if you issue a limit query that returns a resultset, you now have access to the total number of matches. Here&#8217;s a quick example.</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>
<span style="color: #000000; font-weight: bold;">class</span> My_Table <span style="color: #000000; font-weight: bold;">extends</span> MoreCowbell_Db_Table_Abstract
<span style="color: #009900;">&#123;</span>
    protected <span style="color: #000088;">$_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'my_table'</span><span style="color: #339933;">;</span>
&nbsp;
    protected <span style="color: #000088;">$_primary</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'id'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>


<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: #000088;">$table</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> My_Table<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$select</span> <span style="color: #339933;">=</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;">'status = ?'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'NEW'</span><span style="color: #009900;">&#41;</span>
                          <span style="color: #339933;">-&gt;</span><span style="color: #004000;">limit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Returns rows 21 through 30</span>
<span style="color: #666666; font-style: italic;">// In this example we'll assume there are 50 'NEW' rows present</span>
&nbsp;
<span style="color: #000088;">$rows</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$table</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchAll</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$select</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rows</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 10</span>
<span style="color: #000088;">$total</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rows</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">total</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 50</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.philipbrown.id.au/2008/08/zend-framework-automatically-fetch-the-grand-total-from-a-limit-query/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->