<?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>Rob Allen&#039;s DevNotes</title>
	<atom:link href="http://akrabat.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://akrabat.com</link>
	<description>Developing PHP software in the Real World, by Rob Allen</description>
	<lastBuildDate>Thu, 23 May 2013 02:19:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>injecting configuration into a ZF2 controller</title>
		<link>http://akrabat.com/zend-framework-2/injecting-configuration-into-a-zf2-controller/</link>
		<comments>http://akrabat.com/zend-framework-2/injecting-configuration-into-a-zf2-controller/#comments</comments>
		<pubDate>Tue, 30 Apr 2013 08:01:59 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Zend Framework 2]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=2799</guid>
		<description><![CDATA[One thing you may find yourself needing to do is access configuration information in a controller or service class. The easiest way to do this is to use the ServiceManger's initialiser feature. This allows you to write one piece of injection code that can be applied to multiple objects. It's easier to show this in [...]]]></description>
				<content:encoded><![CDATA[<p>One thing you may find yourself needing to do is access configuration information in a controller or service class.</p>
<p>The easiest way to do this is to use the <tt>ServiceManger</tt>'s <tt>initialiser</tt> feature. This allows you to write one piece of injection code that can be applied to multiple objects. It's easier to show this in action!</p>
<p>Let's assume that we have this configuration file:</p>
<p><strong>config/autoload/global.php:</strong></p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw1">return</span> <span class="kw3">array</span><span class="br0">&#40;</span>
    <span class="st_h">'application'</span> <span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span>
        <span class="st_h">'setting_1'</span> <span class="sy0">=&gt;</span> <span class="nu0">234</span><span class="sy0">,</span>
    <span class="br0">&#41;</span>
<span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>That is, we have a key called <tt>'application'</tt> that contains application-specific configuration that we would like to access in our controllers (or service classes). </p>
<p>Firstly we define a interface, ConfigAwareInterface:</p>
<p><strong>module/Application/src/Application/ConfigAwareInterface.php:</strong></p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">namespace</span> Application<span class="sy0">;</span>
&nbsp;
<span class="kw2">interface</span> ConfigAwareInterface
<span class="br0">&#123;</span>
    <span class="kw2">public</span> <span class="kw2">function</span> setConfig<span class="br0">&#40;</span><span class="re0">$config</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>We can now add this to a controller:</p>
<p><strong>module/Application/src/Application/Controller/IndexController.php:</strong></p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">namespace</span> Application\Controller<span class="sy0">;</span>
&nbsp;
<span class="kw2">use</span> Zend\Mvc\Controller\AbstractActionController<span class="sy0">;</span>
<span class="kw2">use</span> Zend\View\Model\ViewModel<span class="sy0">;</span>
<span class="kw2">use</span> Application\ConfigAwareInterface<span class="sy0">;</span>
&nbsp;
<span class="kw2">class</span> IndexController <span class="kw2">extends</span> AbstractActionController
    implements ConfigAwareInterface
<span class="br0">&#123;</span>
    <span class="kw2">protected</span> <span class="re0">$config</span><span class="sy0">;</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> setConfig<span class="br0">&#40;</span><span class="re0">$config</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">config</span> <span class="sy0">=</span> <span class="re0">$config</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="co1">// action methods, etc.</span>
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>In the controller, we add a use statement, implement our interface and the required <tt>setConfig()</tt> method.</p>
<p>Finally, we add an <tt>initializer</tt> to the <tt>Module</tt> class:</p>
<p><strong>module/Application/Module.php:</strong></p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">class</span> Module
<span class="br0">&#123;</span>
    <span class="co1">// Other methods, such as OnBoostrap(), getAutoloaderConfig(), etc.</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> getControllerConfig<span class="br0">&#40;</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="kw1">return</span> <span class="kw3">array</span><span class="br0">&#40;</span>
             <span class="st_h">'initializers'</span> <span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span>
                <span class="kw2">function</span> <span class="br0">&#40;</span><span class="re0">$instance</span><span class="sy0">,</span> <span class="re0">$sm</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                    <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$instance</span> instanceof ConfigAwareInterface<span class="br0">&#41;</span> <span class="br0">&#123;</span>
                        <span class="re0">$locator</span> <span class="sy0">=</span> <span class="re0">$sm</span><span class="sy0">-&gt;</span><span class="me1">getServiceLocator</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
                        <span class="re0">$config</span>  <span class="sy0">=</span> <span class="re0">$locator</span><span class="sy0">-&gt;</span><span class="me1">get</span><span class="br0">&#40;</span><span class="st_h">'Config'</span><span class="br0">&#41;</span><span class="sy0">;</span>
                        <span class="re0">$instance</span><span class="sy0">-&gt;</span><span class="me1">setConfig</span><span class="br0">&#40;</span><span class="re0">$config</span><span class="br0">&#91;</span><span class="st_h">'application'</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span>
                    <span class="br0">&#125;</span>
                <span class="br0">&#125;</span>
            <span class="br0">&#41;</span>
        <span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>(We also have a <tt>use Application\ConfigAwareInterface;</tt> statement at the top!)</p>
<p>As <tt>getControllerConfig()</tt> is used by a specific <tt>ServiceManager</tt> only for controllers, we need to retrieve the main <tt>ServiceManager</tt> using <tt>getServiceLocator()</tt> in order to collect the merged configuration. As we only want the settings from within the <tt>'application'</tt> key, we only pass that into the controller's <tt>setConfig()</tt> method.</p>
<p>The configuration settings are now available to us in any controller that implements <tt>ConfigAwareInterface</tt>.</p>
<p>We can also do this for service classes - we simply add another <tt>initalizer</tt> to the <tt>Module</tt>:</p>
<p><strong>module/Application/Module.php:</strong></p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">    <span class="kw2">public</span> <span class="kw2">function</span> getServiceConfig<span class="br0">&#40;</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="kw1">return</span> <span class="kw3">array</span><span class="br0">&#40;</span>
             <span class="st_h">'initializers'</span> <span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span>
                <span class="kw2">function</span> <span class="br0">&#40;</span><span class="re0">$instance</span><span class="sy0">,</span> <span class="re0">$sm</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                    <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$instance</span> instanceof ConfigAwareInterface<span class="br0">&#41;</span> <span class="br0">&#123;</span>
                        <span class="re0">$config</span>  <span class="sy0">=</span> <span class="re0">$sm</span><span class="sy0">-&gt;</span><span class="me1">get</span><span class="br0">&#40;</span><span class="st_h">'Config'</span><span class="br0">&#41;</span><span class="sy0">;</span>
                        <span class="re0">$instance</span><span class="sy0">-&gt;</span><span class="me1">setConfig</span><span class="br0">&#40;</span><span class="re0">$config</span><span class="br0">&#91;</span><span class="st_h">'application'</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span>
                    <span class="br0">&#125;</span>
                <span class="br0">&#125;</span>
            <span class="br0">&#41;</span>
        <span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>It also follows that you can use <tt>initializers</tt> for any type of generic injection, such as mappers, db adapters, loggers, etc. Simply create an <tt>interface</tt> and write an <tt>initalizer</tt>.</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/zend-framework-2/injecting-configuration-into-a-zf2-controller/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fzend-framework-2%2Finjecting-configuration-into-a-zf2-controller%2F&amp;language=en_GB&amp;category=text&amp;title=injecting+configuration+into+a+ZF2+controller&amp;description=One+thing+you+may+find+yourself+needing+to+do+is+access+configuration+information+in+a+controller+or+service+class.+The+easiest+way+to+do+this+is+to+use+the+ServiceManger%27s...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>Adding Diff syntax highlighting to Sublime Text</title>
		<link>http://akrabat.com/software/adding-diff-syntax-highlighting-to-sublime-text/</link>
		<comments>http://akrabat.com/software/adding-diff-syntax-highlighting-to-sublime-text/#comments</comments>
		<pubDate>Fri, 26 Apr 2013 07:47:48 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=2790</guid>
		<description><![CDATA[My chosen colour scheme for Sublime Text doesn't include support for diff/patch files, so I added my own. To the bottom of my .tmTheme file, I added this just above the closing &#60;/array&#62;: &#60;dict&#62; &#60;key&#62;name&#60;/key&#62; &#60;string&#62;diff.header&#60;/string&#62; &#60;key&#62;scope&#60;/key&#62; &#60;string&#62;meta.diff, meta.diff.header&#60;/string&#62; &#60;key&#62;settings&#60;/key&#62; &#60;/dict&#62;&#60;dict&#62; &#60;key&#62;foreground&#60;/key&#62; &#60;string&#62;#009933&#60;/string&#62; &#60;/dict&#62; &#160; &#60;dict&#62; &#60;key&#62;name&#60;/key&#62; &#60;string&#62;diff.deleted&#60;/string&#62; &#60;key&#62;scope&#60;/key&#62; &#60;string&#62;markup.deleted&#60;/string&#62; &#60;key&#62;settings&#60;/key&#62; &#60;/dict&#62;&#60;dict&#62; &#60;key&#62;foreground&#60;/key&#62; &#60;string&#62;#DD5555&#60;/string&#62; &#60;/dict&#62; [...]]]></description>
				<content:encoded><![CDATA[<p>My chosen colour scheme for <a href="http://www.sublimetext.com">Sublime Text</a> doesn't include support for diff/patch files, so I added my own.</p>
<p>To the bottom of my <tt>.tmTheme</tt> file, I added this just above the closing <tt>&lt;/array&gt;</tt>:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="xml"><pre class="de1">        <span class="sc3"><span class="re1">&lt;dict<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;key<span class="re2">&gt;</span></span></span>name<span class="sc3"><span class="re1">&lt;/key<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;string<span class="re2">&gt;</span></span></span>diff.header<span class="sc3"><span class="re1">&lt;/string<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;key<span class="re2">&gt;</span></span></span>scope<span class="sc3"><span class="re1">&lt;/key<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;string<span class="re2">&gt;</span></span></span>meta.diff, meta.diff.header<span class="sc3"><span class="re1">&lt;/string<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;key<span class="re2">&gt;</span></span></span>settings<span class="sc3"><span class="re1">&lt;/key<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;/dict<span class="re2">&gt;</span></span><span class="re1">&lt;dict<span class="re2">&gt;</span></span></span>
                <span class="sc3"><span class="re1">&lt;key<span class="re2">&gt;</span></span></span>foreground<span class="sc3"><span class="re1">&lt;/key<span class="re2">&gt;</span></span></span>
                <span class="sc3"><span class="re1">&lt;string<span class="re2">&gt;</span></span></span>#009933<span class="sc3"><span class="re1">&lt;/string<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;/dict<span class="re2">&gt;</span></span></span>
&nbsp;
        <span class="sc3"><span class="re1">&lt;dict<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;key<span class="re2">&gt;</span></span></span>name<span class="sc3"><span class="re1">&lt;/key<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;string<span class="re2">&gt;</span></span></span>diff.deleted<span class="sc3"><span class="re1">&lt;/string<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;key<span class="re2">&gt;</span></span></span>scope<span class="sc3"><span class="re1">&lt;/key<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;string<span class="re2">&gt;</span></span></span>markup.deleted<span class="sc3"><span class="re1">&lt;/string<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;key<span class="re2">&gt;</span></span></span>settings<span class="sc3"><span class="re1">&lt;/key<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;/dict<span class="re2">&gt;</span></span><span class="re1">&lt;dict<span class="re2">&gt;</span></span></span>
                <span class="sc3"><span class="re1">&lt;key<span class="re2">&gt;</span></span></span>foreground<span class="sc3"><span class="re1">&lt;/key<span class="re2">&gt;</span></span></span>
                <span class="sc3"><span class="re1">&lt;string<span class="re2">&gt;</span></span></span>#DD5555<span class="sc3"><span class="re1">&lt;/string<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;/dict<span class="re2">&gt;</span></span></span>
&nbsp;
        <span class="sc3"><span class="re1">&lt;dict<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;key<span class="re2">&gt;</span></span></span>name<span class="sc3"><span class="re1">&lt;/key<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;string<span class="re2">&gt;</span></span></span>diff.inserted<span class="sc3"><span class="re1">&lt;/string<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;key<span class="re2">&gt;</span></span></span>scope<span class="sc3"><span class="re1">&lt;/key<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;string<span class="re2">&gt;</span></span></span>markup.inserted<span class="sc3"><span class="re1">&lt;/string<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;key<span class="re2">&gt;</span></span></span>settings<span class="sc3"><span class="re1">&lt;/key<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;/dict<span class="re2">&gt;</span></span><span class="re1">&lt;dict<span class="re2">&gt;</span></span></span>
                <span class="sc3"><span class="re1">&lt;key<span class="re2">&gt;</span></span></span>foreground<span class="sc3"><span class="re1">&lt;/key<span class="re2">&gt;</span></span></span>
                <span class="sc3"><span class="re1">&lt;string<span class="re2">&gt;</span></span></span>#3333FF<span class="sc3"><span class="re1">&lt;/string<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;/dict<span class="re2">&gt;</span></span></span>
&nbsp;
        <span class="sc3"><span class="re1">&lt;dict<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;key<span class="re2">&gt;</span></span></span>name<span class="sc3"><span class="re1">&lt;/key<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;string<span class="re2">&gt;</span></span></span>diff.changed<span class="sc3"><span class="re1">&lt;/string<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;key<span class="re2">&gt;</span></span></span>scope<span class="sc3"><span class="re1">&lt;/key<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;string<span class="re2">&gt;</span></span></span>markup.changed<span class="sc3"><span class="re1">&lt;/string<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;key<span class="re2">&gt;</span></span></span>settings<span class="sc3"><span class="re1">&lt;/key<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;/dict<span class="re2">&gt;</span></span><span class="re1">&lt;dict<span class="re2">&gt;</span></span></span>
                <span class="sc3"><span class="re1">&lt;key<span class="re2">&gt;</span></span></span>foreground<span class="sc3"><span class="re1">&lt;/key<span class="re2">&gt;</span></span></span>
                <span class="sc3"><span class="re1">&lt;string<span class="re2">&gt;</span></span></span>#E6DB74<span class="sc3"><span class="re1">&lt;/string<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;/dict<span class="re2">&gt;</span></span></span></pre></div></div></div></div></div></div></div>


<p>This sets up a green colour for the meta information, blue for added lines, red for deleted lines and a yellowish colour for changed.</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/software/adding-diff-syntax-highlighting-to-sublime-text/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fsoftware%2Fadding-diff-syntax-highlighting-to-sublime-text%2F&amp;language=en_GB&amp;category=text&amp;title=Adding+Diff+syntax+highlighting+to+Sublime+Text&amp;description=My+chosen+colour+scheme+for+Sublime+Text+doesn%27t+include+support+for+diff%2Fpatch+files%2C+so+I+added+my+own.+To+the+bottom+of+my+.tmTheme+file%2C+I+added+this+just+above...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>Simple logging of ZF2 exceptions</title>
		<link>http://akrabat.com/zend-framework-2/simple-logging-of-zf2-exceptions/</link>
		<comments>http://akrabat.com/zend-framework-2/simple-logging-of-zf2-exceptions/#comments</comments>
		<pubDate>Wed, 24 Apr 2013 09:58:40 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Zend Framework 2]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=2787</guid>
		<description><![CDATA[I recently had a problem with a ZF2 based website where users were reporting seeing the error page displayed, but I couldn't reproduce in testing. To find this problem I decided to log every exception to a file so I could then go back and work out what was happening. In a standard ZF2 application, [...]]]></description>
				<content:encoded><![CDATA[<p>I recently had a problem with a ZF2 based website where users were reporting seeing the error page displayed, but I couldn't reproduce in testing. To find this problem I decided to log every exception to a file so I could then go back and work out what was happening. In a standard ZF2 application, the easiest way to do this is to add a listener to the 'dispatch.error' event and log using <tt>Zend\Log</tt>.</p>
<p>To do this, I started with the Application's <tt>Module</tt> class and added an event listener:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">    <span class="kw2">public</span> <span class="kw2">function</span> onBootstrap<span class="br0">&#40;</span><span class="re0">$e</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$eventManager</span> <span class="sy0">=</span> <span class="re0">$e</span><span class="sy0">-&gt;</span><span class="me1">getApplication</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">getEventManager</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="re0">$eventManager</span><span class="sy0">-&gt;</span><span class="me1">attach</span><span class="br0">&#40;</span><span class="st_h">'dispatch.error'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="re0">$event</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
            <span class="re0">$exception</span> <span class="sy0">=</span> <span class="re0">$event</span><span class="sy0">-&gt;</span><span class="me1">getResult</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">exception</span><span class="sy0">;</span>
            <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$exception</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="re0">$sm</span> <span class="sy0">=</span> <span class="re0">$event</span><span class="sy0">-&gt;</span><span class="me1">getApplication</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">getServiceManager</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
                <span class="re0">$service</span> <span class="sy0">=</span> <span class="re0">$sm</span><span class="sy0">-&gt;</span><span class="me1">get</span><span class="br0">&#40;</span><span class="st_h">'Application\Service\ErrorHandling'</span><span class="br0">&#41;</span><span class="sy0">;</span>
                <span class="re0">$service</span><span class="sy0">-&gt;</span><span class="me1">logException</span><span class="br0">&#40;</span><span class="re0">$exception</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="br0">&#125;</span>
        <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>This code attaches an anonymous function to the 'dispatch.error' event which retrieves the exception from the event's result and passes it to the <tt>logException()</tt> method in an <tt>ErrorHandling</tt> class. We retrieve ErrorHandling from the service manager which allows us to inject an instance of <tt>Zend\Log</tt> into it:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">    <span class="kw2">public</span> <span class="kw2">function</span> getServiceConfig<span class="br0">&#40;</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="kw1">return</span> <span class="kw3">array</span><span class="br0">&#40;</span>
            <span class="st_h">'factories'</span> <span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span>
                <span class="st_h">'Application\Service\ErrorHandling'</span> <span class="sy0">=&gt;</span>  <span class="kw2">function</span><span class="br0">&#40;</span><span class="re0">$sm</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                    <span class="re0">$logger</span> <span class="sy0">=</span> <span class="re0">$sm</span><span class="sy0">-&gt;</span><span class="me1">get</span><span class="br0">&#40;</span><span class="st_h">'Zend\Log'</span><span class="br0">&#41;</span><span class="sy0">;</span>
                    <span class="re0">$service</span> <span class="sy0">=</span> <span class="kw2">new</span> ErrorHandlingService<span class="br0">&#40;</span><span class="re0">$logger</span><span class="br0">&#41;</span><span class="sy0">;</span>
                    <span class="kw1">return</span> <span class="re0">$service</span><span class="sy0">;</span>
                <span class="br0">&#125;</span><span class="sy0">,</span>
                <span class="st_h">'Zend\Log'</span> <span class="sy0">=&gt;</span> <span class="kw2">function</span> <span class="br0">&#40;</span><span class="re0">$sm</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                    <span class="re0">$filename</span> <span class="sy0">=</span> <span class="st_h">'log_'</span> <span class="sy0">.</span> <span class="kw3">date</span><span class="br0">&#40;</span><span class="st_h">'F'</span><span class="br0">&#41;</span> <span class="sy0">.</span> <span class="st_h">'.txt'</span><span class="sy0">;</span>
                    <span class="re0">$log</span> <span class="sy0">=</span> <span class="kw2">new</span> Logger<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
                    <span class="re0">$writer</span> <span class="sy0">=</span> <span class="kw2">new</span> LogWriterStream<span class="br0">&#40;</span><span class="st_h">'./data/logs/'</span> <span class="sy0">.</span> <span class="re0">$filename</span><span class="br0">&#41;</span><span class="sy0">;</span>
                    <span class="re0">$log</span><span class="sy0">-&gt;</span><span class="me1">addWriter</span><span class="br0">&#40;</span><span class="re0">$writer</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
                    <span class="kw1">return</span> <span class="re0">$log</span><span class="sy0">;</span>
                <span class="br0">&#125;</span><span class="sy0">,</span>
            <span class="br0">&#41;</span><span class="sy0">,</span>
        <span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>There's obviously a few use statements at the top of the file for this to work:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">use</span> Application\Service\ErrorHandling <span class="kw1">as</span> ErrorHandlingService<span class="sy0">;</span>
<span class="kw2">use</span> Zend\<span class="kw3">Log</span>\Logger<span class="sy0">;</span>
<span class="kw2">use</span> Zend\<span class="kw3">Log</span>\Writer\Stream <span class="kw1">as</span> LogWriterStream<span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>The logging itself is done within the <tt>ErrorHandling</tt> class:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">namespace</span> Application\Service<span class="sy0">;</span>
&nbsp;
<span class="kw2">class</span> ErrorHandling
<span class="br0">&#123;</span>
    <span class="kw2">protected</span> <span class="re0">$logger</span><span class="sy0">;</span>
&nbsp;
    <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$logger</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">logger</span> <span class="sy0">=</span> <span class="re0">$logger</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="kw2">function</span> logException<span class="br0">&#40;</span>\Exception <span class="re0">$e</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$trace</span> <span class="sy0">=</span> <span class="re0">$e</span><span class="sy0">-&gt;</span><span class="me1">getTraceAsString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="re0">$i</span> <span class="sy0">=</span> <span class="nu0">1</span><span class="sy0">;</span>
        <span class="kw1">do</span> <span class="br0">&#123;</span>
            <span class="re0">$messages</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="re0">$i</span><span class="sy0">++</span> <span class="sy0">.</span> <span class="st0">&quot;: &quot;</span> <span class="sy0">.</span> <span class="re0">$e</span><span class="sy0">-&gt;</span><span class="me1">getMessage</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span> <span class="kw1">while</span> <span class="br0">&#40;</span><span class="re0">$e</span> <span class="sy0">=</span> <span class="re0">$e</span><span class="sy0">-&gt;</span><span class="me1">getPrevious</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="re0">$log</span> <span class="sy0">=</span> <span class="st0">&quot;Exception:<span class="es1">\n</span>&quot;</span> <span class="sy0">.</span> <span class="kw3">implode</span><span class="br0">&#40;</span><span class="st0">&quot;<span class="es1">\n</span>&quot;</span><span class="sy0">,</span> <span class="re0">$messages</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="re0">$log</span> <span class="sy0">.=</span> <span class="st0">&quot;<span class="es1">\n</span>Trace:<span class="es1">\n</span>&quot;</span> <span class="sy0">.</span> <span class="re0">$trace</span><span class="sy0">;</span>
&nbsp;
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">logger</span><span class="sy0">-&gt;</span><span class="me1">err</span><span class="br0">&#40;</span><span class="re0">$log</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>The <tt>logException</tt> method simply creates a string containing the exception's message along with any previous exception messages and the trace. We then call the <tt>Log</tt>'s <tt>err</tt> method to store the log and can peruse at our leisure.</p>
<p><strong>Update:</strong> I have updated the <tt>logException</tt> method to use a do..while() loop as it's neater and doesn't cause a an out-of-memory error that the previous code did. That is, it's a good idea to reuse the same variable when calling <tt>getPrevious()</tt>!</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/zend-framework-2/simple-logging-of-zf2-exceptions/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fzend-framework-2%2Fsimple-logging-of-zf2-exceptions%2F&amp;language=en_GB&amp;category=text&amp;title=Simple+logging+of+ZF2+exceptions&amp;description=I+recently+had+a+problem+with+a+ZF2+based+website+where+users+were+reporting+seeing+the+error+page+displayed%2C+but+I+couldn%27t+reproduce+in+testing.+To+find+this+problem+I...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>Notes on embedding fonts in rst2pdf</title>
		<link>http://akrabat.com/software/notes-on-embedding-fonts-in-rst2pdf/</link>
		<comments>http://akrabat.com/software/notes-on-embedding-fonts-in-rst2pdf/#comments</comments>
		<pubDate>Thu, 18 Apr 2013 10:49:16 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=2772</guid>
		<description><![CDATA[I wanted to use Helvetica Light in a PDF that I'm creating using rst2pdf and this proved a little tricker than I expected. In this case, I discovered that setting stdFont: Helvetica-Light or stdFont: HelveticaLight in the style file resulted in Verdana being used in the PDF file! Fortunately, the -v switch to rst2pdf is [...]]]></description>
				<content:encoded><![CDATA[<p>I wanted to use Helvetica Light in a PDF that I'm creating using <a href="http://rst2pdf.ralsina.com.ar">rst2pdf</a> and this proved a little tricker than I expected.</p>
<p>In this case, I discovered that setting <tt>stdFont: Helvetica-Light </tt> or <tt>stdFont: HelveticaLight</tt> in the style file resulted in Verdana being used in the PDF file!</p>
<p>Fortunately, the <tt>-v</tt> switch to rst2pdf is your friend when you want to know what's happening:</p>
<pre>
[INFO] findfonts.py:270 Trying to embed Helvetica-Light
[INFO] findfonts.py:307 fname for findTTFont: Helvetica-Light
[INFO] findfonts.py:308 Variants via findTTFont: ['/Library/Fonts/Microsoft/Verdana.ttf', '/Library/Fonts/Microsoft/Verdana Bold.ttf', '/Library/Fonts/Microsoft/Verdana Italic.ttf', '/Library/Fonts/Microsoft/Verdana Bold Italic.ttf']
[INFO] findfonts.py:317 Registering font: Verdana from /Library/Fonts/Microsoft/Verdana.ttf
[INFO] findfonts.py:317 Registering font: Verdana Bold from /Library/Fonts/Microsoft/Verdana Bold.ttf
[INFO] findfonts.py:317 Registering font: Verdana Italic from /Library/Fonts/Microsoft/Verdana Italic.ttf
[INFO] findfonts.py:317 Registering font: Verdana Bold Italic from /Library/Fonts/Microsoft/Verdana Bold Italic.ttf
[INFO] findfonts.py:329 Embedding via findTTFont as ['Verdana', 'Verdana Bold', 'Verdana Italic', 'Verdana Bold Italic']

</pre>
<p>As can be seen by the output, rst2pdf can't find Helvetica-Light and so has substituted Verdana instead. This would seem to be because it couldn't find a TTF file for the font on my Mac as Helvetica Light is stored in a dfont file. </p>
<p>To solve this, I used <a href="http://peter.upfold.org.uk/projects/dfontsplitter">DFontSplitter</a> to extract Helvetica Light as a separate TTF file and added this as an embedded font to the style file:</p>
<pre>
embeddedFonts: []
    [fonts/HelveticaLight.ttf, fonts/HelveticaLight.ttf, fonts/HelveticaLight.ttf, fonts/HelveticaLight.ttf]

</pre>
<p>and re-ran with <tt>-v</tt> which gave me this information:</p>
<pre>[INFO] styles.py:276 Registering font: fonts/HelveticaLight from ./fonts/HelveticaLight.ttf</pre>
<p>I now know that the name to use when referencing this font is "fonts/HelveticaLight" and so I can use it within the style file like this:</p>
<pre>
stdFont: fonts/HelveticaLight
</pre>
<p>and the font is embedded as I expected.</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/software/notes-on-embedding-fonts-in-rst2pdf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fsoftware%2Fnotes-on-embedding-fonts-in-rst2pdf%2F&amp;language=en_GB&amp;category=text&amp;title=Notes+on+embedding+fonts+in+rst2pdf&amp;description=I+wanted+to+use+Helvetica+Light+in+a+PDF+that+I%27m+creating+using+rst2pdf+and+this+proved+a+little+tricker+than+I+expected.+In+this+case%2C+I+discovered+that+setting...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>Setting the umask when using Capistrano</title>
		<link>http://akrabat.com/development/setting-the-umask-when-using-capistrano/</link>
		<comments>http://akrabat.com/development/setting-the-umask-when-using-capistrano/#comments</comments>
		<pubDate>Tue, 16 Apr 2013 07:52:32 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=2770</guid>
		<description><![CDATA[This is one of those posts to remind me how I solved a problem last time! I've recently been using Capistrano for deployment and other remote tasks and it's proving quite useful. One problem I ran into was that the umask was being set to 022 when using Capistrano and 002 when I was ssh'd [...]]]></description>
				<content:encoded><![CDATA[<p>This is one of those posts to remind me how I solved a problem last time!</p>
<p>I've recently been using <a href="https://github.com/capistrano/capistrano">Capistrano</a> for deployment and other remote tasks and it's proving quite useful.</p>
<p>One problem I ran into was that the <tt>umask</tt> was being set to <tt>022</tt> when using Capistrano and <tt>002</tt> when I was ssh'd into the server itself.</p>
<p>After a bit of research, I discovered that the secret is to put the <tt>umask</tt> statement in my <tt>.bashrc</tt> file <em>before</em> the line that says <tt>[ -z "$PS1" ] &#038;& return</tt> as when Capistrano logs into the server, it doesn't have an interactive shell (and so <tt>$PS1</tt> isn't set.</p>
<p>My <tt>.bashrc</tt> now looks like this:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1"><span class="co0"># ~/.bashrc: executed by bash(1) for non-login shells.</span>
<span class="co0"># see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)</span>
<span class="co0"># for examples</span>
&nbsp;
<span class="kw3">umask</span> 002
<span class="kw3">export</span> <span class="re2">PATH</span>=~<span class="sy0">/</span>bin:<span class="re1">$PATH</span>
&nbsp;
<span class="co0"># If not running interactively, don't do anything</span>
<span class="br0">&#91;</span> <span class="re5">-z</span> <span class="st0">&quot;<span class="es2">$PS1</span>&quot;</span> <span class="br0">&#93;</span> <span class="sy0">&amp;&amp;</span> <span class="kw3">return</span></pre></div></div></div></div></div></div></div>


<p>(This is on Ubuntu 12.04 LTS)</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/development/setting-the-umask-when-using-capistrano/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fdevelopment%2Fsetting-the-umask-when-using-capistrano%2F&amp;language=en_GB&amp;category=text&amp;title=Setting+the+umask+when+using+Capistrano&amp;description=This+is+one+of+those+posts+to+remind+me+how+I+solved+a+problem+last+time%21+I%27ve+recently+been+using+Capistrano+for+deployment+and+other+remote+tasks+and+it%27s+proving...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>Minding my own business</title>
		<link>http://akrabat.com/me/minding-my-own-business/</link>
		<comments>http://akrabat.com/me/minding-my-own-business/#comments</comments>
		<pubDate>Wed, 03 Apr 2013 14:08:20 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Me]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=2768</guid>
		<description><![CDATA[After nearly ten fantastic years at Big Room Internet, I have decided to take the jump and run my own business! Having concentrated on project management for the last year or so, I am happy to return to hands-on development. I also intend to provide training and consultancy services. I'm really excited by this new [...]]]></description>
				<content:encoded><![CDATA[<p>After nearly ten fantastic years at Big Room Internet, I have decided to take the jump and run my own business! Having concentrated on project management for the last year or so, I am happy to return to hands-on development. I also intend to provide training and consultancy services.</p>
<p>I'm really excited by this new phase of my career. I have created a shiny new company for interesting projects: <a href="http://nineteenfeet.com">Nineteen Feet Limited</a>. If you want to hire me, then please <a href="mailto:rob@nineteenfeet.com">get in touch</a>.</p>
<p>Over the years, I've discovered that I most enjoy developing directly for a client and building solutions that meet their specific needs. I've had great success teaching at conferences (such as PHPNW and ZendCon) and am looking forward to providing bespoke Zend Framework training to developers.</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/me/minding-my-own-business/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fme%2Fminding-my-own-business%2F&amp;language=en_GB&amp;category=text&amp;title=Minding+my+own+business&amp;description=After+nearly+ten+fantastic+years+at+Big+Room+Internet%2C+I+have+decided+to+take+the+jump+and+run+my+own+business%21+Having+concentrated+on+project+management+for+the+last+year...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>Objects in the model layer: Part 2</title>
		<link>http://akrabat.com/php/objects-in-the-model-layer-part-2/</link>
		<comments>http://akrabat.com/php/objects-in-the-model-layer-part-2/#comments</comments>
		<pubDate>Mon, 01 Apr 2013 13:50:56 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=2758</guid>
		<description><![CDATA[I previously talked about the terms I use for objects in the model layer and now it's time to put some code on those bones. Note that,as always, all code here is example code and not production-ready. An entity My entities are plain old PHP objects: namespace Book; &#160; class Entity &#123; protected $id; protected [...]]]></description>
				<content:encoded><![CDATA[<p>I previously talked about the terms I use for <a href="http://akrabat.com/development/objects-in-the-model-layer/">objects in the model layer</a> and now it's time to put some code on those bones. Note that,as always, all code here is example code and not production-ready.</p>
<h3>An entity</h3>
<p>My entities are plain old PHP objects:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">namespace</span> Book<span class="sy0">;</span>
&nbsp;
<span class="kw2">class</span> Entity
<span class="br0">&#123;</span>
    <span class="kw2">protected</span> <span class="re0">$id</span><span class="sy0">;</span>
    <span class="kw2">protected</span> <span class="re0">$author</span><span class="sy0">;</span>
    <span class="kw2">protected</span> <span class="re0">$title</span><span class="sy0">;</span>
    <span class="kw2">protected</span> <span class="re0">$isbn</span><span class="sy0">;</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$data</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">populate</span><span class="br0">&#40;</span><span class="re0">$data</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="co1">// Data transfer methods</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> populate<span class="br0">&#40;</span><span class="re0">$data</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw3">array_key_exists</span><span class="br0">&#40;</span><span class="st_h">'id'</span><span class="sy0">,</span> <span class="re0">$data</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">setId</span><span class="br0">&#40;</span><span class="re0">$data</span><span class="br0">&#91;</span><span class="st_h">'id'</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span>
        <span class="co1">// repeat for other properties</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> getArrayCopy<span class="br0">&#40;</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="kw1">return</span> <span class="kw3">array</span><span class="br0">&#40;</span>
            <span class="st_h">'id'</span>     <span class="sy0">=&gt;</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">getId</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span>
            <span class="co1">// repeat for other properties</span>
        <span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="co1">// Entity-specific methods</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> isValidIsbn<span class="br0">&#40;</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="co1">// validate ISBN and return true/false</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="co1">// Property getters and setters</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> getId<span class="br0">&#40;</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="kw1">return</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">id</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> setId<span class="br0">&#40;</span><span class="re0">$id</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">id</span> <span class="sy0">=</span> <span class="re0">$id</span><span class="sy0">;</span>
        <span class="kw1">return</span> <span class="re0">$this</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="co1">// Repeat for other properties...</span>
&nbsp;
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>There's nothing particularly complicated here. We have an object with a number of properties and some methods. This object represents a book, so we have properties of <tt>author</tt>, <tt>title</tt> and <tt>isbn</tt>. We need to be able to set and retrieve the properties so there is a get and set method for each one (only <tt>getId()</tt> and <tt>setId()</tt> are in the code snippet above to save space!)</p>
<p>Generally, I populate an entity from a mapper and use a pair of methods to do this: <tt>populate()</tt> and <tt>getArrayCopy()</tt>. These methods transfer the data in the properties to and from an array.</p>
<p>There are also entity-specific methods within the entity. For this object, I have a method called <tt>isValidIsbn()</tt>; for a user object, I may have a method called <tt>getFullName()</tt> which concatenates the user's first name and surname.</p>
<h3>A mapper</h3>
<p>The mapper knows how to load and save entities. This is a hand-rolled one:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">namespace</span> Book<span class="sy0">;</span>
&nbsp;
<span class="kw2">use</span> PDO<span class="sy0">;</span>
<span class="kw2">use</span> Book\Entity<span class="sy0">;</span>
&nbsp;
<span class="kw2">class</span> Mapper
<span class="br0">&#123;</span>
    <span class="kw2">protected</span> <span class="re0">$pdo</span><span class="sy0">;</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$dsn</span><span class="sy0">,</span> <span class="re0">$username</span><span class="sy0">,</span> <span class="re0">$password</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">pdo</span> <span class="sy0">=</span> <span class="kw2">new</span> PDO<span class="br0">&#40;</span><span class="re0">$dsn</span><span class="sy0">,</span> <span class="re0">$username</span><span class="sy0">,</span>  <span class="re0">$password</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> loadById<span class="br0">&#40;</span><span class="re0">$id</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$sql</span> <span class="sy0">=</span> <span class="st_h">'SELECT * FROM book WHERE id = :id'</span><span class="sy0">;</span>
&nbsp;
        <span class="re0">$statement</span> <span class="sy0">=</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">pdo</span><span class="sy0">-&gt;</span><span class="me1">prepare</span><span class="br0">&#40;</span><span class="re0">$sql</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="re0">$statement</span><span class="sy0">-&gt;</span><span class="me1">setFetchMode</span><span class="br0">&#40;</span>PDO<span class="sy0">::</span><span class="me2">FETCH_ASSOC</span><span class="br0">&#41;</span><span class="sy0">;</span>  
        <span class="re0">$statement</span><span class="sy0">-&gt;</span><span class="me1">execute</span><span class="br0">&#40;</span><span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">'id'</span> <span class="sy0">=&gt;</span> <span class="re0">$id</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="re0">$result</span> <span class="sy0">=</span> <span class="re0">$statement</span><span class="sy0">-&gt;</span><span class="me1">fetch</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$result</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="re0">$book</span> <span class="sy0">=</span> <span class="kw2">new</span> Entity<span class="br0">&#40;</span><span class="re0">$result</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="kw1">return</span> <span class="re0">$book</span><span class="sy0">;</span>
        <span class="br0">&#125;</span>
&nbsp;
        <span class="kw1">return</span> <span class="kw4">false</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> fetchAll<span class="br0">&#40;</span><span class="re0">$order</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="co1">// Select all books from database using PDO</span>
        <span class="co1">// iterate over each one and create a Book\Entity object</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> save<span class="br0">&#40;</span>Entity <span class="re0">$book</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$data</span> <span class="sy0">=</span> <span class="re0">$book</span><span class="sy0">-&gt;</span><span class="me1">getArrayCopy</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$data</span><span class="br0">&#91;</span><span class="st_h">'id'</span><span class="br0">&#93;</span> <span class="sy0">&gt;</span> <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="co1">// Update data in table using PDO and set $result</span>
        <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
            <span class="co1">// Insert data into table using PDO and set $result</span>
        <span class="br0">&#125;</span>
&nbsp;
        <span class="kw1">return</span> <span class="re0">$result</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> delete<span class="br0">&#40;</span><span class="re0">$id</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="co1">// Delete row in table using PDO</span>
    <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>In the mapper, I have methods that load with multiple entities and also ones that work on a single one. I like to use the method prefix "find" for methods that will return an array of entities and "load" for methods that return a single entity. This is just a stylistic thing, but I find it makes reading code easier. We then have <tt>save</tt> and <tt>delete</tt> methods that allow us to save and remove an entity from the data store.</p>
<p>This is just a skeleton of a specifically written mapper that users PDO. In a ZF2 application I use <a href="https://github.com/ZF-Commons/ZfcBase/blob/master/src/ZfcBase/Mapper/AbstractDbMapper.php">ZfcBase\Mapper\AbstractDbMapper</a> and in other applications I tend to abstract the common code into a base class and extend.</p>
<h3>Service objects</h3>
<p>Lastly, service objects provide the API to the rest of the application:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">namespace</span> Book<span class="sy0">;</span>
&nbsp;
<span class="kw2">use</span> Book\Mapper<span class="sy0">;</span>
&nbsp;
<span class="kw2">class</span> Service
<span class="br0">&#123;</span>
    <span class="kw2">protected</span> <span class="re0">$mapper</span><span class="sy0">;</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span>Mapper <span class="re0">$mapper</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">mapper</span> <span class="sy0">=</span> <span class="re0">$mapper</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> fetchAllByTitle<span class="br0">&#40;</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$results</span> <span class="sy0">=</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">events</span><span class="sy0">-&gt;</span><span class="me1">trigger</span><span class="br0">&#40;</span><span class="kw4">__FUNCTION__</span><span class="sy0">.</span><span class="st_h">'.pre'</span><span class="sy0">,</span> <span class="re0">$this</span><span class="sy0">,</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span> 
            <span class="kw2">function</span> <span class="br0">&#40;</span><span class="re0">$result</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="kw1">return</span> <span class="kw3">is_array</span><span class="br0">&#40;</span><span class="re0">$result</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="br0">&#125;</span>
        <span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$results</span><span class="sy0">-&gt;</span><span class="me1">stopped</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> 
            <span class="kw1">return</span> <span class="re0">$results</span><span class="sy0">-&gt;</span><span class="me1">last</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> 
        <span class="br0">&#125;</span>
&nbsp;
        <span class="re0">$books</span> <span class="sy0">=</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">mapper</span><span class="sy0">-&gt;</span><span class="me1">fetchAll</span><span class="br0">&#40;</span><span class="st_h">'title'</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">getEventManager</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">trigger</span><span class="br0">&#40;</span><span class="kw4">__FUNCTION__</span><span class="sy0">.</span><span class="st_h">'.post'</span><span class="sy0">,</span> <span class="re0">$this</span><span class="sy0">,</span> 
            <span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">'books'</span> <span class="sy0">=&gt;</span> <span class="re0">$books</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="kw1">return</span> <span class="re0">$books</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> loadById<span class="br0">&#40;</span><span class="re0">$id</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$results</span> <span class="sy0">=</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">events</span><span class="sy0">-&gt;</span><span class="me1">trigger</span><span class="br0">&#40;</span><span class="kw4">__FUNCTION__</span><span class="sy0">.</span><span class="st_h">'.pre'</span><span class="sy0">,</span> <span class="re0">$this</span><span class="sy0">,</span> 
            <span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">'id'</span> <span class="sy0">=&gt;</span> <span class="re0">$id</span><span class="br0">&#41;</span><span class="sy0">,</span> 
            <span class="kw2">function</span> <span class="br0">&#40;</span><span class="re0">$result</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="kw1">return</span> <span class="br0">&#40;</span><span class="re0">$result</span> instanceof Book\Entity<span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="br0">&#125;</span>
        <span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$results</span><span class="sy0">-&gt;</span><span class="me1">stopped</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> 
            <span class="kw1">return</span> <span class="re0">$results</span><span class="sy0">-&gt;</span><span class="me1">last</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> 
        <span class="br0">&#125;</span>
&nbsp;
        <span class="re0">$book</span> <span class="sy0">=</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">mapper</span><span class="sy0">-&gt;</span><span class="me1">loadById</span><span class="br0">&#40;</span><span class="re0">$id</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">getEventManager</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">trigger</span><span class="br0">&#40;</span><span class="kw4">__FUNCTION__</span><span class="sy0">.</span><span class="st_h">'.post'</span><span class="sy0">,</span> <span class="re0">$this</span><span class="sy0">,</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">'book'</span> <span class="sy0">=&gt;</span> <span class="re0">$book</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="kw1">return</span> <span class="re0">$book</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="co1">// etc</span>
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>A simple service object essentially proxies through to the mapper. I generally have more specific methods, such as <tt>fetchAllByTitle()</tt>, but that's a personal preference. In this example, I have an ZF2 event manager in play and the service object triggers events as required.</p>
<p>The service object is also useful when there are multiple related objects. For instance, if <tt>books</tt> had <tt>tags</tt> that were loaded separately, then I would have a method such as <tt>loadTagsIntoBook($book)</tt> on this service object. Of course, others prefer to use an ORM, such as Doctrine for these things. </p>
<h3>Summary</h3>
<p>This overview shows the type of methods that I have in each type of core object in my model layer. My controllers and view helpers only ever deal with service objects and entities, so I can change my mapper at any time. </p>
<p>You also need to think carefully where the business logic lives. I'm a fan of putting the logic in the entities as well as in service objects. Others tend to like their entities to be quite "dumb", though.</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/php/objects-in-the-model-layer-part-2/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fphp%2Fobjects-in-the-model-layer-part-2%2F&amp;language=en_GB&amp;category=text&amp;title=Objects+in+the+model+layer%3A+Part+2&amp;description=I+previously+talked+about+the+terms+I+use+for+objects+in+the+model+layer+and+now+it%27s+time+to+put+some+code+on+those+bones.+Note+that%2Cas+always%2C+all+code...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>Ideas of March: It&#039;s my content and my opinion</title>
		<link>http://akrabat.com/me/ideas-of-march-its-my-content-and-my-opinion/</link>
		<comments>http://akrabat.com/me/ideas-of-march-its-my-content-and-my-opinion/#comments</comments>
		<pubDate>Thu, 28 Mar 2013 08:16:54 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Me]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=2749</guid>
		<description><![CDATA[If there's one thing Twitter is not good at, it's holding a discussion on something controversial. Sean Coates noted this problem when he said: "I’m not worried about having my opinion disagreed with; I *am* concerned that I will be crucified for something I don’t mean." I suspect this is because you don't have the [...]]]></description>
				<content:encoded><![CDATA[<p>If there's one thing Twitter is not good at, it's holding a discussion on something controversial.  Sean Coates <a href="https://twitter.com/coates/status/305132496461373440">noted this problem</a> when he said: "I’m not worried about having my opinion disagreed with; I *am* concerned that I will be crucified for something I don’t mean." I suspect this is because you don't have the space to develop an idea on Twitter. It also doesn't help that Twitter is a conversation which means that people are arguing before you've written your second tweet!</p>
<p>As <a href="http://shiflett.org/blog/2013/mar/ideas-of-march">Chris Shiflett</a> wrote earlier this month, real writing happens on blogs. With a blog you have the space to write down what you mean and develop the idea. However, even here there is risk. <a href="http://www.lornajane.net/posts/2013/ideas-of-march-dont-read-the-comments">Lorna Mitchell</a> noted that comments make it easy to criticise. You can leave an quick "drive-by" insult anonymously for minimal effort. This can become a disincentive to write opinion pieces on blogs too.</p>
<p>Like Chris, I also strongly believe in owning my data. My blog provides information that I want to share and more important, information that I want to find again! I was caught out on my <a href="/computing/automatic-apache-vhosts/">Automatic Apache vhosts</a> post where I linked to an alternative solution which has subsequently been taken down. Information that I want to refer to again <em>must</em> be on my blog as otherwise I can't be sure it'll be there when I need it. </p>
<p>I will continue to share technical things on this site as I believe that my posts are useful to others.  Over time, I intend to blog more opinionated pieces. My views on some topics should be shared and would be a useful reference to point people at. Sometimes, I will turn off comments and encourage others to respond by writing their on their own blog. In more than 140 characters and less anonymously.</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/me/ideas-of-march-its-my-content-and-my-opinion/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fme%2Fideas-of-march-its-my-content-and-my-opinion%2F&amp;language=en_GB&amp;category=text&amp;title=Ideas+of+March%3A+It%27s+my+content+and+my+opinion&amp;description=If+there%27s+one+thing+Twitter+is+not+good+at%2C+it%27s+holding+a+discussion+on+something+controversial.+Sean+Coates+noted+this+problem+when+he+said%3A+%22I%E2%80%99m+not+worried+about+having+my...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>Objects in the model layer</title>
		<link>http://akrabat.com/development/objects-in-the-model-layer/</link>
		<comments>http://akrabat.com/development/objects-in-the-model-layer/#comments</comments>
		<pubDate>Fri, 22 Mar 2013 08:06:35 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=2742</guid>
		<description><![CDATA[I currently use a very simple set of core objects within my model layer: entities, mappers and service objects. Entities are objects that represent something in my business logic. For example, in my traditional Album's tutorial, the entity would be the object that holds one album. It has properties such as title, artist and date [...]]]></description>
				<content:encoded><![CDATA[<p>I currently use a very simple set of core objects within my model layer: entities, mappers and service objects.</p>
<p><em>Entities</em> are objects that represent something in my business logic. For example, in my traditional Album's tutorial, the entity would be the object that holds one album. It has properties such as title, artist and date created and methods that are specific to this entity.</p>
<p><em>Mappers</em> know how to save and load an entity from the data store. This could be a database or a web service or an CSV file on disk. There is no requirement that a given entity maps to a single database table (or file on disk) as the mapper can simply use multiple tables for different properties within the entity if it wants to. The entity has no knowledge of how it is loaded and saved. This isolation means that I can have multiple mappers for the same entity that store it to different data stores.</p>
<p><em>Service objects</em> provide the API that the rest of the application uses. I allow controllers and view helpers to talk to service objects, though I appreciate that others have a different take on MVC. Any given service object knows about mappers and entities and anything else that the business logic requires.  I like having a service object as I can rework which mappers do what without having to touch the rest of the application. The service layer also know about other app details such as sending emails after a form is submitted. In an event based system, such as a ZF2, these details can now live in their own objects which listen for events triggered by the service object. </p>
<p>I dislike the phrase "service object" as the word "service" means so many things to so many people. I haven't heard a better phrase yet that everyone understands though.</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/development/objects-in-the-model-layer/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fdevelopment%2Fobjects-in-the-model-layer%2F&amp;language=en_GB&amp;category=text&amp;title=Objects+in+the+model+layer&amp;description=I+currently+use+a+very+simple+set+of+core+objects+within+my+model+layer%3A+entities%2C+mappers+and+service+objects.+Entities+are+objects+that+represent+something+in+my+business+logic.+For...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>Changing the format of a Zend\Form DateTime element</title>
		<link>http://akrabat.com/zend-framework-2/changing-the-format-of-a-zendform-datetime-element/</link>
		<comments>http://akrabat.com/zend-framework-2/changing-the-format-of-a-zendform-datetime-element/#comments</comments>
		<pubDate>Mon, 04 Mar 2013 08:55:10 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Zend Framework 2]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=2731</guid>
		<description><![CDATA[If you want to change the format of the value of a DateTime element, the easiest way to do this in your Form class is to do this: $this-&#62;add&#40;array&#40; 'name' =&#62; 'next_appointment', 'type' =&#62; 'Zend\Form\Element\DateTime', 'options' =&#62; array&#40; 'label' =&#62; 'Next callback time', &#41;, 'attributes' =&#62; array&#40; 'min' =&#62; '1 Jan 2013, 00:00', &#41;, &#41;&#41;; [...]]]></description>
				<content:encoded><![CDATA[<p>If you want to change the format of the value of a <tt>DateTime</tt> element, the easiest way to do this in your <tt>Form</tt> class is to do this:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">add</span><span class="br0">&#40;</span><span class="kw3">array</span><span class="br0">&#40;</span>
            <span class="st_h">'name'</span> <span class="sy0">=&gt;</span> <span class="st_h">'next_appointment'</span><span class="sy0">,</span>
            <span class="st_h">'type'</span> <span class="sy0">=&gt;</span> <span class="st_h">'Zend\Form\Element\DateTime'</span><span class="sy0">,</span>
            <span class="st_h">'options'</span> <span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span>
                <span class="st_h">'label'</span> <span class="sy0">=&gt;</span> <span class="st_h">'Next callback time'</span><span class="sy0">,</span>
            <span class="br0">&#41;</span><span class="sy0">,</span>
            <span class="st_h">'attributes'</span> <span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span>
                <span class="st_h">'min'</span> <span class="sy0">=&gt;</span> <span class="st_h">'1 Jan 2013, 00:00'</span><span class="sy0">,</span>
            <span class="br0">&#41;</span><span class="sy0">,</span>
        <span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">get</span><span class="br0">&#40;</span><span class="st_h">'next_appointment'</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">setFormat</span><span class="br0">&#40;</span><span class="st_h">'j M Y, H:i'</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>The two things to note:</p>
<ol>
<li>You can't set the format within the array - it has to be via a <tt>setFormat()</tt> call.</li>
<li>If you change the format, you must set the <tt>min</tt> attribute in the same format, as otherwise it will try to set it with the hardcoded string of '1970-01-01T00:00Z' which will not work with your specified format.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/zend-framework-2/changing-the-format-of-a-zendform-datetime-element/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=akrabat&amp;popout=1&amp;url=http%3A%2F%2Fakrabat.com%2Fzend-framework-2%2Fchanging-the-format-of-a-zendform-datetime-element%2F&amp;language=en_GB&amp;category=text&amp;title=Changing+the+format+of+a+Zend%5CForm+DateTime+element&amp;description=If+you+want+to+change+the+format+of+the+value+of+a+DateTime+element%2C+the+easiest+way+to+do+this+in+your+Form+class+is+to+do+this%3A+%24this-%26gt%3Badd%26%2340%3Barray%26%2340%3B+%27name%27...&amp;tags=blog" type="text/html" />
	</item>
	</channel>
</rss>

<!-- Dynamic page generated in 1.887 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2013-05-24 23:15:12 -->
