<?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>Jon Lebensold &#187; Zend</title>
	<atom:link href="http://lebensold.net/category/zend/feed" rel="self" type="application/rss+xml" />
	<link>http://lebensold.net</link>
	<description>thoughts on web development, technology and media</description>
	<lastBuildDate>Tue, 22 Jun 2010 16:04:05 +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>Setting Unicode in Zend_Db and Doctrine with MySQL</title>
		<link>http://lebensold.net/zend/setting-unicode-in-zend_db-and-doctrine-with-mysql</link>
		<comments>http://lebensold.net/zend/setting-unicode-in-zend_db-and-doctrine-with-mysql#comments</comments>
		<pubDate>Tue, 26 Aug 2008 12:46:38 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Doctrine]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[character encoding]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[utf8]]></category>
		<category><![CDATA[zend_db]]></category>

		<guid isPermaLink="false">http://jon.lebensold.ca/?p=184</guid>
		<description><![CDATA[When your using a persistence layer like Zend_Db or Doctrine, setting the character set is crucial in order for everything to turn out as expected in your database. I've spent too much time trying to sort character encoding issues in the past, so I'm putting it here as a reference.
In Zend_Db, after configuring your database, [...]]]></description>
			<content:encoded><![CDATA[<p>When your using a persistence layer like Zend_Db or Doctrine, setting the character set is crucial in order for everything to turn out as expected in your database. I've spent too much time trying to sort character encoding issues in the past, so I'm putting it here as a reference.</p>
<p>In Zend_Db, after configuring your database, in order for arabic / persian / non-ascii characters to persist properly, you need to configure the character set:</p>
<pre class="php">&nbsp;
<span style="color: #808080; font-style: italic;">// setup database</span>
<span style="color: #0000ff;">$db</span> = Zend_Db::<span style="color: #006600;">factory</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$config</span>-&gt;<span style="color: #006600;">db</span>-&gt;<span style="color: #006600;">adapter</span>, <span style="color: #0000ff;">$config_values</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// for Unicode support</span>
<span style="color: #0000ff;">$db</span>-&gt;<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;SET NAMES 'utf8'&quot;</span><span style="color: #66cc66;">&#41;</span>;
Zend_Db_Table::<span style="color: #006600;">setDefaultAdapter</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$db</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>In Doctrine, a similar declaration is needed to setup the right character set. These lines would be in the config.php file included in the sample project, or whenever you initialize your Doctrine_Manager:</p>
<pre class="php">&nbsp;
<span style="color: #0000ff;">$connection</span> = Doctrine_Manager::<span style="color: #006600;">connection</span><span style="color: #66cc66;">&#40;</span>DB_PATH<span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$connection</span>-&gt;<span style="color: #006600;">setCharset</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'UTF8'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
]]></content:encoded>
			<wfw:commentRss>http://lebensold.net/zend/setting-unicode-in-zend_db-and-doctrine-with-mysql/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Dojo and Zend: Where&#8217;s the loose coupling gone?</title>
		<link>http://lebensold.net/zend/dojo-and-zend-wheres-the-loose-coupling-gone</link>
		<comments>http://lebensold.net/zend/dojo-and-zend-wheres-the-loose-coupling-gone#comments</comments>
		<pubDate>Wed, 13 Aug 2008 20:28:50 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Commentary]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[dojo]]></category>

		<guid isPermaLink="false">http://jon.lebensold.ca/?p=151</guid>
		<description><![CDATA[On the eve of the release of the Zend Framework's 1.6RC, found it interesting to read Lukas' comment on Matthew Weier O'Phinney's blog about how there were outstanding issues in existing core components of the Zend Framework that hadn't been resolved, and yet the Zend_* component library was still growing and accepting new proposals. 
The [...]]]></description>
			<content:encoded><![CDATA[<p>On the eve of the release of the Zend Framework's 1.6RC, found it interesting to read <a href='http://pooteeweet.org/'>Lukas</a>' comment on <a href='http://weierophinney.net/matthew/'>Matthew Weier O'Phinney's blog</a> about how there were outstanding issues in existing core components of the Zend Framework that hadn't been resolved, and yet the Zend_* component library was still growing and accepting new proposals. </p>
<p>The last comment by Martin noted that Zend_Db still had some unresolved issues for over a year. </p>
<p>Matthew's response was that this was a community project and that priorities are dictated as such. Personally, this doesn't affect me because I'm hedging my bets on Doctrine as an ORM layer for PHP development. </p>
<p>I like Matthew's response because the Zend Framework's philosophy encourages the development of loosely coupled components wrapped up in a development philosophy that encourages open contributions, financial backing (thanks to Zend, IBM, Sun and others), Unit Testing and project transparency. Here's where things go a little wonky.</p>
<h1>Partnership with the Dojo community</h1>
<p>While I really enjoy the implementation of Zend_Form on the server side. It allows multiple configurations, supports validation and while the decorators are a bit obtuse, on a whole, the Zend_Form libraries thin out controllers and encapsulate the concern of data entry in a clean manner.</p>
<p>In this point release, however it seems like there's been a whole outgrowth of Zend_Dojo_* classes that cater to doing AJAX development with the Dojo stack. I'm not sold on Dojo as a client-side framework. Past experience with it has left me trying to sort out a large, over-specialized collection of patterns that almost do what I want but not quite. I also don't like the coupling that occurs between the data set, the client-side validation and the magic rendering of panels (like Accordian's and content panes). </p>
<h1>An alternative architectural approach</h1>
<p>I would rather write the events, the theme, the data and the validation separately and reduce duplication in each of those patterns rather than in the UI pattern as a whole. In short, I think they've abstracted the wrong part. The same example can be seen in ASP.NET form validators from two years ago.</p>
<p>With Prototype, I can rapidly mock out a site with the functional requirements and use Script.aculo.us to accent the presentation with animation. My functional specification isn't muddied with fancy visuals and it lets me focus on the event handling, validation and end states rather than the fancy effects.</p>
<p>By using Zend_Dojo, you won't be tempted to build a well-exposed Service Layer from Zend_Controller. The advantage here is that you lead development towards service-orientation. If you need to write a Flex front-end, or a whole new business-tier, you at least have the beginnings of a web service, whereas if your working with Zend_Dojo's declarative templating syntax, you're already coupling the events, validation and data into heavily abstracted UI patterns.</p>
<p>This approach doesn't lead development towards the production of a refined Service Layer, but to a specialized Dojo-friendly interface with markup-generation from Zend_Form classes into Dojo markup. I would rather see a Zend_Form Service API where developers can send / receive JSON messages to inherited Zend_Form and then have the Dojo community write an Adapter that would send / receive messages from Dojo widgets.</p>
<p>A year or two from now, I wouldn't be surprised if people will look at the same Zend bug tracker, asking where support for Dojo went after the Zend team jumps to the next cool Javascript framework on the block and we find ourselves with Zend_Form_SproutCore or some other dependency.</p>
]]></content:encoded>
			<wfw:commentRss>http://lebensold.net/zend/dojo-and-zend-wheres-the-loose-coupling-gone/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Doctrine, MySQL and Zend_Date</title>
		<link>http://lebensold.net/development/doctrine-mysql-and-zend_date</link>
		<comments>http://lebensold.net/development/doctrine-mysql-and-zend_date#comments</comments>
		<pubDate>Mon, 11 Aug 2008 02:54:30 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Doctrine]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[zend_date]]></category>

		<guid isPermaLink="false">http://jon.lebensold.ca/?p=143</guid>
		<description><![CDATA[Something that I've found really helpful in C# is the DateTime object and how it can be natively mapped to NHibernate classes and then straight to the database.
The equivalent Doctrine code is almost as elegant. Here's an excellent example of when objects abstract you from the nitty-gritty string parsing code that would be required to [...]]]></description>
			<content:encoded><![CDATA[<p>Something that I've found really helpful in C# is the DateTime object and how it can be natively mapped to NHibernate classes and then straight to the database.</p>
<p>The equivalent Doctrine code is almost as elegant. Here's an excellent example of when objects abstract you from the nitty-gritty string parsing code that would be required to do time-stamping in a database:</p>
<pre class="php">&nbsp;
<span style="color: #0000ff;">$today</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Date<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$newPost</span> = <span style="color: #000000; font-weight: bold;">new</span> Post<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$newPost</span>-&gt;<span style="color: #006600;">title</span> = <span style="color: #ff0000;">'posting for '</span> . <span style="color: #0000ff;">$today</span>-&gt;<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>Zend_Date::<span style="color: #006600;">TIME_MEDIUM</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$newPost</span>-&gt;<span style="color: #006600;">createdAt</span> = <span style="color: #0000ff;">$today</span>-&gt;<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>Zend_Date::<span style="color: #006600;">W3C</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$newPost</span>-&gt;<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Zend_Date::W3C returns a W3C compliant timestamp, which the Doctrine Model then passes to the underlying persistence layer.</p>
<p>Assuming that you had a UnitOfWork class handling your persistence, you could add createdAt to all of the YAML models, and have every record stamped with updatedAt and createdAt without changing any higher level code. </p>
<h1>Why Bother With UpdatedAt and CreatedAt?</h1>
<p>having these two columns in a table can save tremendous amounts of time sorting out concurrency issues. They also allow you to do some very primitive reporting on your data at no cost to adding complexity to the rest of your application. </p>
<p>My Doctrine Model was configured using a YAML file:</p>
<pre>&nbsp;
Post:
  tableName: post
  columns:
    id:
      primary: true
      autoincrement: true
      type: integer(4)
    createdAt: date
    title: string(255)
&nbsp;</pre>
<p>Then it's just a matter of running './doctrine rebuild-db' from the command line from the Doctrine directory and it create's the PHP classes and prepares the database columns. </p>
<p>Using Zend_Date, you can easily do date comparisons, generate different date formats, do complex date querying and so on without having to get into the string / number manipulations. </p>
]]></content:encoded>
			<wfw:commentRss>http://lebensold.net/development/doctrine-mysql-and-zend_date/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>A Repository Pattern in PHP</title>
		<link>http://lebensold.net/zend/repository-pattern-in-php-not-quite-yet</link>
		<comments>http://lebensold.net/zend/repository-pattern-in-php-not-quite-yet#comments</comments>
		<pubDate>Sun, 03 Aug 2008 05:49:01 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[Doctrine]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[persistence]]></category>
		<category><![CDATA[poeaa]]></category>
		<category><![CDATA[repository]]></category>

		<guid isPermaLink="false">http://jon.lebensold.ca/?p=97</guid>
		<description><![CDATA[Adam Jenson on the Zend_MVC list was talking about the difficulties in MVC of making a thin controller. The discussion is an interesting example of the ineffectiveness MVC as the only pattern holding a web application together. Since I've already complained about MVC in the past, I think Adam touched on something that I struggle [...]]]></description>
			<content:encoded><![CDATA[<p>Adam Jenson on the Zend_MVC list was talking about the difficulties in MVC of <a href='http://www.nabble.com/Best-practices-in-a-thin-controller-application-tt18538446.html'>making a thin controller</a>. The discussion is an interesting example of the ineffectiveness MVC as the only pattern holding a web application together. Since I've already <a href='http://jon.lebensold.ca/development/mvp-or-mvc-or-neither'>complained about MVC in the past</a>, I think Adam touched on something that I struggle with as well, which is a thin Controller.</p>
<p>My solution stems from a layer between what the 37Signals would refer to as "the Model" and the Controller, namely a Repository which acts as a broker between a collection of Model objects and the Controller. By using a Repository and Zend_Form, the action of a Controller in an Application can go from being several hundred lines to a couple dozen. </p>
<p>In my last project, my fattest controller looked like this (NOTE: I've included the init() method for completeness):</p>
<pre class="php">&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentModelId</span> = <a href="http://www.php.net/intval"><span style="color: #000066;">intval</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;_request-&gt;<span style="color: #006600;">getParam</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;modelId&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> postsAction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>PostRepository::<span style="color: #006600;">Exists</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentModelId</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//edit</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;_request-&gt;<span style="color: #006600;">getParam</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;state&quot;</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #ff0000;">&quot;edit&quot;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">form</span> = <span style="color: #000000; font-weight: bold;">new</span> forms_Post<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;edit&quot;</span>, PostRepository::<span style="color: #006600;">Find</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentModelId</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;						
&nbsp;
		<span style="color: #808080; font-style: italic;">//delete</span>
			<span style="color: #b1b100;">elseif</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;_request-&gt;<span style="color: #006600;">getParam</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;state&quot;</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #ff0000;">&quot;delete&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				PostRepository::<span style="color: #006600;">Delete</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentModelId</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #0000ff;">$this</span>-&gt;_forward<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'viewposts'</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #b1b100;">else</span>
		<span style="color: #66cc66;">&#123;</span>
		    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>! <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getRequest</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">isPost</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
			    <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">form</span> = <span style="color: #000000; font-weight: bold;">new</span> forms_Post<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;create&quot;</span>, PostRepository::<span style="color: #006600;">NewPost</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">else</span>
			    <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">form</span> = <span style="color: #000000; font-weight: bold;">new</span> forms_Post<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;create&quot;</span>, PostRepository::<span style="color: #006600;">Find</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;_request-&gt;<span style="color: #006600;">getParam</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;id&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>		
&nbsp;
		<span style="color: #808080; font-style: italic;">//postback</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getRequest</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">isPost</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">PostBack</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                       <span style="color: #0000ff;">$this</span>-&gt;_helper-&gt;<span style="color: #006600;">Redirector</span>-&gt;<span style="color: #006600;">gotoUrl</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/admin/viewposts'</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> PostBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0000ff;">$post</span> = <a href="http://www.php.net/array_merge"><span style="color: #000066;">array_merge</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_POST</span>, <span style="color: #0000ff;">$_FILES</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">form</span>-&gt;<span style="color: #006600;">isValid</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$post</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">form</span>-&gt;<span style="color: #006600;">persistData</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #b1b100;">else</span>
			<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">errorElements</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">form</span>-&gt;<span style="color: #006600;">getMessages</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Because all my forms are attached in the same way and they use the same pattern, I can use the same PostBack() method on different forms.</p>
<p>To be honest, I don't like making calls to a repository statically, and in retrospect, I wouldn't recommend doing this again. This lead me to searching for a better Repository implementation in PHP.<br />
As I was hunting through the the mighty Google, I found an article by <a href='http://www.travisswicegood.com/index.php/2007/11/20/repository_pattern_in_php' rel="nofollow">Travis Swice</a> in late 2007. While he's concerned with the 'getting' part of the pattern, I'm more interested in the 'setting' (persistence) part of the pattern. In Travis' example, he simple creates a 'new Repository();'. I have a problem with this, since its often times an over-abstraction of an early Domain.</p>
<p>In a simple application, or even during development, you don't want to take the complexities of a querying language out of SQL, into an object-relational mapping tool AND THEN into a Repository pattern! You're just bubbling up the problem. A Repository should simplify querying and persisting data by providing a relatively flat layer for your UI to make simple calls to. If you create an abstract base class called Repository, you can then collect these flat method calls into logical groupings. </p>
<p>In the example above, the PostRepository would inherit from a Repository class like the one below (along with all other Repositories) and it would handle the creation of Post objects as well as their object-relationship mappings to other models. While at first, "new Post();" seems like an equivalent call to "$postRepository->NewPost();", the latter can input default bits of content based on who is creating the Posting. While I know that I'm stretching the definition of a Repository, I do so because often times the pattern need not be completely implemented before its usefulness becomes apparent. As the project grows, you already have then necessary naming conventions in your codebase to implement a full blown domain. Think of it as picking a hiking trail and enjoying the mid-way summit instead of being stuck making your own road with a machete.</p>
<p>I'm still working through a full architecture, however I think with Doctrine, a bit of Reflection and the abstract base class below, I can come up with a half-way Repository pattern with a Unit of Work that will simplify persisting collections of objects with proper transactionality.</p>
<pre class="php">&nbsp;
abstract <span style="color: #000000; font-weight: bold;">class</span> Repository <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Unit of Work for each Repository Instantiated
	 *
	 * @var UnitOfWork
	 */</span>
	protected <span style="color: #0000ff;">$unitOfWork</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">unitOfWork</span> = <span style="color: #000000; font-weight: bold;">new</span> UnitOfWork<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">//Doctrine Session initialization code here...</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Add a Create object to the unit of work
	 *
	 * @param Doctrine_Record $model
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Create<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$model</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">unitOfWork</span>-&gt;<span style="color: #006600;">RegisterModelForCreate</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$model</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Add a Update object to the unit of work
	 *
	 * @param Doctrine_Record $model
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Update<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$model</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">unitOfWork</span>-&gt;<span style="color: #006600;">RegisterModelForUpdate</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$model</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #808080; font-style: italic;">/**
	 * Add a Delete object to the unit of work
	 *
	 * @param Doctrine_Record $model
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Delete<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$model</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">unitOfWork</span>-&gt;<span style="color: #006600;">RegisterModelForDelete</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$model</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Commit<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">unitOfWork</span>-&gt;<span style="color: #006600;">CommitAll</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> clearUnitOfWork<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">unitOfWork</span>-&gt;<span style="color: #006600;">ClearAll</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
]]></content:encoded>
			<wfw:commentRss>http://lebensold.net/zend/repository-pattern-in-php-not-quite-yet/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Zend with Flickr: random photos from users or groups</title>
		<link>http://lebensold.net/development/zend-with-flickr-random-photos-from-users-or-groups</link>
		<comments>http://lebensold.net/development/zend-with-flickr-random-photos-from-users-or-groups#comments</comments>
		<pubDate>Mon, 07 Jul 2008 23:15:25 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[free code]]></category>

		<guid isPermaLink="false">http://jon.lebensold.ca/?p=94</guid>
		<description><![CDATA[I'm currently working on a project that involves pulling photos randomly. While my initial programmer instinct tells me I should write something that would enable my client to upload photos locally. However, there are some missed social networking opportunities when you decide to host your own images.
By putting the photos on Flickr, my client can [...]]]></description>
			<content:encoded><![CDATA[<p>I'm currently working on a project that involves pulling photos randomly. While my initial programmer instinct tells me I should write something that would enable my client to upload photos locally. However, there are some missed social networking opportunities when you decide to host your own images.</p>
<p>By putting the photos on Flickr, my client can keep posting photos and build a pool of images that other people can contribute to. From a development perspective, all I needed to do was leverage the Zend_Service_Flickr classes and write a simple wrapper.</p>
<p><iframe src="http://jon.lebensold.ca/demo/ZendFlickrRandomPhotos/" width="600" height="420"></iframe></p>
<p>The frontend page looks like this:</p>
<pre class="php">&nbsp;
<span style="color: #0000ff;">$randomPhotos</span> = <span style="color: #000000; font-weight: bold;">new</span> RandomPhotosFromFlickr<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'YOUR_API_KEY'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$numberOfPhotos</span> = <span style="color: #cc66cc;">3</span>;
&nbsp;
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;
&lt;h2&gt;Group Photos&lt;/h2&gt;
&nbsp;
&quot;</span>;
<span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$randomPhotos</span>-&gt;<span style="color: #006600;">FromGroup</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span>, <span style="color: #ff0000;">&quot;GROUP@ID&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$p</span><span style="color: #66cc66;">&#41;</span>
	<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;&lt;img src='&quot;</span>.<span style="color: #0000ff;">$p</span>-&gt;<span style="color: #006600;">Small</span>-&gt;<span style="color: #006600;">uri</span>.<span style="color: #ff0000;">&quot;' /&gt;&quot;</span>;
&nbsp;
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;
&lt;h2&gt;User's Photos&lt;/h2&gt;
&nbsp;
&quot;</span>;
<span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$randomPhotos</span>-&gt;<span style="color: #006600;">FromUser</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span>, <span style="color: #ff0000;">&quot;user@yahoo.com&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$p</span><span style="color: #66cc66;">&#41;</span>
	<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;&lt;img src='&quot;</span>.<span style="color: #0000ff;">$p</span>-&gt;<span style="color: #006600;">Small</span>-&gt;<span style="color: #006600;">uri</span>.<span style="color: #ff0000;">&quot;' /&gt;&quot;</span>;
&nbsp;</pre>
<p>In this example, $p is a <a href="http://framework.zend.com/manual/en/zend.service.flickr.html#zend.service.flickr.classes.image">Zend_Service_Flickr object</a>, so you can pull any kind of data while letting the simple class I wrote handle the selection of random photos:</p>
<pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> RandomPhotosFromFlickr
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">/**
	 * API key for Flickr
	 *
	 * @var string
	 */</span>
	protected <span style="color: #0000ff;">$apiKey</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * photoset used for retrieving random photos
	 *
	 * @var Zend_Service_Flickr_ResultSet
	 */</span>
	protected <span style="color: #0000ff;">$currentPhotoset</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * number of photos in the current photoset
	 *
	 * @var integer
	 */</span>
	protected <span style="color: #0000ff;">$totalPhotoCount</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Class constructor
	 * We need your Flickr API Key
	 * Go to http://www.flickr.com/services/api/keys/apply/ to get one
	 * @param string $apiKey
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$apiKey</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">apiKey</span> = <span style="color: #0000ff;">$apiKey</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Supply a username (a user's email address) and the number of photos you'd like returned
	 *
	 * @param integer $numberOfPhotos
	 * @param string $username
	 * @return Zend_Service_Flickr_ResultSet
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> FromUser<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span>, <span style="color: #0000ff;">$username</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$flickr</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Service_Flickr<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">apiKey</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span> = <span style="color: #0000ff;">$flickr</span>-&gt;<span style="color: #006600;">userSearch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$username</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">totalPhotoCount</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span>-&gt;<span style="color: #006600;">totalResultsAvailable</span>;
&nbsp;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span> = <span style="color: #0000ff;">$flickr</span>-&gt;<span style="color: #006600;">userSearch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$username</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'per_page'</span> =&gt; <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">totalPhotoCount</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getPhotosFromCurrentPhotoset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Supply a group ID and the number of photos you'd like returned
	 *
	 * @param integer $numberOfPhotos
	 * @param string $groupId
	 * @return Zend_Service_Flickr_ResultSet
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> FromGroup<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span>, <span style="color: #0000ff;">$groupId</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0000ff;">$flickr</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Service_Flickr<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">apiKey</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span> = <span style="color: #0000ff;">$flickr</span>-&gt;<span style="color: #006600;">groupPoolGetPhotos</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$groupId</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">totalPhotoCount</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span>-&gt;<span style="color: #006600;">totalResultsAvailable</span>;
&nbsp;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span> = <span style="color: #0000ff;">$flickr</span>-&gt;<span style="color: #006600;">groupPoolGetPhotos</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$groupId</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'per_page'</span> =&gt; <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">totalPhotoCount</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getPhotosFromCurrentPhotoset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Return an integer within the photo index range
	 *
	 * @return integer
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getRandomPhotoIndex<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> <a href="http://www.php.net/rand"><span style="color: #000066;">rand</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span> , <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">totalPhotoCount</span> - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Get a photo object from the Flickr Web Service
	 * Provide an indice from the current photo set
	 *
	 * @param integer $index
	 * @return Zend_Service_Flickr_Image
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getPhoto<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$index</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span>-&gt;<span style="color: #006600;">seek</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$index</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span>-&gt;<span style="color: #006600;">current</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * using $this-&gt;currentPhotoset, grab a collection of random photos
	 *
	 * @param integer $numberOfPhotos
	 * @return Zend_Service_Flickr_ResultSet
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getPhotosFromCurrentPhotoset<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// first collect up a list of IDs (making sure there are no duplicates</span>
		<span style="color: #0000ff;">$photoIds</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$photoIds</span><span style="color: #66cc66;">&#41;</span> &lt; <span style="color: #0000ff;">$numberOfPhotos</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0000ff;">$id</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getRandomPhotoIndex</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>! <a href="http://www.php.net/in_array"><span style="color: #000066;">in_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span>, <span style="color: #0000ff;">$photoIds</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #0000ff;">$photoIds</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$id</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0000ff;">$randomPhotos</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// query the web service to grab the photo objects</span>
		<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$photoIds</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$id</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #0000ff;">$randomPhotos</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getPhoto</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$randomPhotos</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>you can <a href="http://jon.lebensold.ca/demo/ZendFlickrRandomPhotos/RandomPhotosFromFlickr.zip">download the full source</a> if you'd like to try it yourself. Please note that I've omitted bundling my code with a copy of the Zend Framework.</p>
<p>Using Zend Framework's Zend_Service_Flickr </p>
]]></content:encoded>
			<wfw:commentRss>http://lebensold.net/development/zend-with-flickr-random-photos-from-users-or-groups/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Hibernate for PHP: using Doctrine as part of a Unit-Test workflow</title>
		<link>http://lebensold.net/development/hibernate-for-php-using-doctrine-as-part-of-a-unit-test-workflow</link>
		<comments>http://lebensold.net/development/hibernate-for-php-using-doctrine-as-part-of-a-unit-test-workflow#comments</comments>
		<pubDate>Fri, 06 Jun 2008 16:52:37 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Doctrine]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend]]></category>

		<guid isPermaLink="false">http://jon.lebensold.ca/development/hibernate-for-php-using-doctrine-as-part-of-a-unit-test-workflow</guid>
		<description><![CDATA[On the Zend general mailing list, Josh Team asked some interesting questions about form validation and best practices. Lately, I've been looking for an alternative to NHibernate for my PHP development. Thinking that I would have to use Hibernate and run PHP inside a Java bucket, I figured the barrier to entry would be too [...]]]></description>
			<content:encoded><![CDATA[<p>On the Zend general mailing list, Josh Team asked some interesting questions about form validation and best practices. Lately, I've been looking for an alternative to NHibernate for my PHP development. Thinking that I would have to use Hibernate and run PHP inside a Java bucket, I figured the barrier to entry would be too great and would require a lot of research. Then someone mentioned to Josh Team that he should use <a href="http://www.phpdoctrine.org">Doctrine</a>, an object-relational mapper in PHP.</p>
<p>The <a href="http://www.phpdoctrine.org/documentation/manual/0_10/chapter/dql-doctrine-query-language">Doctrine Documentation looks pretty solid</a> and <a href="http://ruben.savanne.be/articles/integrating-zend-framework-and-doctrine">Ruben Savanne wrote a great article about integrating Doctrine into a Zend Framework project</a>. I wholeheartedly agree with his statement that Zend_Db is a little too low level for 90% of tasks that application developers are aiming to accomplish. With a simple YAML configuration, Doctrine promises to generate the necessary classes and even your database schema. These two features are a must if you want to be able to model your database on the fly and run unit tests against a clean schema without writing a complex setup harness.</p>
<p>My only concern is the lack of any obvious enterprise support, regardless this library is worth looking at as an alternative to Zend_Db.</p>
]]></content:encoded>
			<wfw:commentRss>http://lebensold.net/development/hibernate-for-php-using-doctrine-as-part-of-a-unit-test-workflow/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Decoupled validation with Zend_Form, Zend_Validate and Zend MVC</title>
		<link>http://lebensold.net/development/decoupled-validation-with-zend_form-zend_validate-and-zend-mvc</link>
		<comments>http://lebensold.net/development/decoupled-validation-with-zend_form-zend_validate-and-zend-mvc#comments</comments>
		<pubDate>Wed, 04 Jun 2008 19:56:32 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Services]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[free code]]></category>

		<guid isPermaLink="false">http://jon.lebensold.ca/development/decoupled-validation-with-zend_form-zend_validate-and-zend-mvc</guid>
		<description><![CDATA[Lately I've been experimenting a lot with trying to serialize a form and send it over the wire as a means of doing asynchronous postback of partially validated forms. 
I've come up with a little prototype that's a PHP version of an ASP.NET implementation I did a couple months ago for a project I was [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I've been experimenting a lot with trying to serialize a form and send it over the wire as a means of doing asynchronous postback of partially validated forms. </p>
<p>I've come up with a little prototype that's a PHP version of an ASP.NET implementation I did a couple months ago for a project I was working on. </p>
<p>The PHP version was surprisingly easier to implement, due to the simplicity of Zend_Form when compared to the bulldozer we-control-everything-and-rewrite-your-user-control-id's approach of ASP.NET forms.</p>
<p>Essentially, I've created a class with a simple form (1 lookup list (one-to-many for the data modelers out there) and a couple of simple textboxes).</p>
<p>While I think that it's crucial to decouple business rules from the actual form implementation, I think that Zend_Validate validators, as well as custom validators, are an excellent way of creating testable, declaritive validation rules that can be applied to form elements as you see fit. The other decoupling you'll find in this example is from the postback model, essentially allowing you to drop the same form into any part of a Zend Framework application and posting to an Ajax Controller service for validation purposes. What's nice about this is that no business rules are left on the client. I've used prototype for a little styling.</p>
<p>The finishing touch in all of this is that I can leverage the server-side validation routine for both the asynchronous and synchronous calls, so that postback and validation run through the same validation code, essentially providing a second interface with to the same form object.</p>
<p>I'm going to look into extending this example to provide not only one-to-one mapping and one-to-many mapping (lookup lists), but also many-to-one mapping (generating sub-forms dynamically and performing partial validation). Not sure exactly how to do that, so comments would be greatly appreciated!</p>
<p>Here's a quick demonstration.<br />
<iframe src="http://jon.lebensold.ca/demo/decoupled-validation/html/" width="550" height="450" border="0"></iframe></p>
<p>and now for the code. The webservice bit is all contained in the IndexController for the sake of brevity. In a full blown application, you would likely have a Controller (or several controllers) that would offer different asynchronous entry points into your MVC framework.</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> IndexController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> indexAction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">f</span> = <span style="color: #000000; font-weight: bold;">new</span> RegistrationForm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">setMode</span> = <span style="color: #ff0000;">&quot;setMode($('wsTab').childNodes[0]);&quot;</span>;
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">f</span>-&gt;<span style="color: #006600;">isValid</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_POST</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">render</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;FormSuccess&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;_request-&gt;<span style="color: #006600;">isPost</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">errorElements</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">f</span>-&gt;<span style="color: #006600;">getMessages</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">setMode</span> = <span style="color: #ff0000;">&quot;setMode($('pbTab').childNodes[0]);&quot;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Ajax JSON call is made through this controller
	 *
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> asyncAction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//prepare a JSON response</span>
		<span style="color: #0000ff;">$this</span>-&gt;_helper-&gt;<span style="color: #006600;">viewRenderer</span>-&gt;<span style="color: #006600;">setNoRender</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0000ff;">$this</span>-&gt;_helper-&gt;<span style="color: #006600;">getHelper</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'layout'</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">disableLayout</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
     	<span style="color: #808080; font-style: italic;">//Map the form from the client-side call</span>
     	<span style="color: #0000ff;">$myFormData</span> = Zend_Json::<span style="color: #006600;">decode</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getRequest</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">getParam</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;fs&quot;</span><span style="color: #66cc66;">&#41;</span> , Zend_Json::<span style="color: #006600;">TYPE_ARRAY</span><span style="color: #66cc66;">&#41;</span>;
     	<span style="color: #0000ff;">$form</span> = <span style="color: #000000; font-weight: bold;">new</span> RegistrationForm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
     	<span style="color: #0000ff;">$form</span>-&gt;<span style="color: #006600;">isValid</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$myFormData</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
     	<span style="color: #808080; font-style: italic;">//return the result</span>
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getResponse</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">setHeader</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Content-Type'</span>, <span style="color: #ff0000;">'application/json'</span><span style="color: #66cc66;">&#41;</span>
							   -&gt;<span style="color: #006600;">setBody</span><span style="color: #66cc66;">&#40;</span>Zend_Json::<span style="color: #006600;">encode</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$form</span>-&gt;<span style="color: #006600;">getMessages</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>You'll notice that I've pulled out the form into its own class (RegistrationForm): </p>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> RegistrationForm <span style="color: #000000; font-weight: bold;">extends</span> Zend_Form
<span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
    	<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">setAttrib</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;id&quot;</span> , <span style="color: #ff0000;">&quot;frm&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    	<span style="color: #0000ff;">$onetomany</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Select<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'sad'</span><span style="color: #66cc66;">&#41;</span>;
    	<span style="color: #0000ff;">$onetomany</span>-&gt;<span style="color: #006600;">setLabel</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'One to many:'</span><span style="color: #66cc66;">&#41;</span>
    			  -&gt;<span style="color: #006600;">setMultiOptions</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getOneToMany</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    			  -&gt;<span style="color: #006600;">addValidator</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Onetomany<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #0000ff;">$usernameRequiredValidator</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Validate_NotEmpty<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$usernameRequiredValidator</span>-&gt;<span style="color: #006600;">setMessage</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Please enter a username.&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #0000ff;">$usernameStringLengthValidator</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Validate_StringLength<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span>,<span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$usernameStringLengthValidator</span>-&gt;<span style="color: #006600;">setMessage</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Username is too short! (8 character minimum)&quot;</span>, Zend_Validate_StringLength::<span style="color: #006600;">TOO_SHORT</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$usernameStringLengthValidator</span>-&gt;<span style="color: #006600;">setMessage</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Username is too long! (less than 20 characters)&quot;</span>, Zend_Validate_StringLength::<span style="color: #006600;">TOO_LONG</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    	<span style="color: #0000ff;">$username</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Text<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'username'</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$username</span>-&gt;<span style="color: #006600;">setLabel</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Username:'</span><span style="color: #66cc66;">&#41;</span>
        		 -&gt;<span style="color: #006600;">addValidator</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$usernameRequiredValidator</span><span style="color: #66cc66;">&#41;</span>
        		 -&gt;<span style="color: #006600;">addValidator</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$usernameStringLengthValidator</span><span style="color: #66cc66;">&#41;</span>
        		 -&gt;<span style="color: #006600;">setRequired</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    	<span style="color: #0000ff;">$name</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Text<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$name</span> -&gt;<span style="color: #006600;">setLabel</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Name:'</span><span style="color: #66cc66;">&#41;</span>
        		 -&gt;<span style="color: #006600;">addValidator</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'StringLength'</span>, <span style="color: #000000; font-weight: bold;">true</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        		 -&gt;<span style="color: #006600;">setRequired</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;    	
&nbsp;
        <span style="color: #0000ff;">$password</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Password<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'password'</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$password</span>-&gt;<span style="color: #006600;">setLabel</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'password:'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">addElements</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        	<span style="color: #0000ff;">$onetomany</span>,
            <span style="color: #0000ff;">$username</span>,
            <span style="color: #0000ff;">$name</span>,
            <span style="color: #0000ff;">$password</span>
&nbsp;
        <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">setDecorators</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        	<span style="color: #ff0000;">'FormElements'</span>,
            <span style="color: #ff0000;">'Fieldset'</span>,
            <span style="color: #ff0000;">'Form'</span>
&nbsp;
        <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getElements</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$element</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
        	<span style="color: #0000ff;">$element</span>-&gt;<span style="color: #006600;">setDecorators</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
                     <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'ViewHelper'</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'class'</span> =&gt; <span style="color: #ff0000;">'formText'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
                     <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Label'</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'class'</span> =&gt; <span style="color: #ff0000;">'label'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
            		 <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'HtmlTag'</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'tag'</span> =&gt; <span style="color: #ff0000;">'dd'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #0000ff;">$element</span>-&gt;<span style="color: #006600;">class</span> = <span style="color: #ff0000;">'formText'</span>;
&nbsp;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getOneToMany<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$ar</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;please select&quot;</span>;
		<span style="color: #0000ff;">$ar</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;car&quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;car&quot;</span>;
		<span style="color: #0000ff;">$ar</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;rollerBlade&quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;roller blades&quot;</span>;
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$ar</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>In my post about enterprise validation, I described a scenario where you would create custom validators that could be applied declaratively in C# using the Enterprise Library. The same thinking was applied here with the One To Many by creating a custom validator. This allows you to test the validation (and your business rules) without thinking about the form, the UI or even session:</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> Onetomany <span style="color: #000000; font-weight: bold;">extends</span> Zend_Validate_Abstract
<span style="color: #66cc66;">&#123;</span>
    const NOT_VALID  = <span style="color: #ff0000;">'stringLengthTooLong'</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * @var array
     */</span>
    protected <span style="color: #0000ff;">$_messageTemplates</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        self::<span style="color: #006600;">NOT_VALID</span> =&gt; <span style="color: #ff0000;">&quot;one to many was not valid choice&quot;</span>,
&nbsp;
    <span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * @var array
     */</span>
    protected <span style="color: #0000ff;">$_messageVariables</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        <span style="color: #ff0000;">'not_valid'</span> =&gt; <span style="color: #ff0000;">'_not_valid'</span>,
&nbsp;
    <span style="color: #66cc66;">&#41;</span>;
&nbsp;
    protected <span style="color: #0000ff;">$_not_valid</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * Returns the min option
     *
     * @return integer
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getNotValid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;_not_valid;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * Defined by Zend_Validate_Interface
     * @param  string $value
     * @return boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> isValid<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$value</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// more business rules would go somewhere here...</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$value</span> == <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #0000ff;">$this</span>-&gt;_error<span style="color: #66cc66;">&#40;</span>self::<span style="color: #006600;">NOT_VALID</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;_messages<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
        <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>The last bit of magic is the Javascript. My weapon of choice (as mentioned above) is prototype, however I'm sure you could do the same thing with any other javascript framework:</p>
<pre class="javascript">Event.<span style="color: #006600;">observe</span><span style="color: #66cc66;">&#40;</span>window , <span style="color: #3366CC;">&quot;load&quot;</span> , <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
		&lt;?= $this-&gt;setMode ?&gt;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">function</span> attachWebServiceObserver<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> url = <span style="color: #3366CC;">&quot;/index/async/&quot;</span>;
		<span style="color: #003366; font-weight: bold;">var</span> f = JSON.<span style="color: #006600;">stringify</span><span style="color: #66cc66;">&#40;</span>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'frm'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">serialize</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #003366; font-weight: bold;">new</span> Ajax.<span style="color: #006600;">Request</span><span style="color: #66cc66;">&#40;</span>url , <span style="color: #66cc66;">&#123;</span>
			method: <span style="color: #3366CC;">'post'</span>,
			parameters: <span style="color: #3366CC;">'fs='</span> + f,
			onComplete : doAjaxCall.<span style="color: #006600;">bindAsEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> attachPostBackObserver<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'frm'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">submit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//this ID comes from the RegistrationForm class</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> setMode<span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	$$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.tab'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>emt<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> emt.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span>background : <span style="color: #3366CC;">&quot;&quot;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
	e.<span style="color: #006600;">parentNode</span>.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>background :<span style="color: #3366CC;">&quot;#FFFF99&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>!FIRST_LOAD<span style="color: #66cc66;">&#41;</span>
		clearErrors<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>e.<span style="color: #006600;">parentNode</span>.<span style="color: #006600;">id</span> ==<span style="color: #3366CC;">&quot;wsTab&quot;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		Event.<span style="color: #006600;">observe</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bt'</span>, <span style="color: #3366CC;">'click'</span>, attachWebServiceObserver<span style="color: #66cc66;">&#41;</span>;
		Event.<span style="color: #006600;">stopObserving</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bt'</span>, <span style="color: #3366CC;">'click'</span>, attachPostBackObserver<span style="color: #66cc66;">&#41;</span>;
		$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bt'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">value</span> = <span style="color: #3366CC;">&quot;Submit Asynchronously&quot;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>e.<span style="color: #006600;">parentNode</span>.<span style="color: #006600;">id</span> ==<span style="color: #3366CC;">&quot;pbTab&quot;</span> <span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		Event.<span style="color: #006600;">stopObserving</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bt'</span>, <span style="color: #3366CC;">'click'</span>, attachWebServiceObserver<span style="color: #66cc66;">&#41;</span>;
		Event.<span style="color: #006600;">observe</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bt'</span>,<span style="color: #3366CC;">'click'</span>, attachPostBackObserver<span style="color: #66cc66;">&#41;</span>;
		$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bt'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">value</span> = <span style="color: #3366CC;">&quot;Submit With Postback&quot;</span>;
	<span style="color: #66cc66;">&#125;</span>
	FIRST_LOAD = <span style="color: #003366; font-weight: bold;">false</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> doAjaxCall<span style="color: #66cc66;">&#40;</span>tr<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	clearErrors<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>formIsValid<span style="color: #66cc66;">&#40;</span>tr.<span style="color: #006600;">responseJSON</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'frm'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">submit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	displayErrorMessages<span style="color: #66cc66;">&#40;</span>tr.<span style="color: #006600;">responseJSON</span><span style="color: #66cc66;">&#41;</span>;	
&nbsp;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> clearErrors<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'dvPostbackErrors'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">update</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'dvPostbackErrors'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">hide</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	$$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;.errormessage&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		Element.<span style="color: #006600;">remove</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	$$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;input.formText &quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		e.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>border : <span style="color: #3366CC;">&quot;1px solid #CCC&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
	$$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;select.formText &quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		e.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>border : <span style="color: #3366CC;">&quot;1px solid #CCC&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> formIsValid<span style="color: #66cc66;">&#40;</span>errorJson<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>isNaN<span style="color: #66cc66;">&#40;</span>errorJson<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> displayErrorMessages<span style="color: #66cc66;">&#40;</span>errorJson<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span>formElement <span style="color: #000066; font-weight: bold;">in</span> errorJson<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span> elementObj = <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;errorJson.&quot;</span>+formElement<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span>error <span style="color: #000066; font-weight: bold;">in</span> elementObj<span style="color: #66cc66;">&#41;</span>
			displayError<span style="color: #66cc66;">&#40;</span>formElement , <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;errorJson.&quot;</span>+formElement+<span style="color: #3366CC;">&quot;.&quot;</span>+error<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> displayError<span style="color: #66cc66;">&#40;</span>emt , msg<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> tpl = <span style="color: #3366CC;">&quot;&lt;span class='errormessage' &gt;&quot;</span>+msg+<span style="color: #3366CC;">&quot;&lt;/span&gt;&quot;</span>;
&nbsp;
	markInvalidTextbox<span style="color: #66cc66;">&#40;</span>emt<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	Element.<span style="color: #006600;">insert</span><span style="color: #66cc66;">&#40;</span>emt , <span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">&quot;after&quot;</span> : tpl <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> markInvalidTextbox<span style="color: #66cc66;">&#40;</span>emt<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	$<span style="color: #66cc66;">&#40;</span>emt<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>border: <span style="color: #3366CC;">&quot;2px solid red&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>In the version that you can download, I include the <a href="http://www.json.org/js.html">JSON.stringify class</a> (courtesy of Crawford) to serialize the form and send it the AsyncAction in my IndexController.</p>
<p>This architectural approach allows you to keep all the validation rules server-side, while allowing you to pick and choose when and how you postback. Because this example works with both postback and asynchronous calls, it's a little verbose. </p>
<p>Regardless, you can <a href="http://jon.lebensold.ca/wp-content/uploads/2008/AsyncValidationManager.zip">download the whole project and try it out for yourself</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://lebensold.net/development/decoupled-validation-with-zend_form-zend_validate-and-zend-mvc/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Zend MVC Screencasts: Zend Layout, Controller, View and Db</title>
		<link>http://lebensold.net/main/zend-mvc-screencasts-zend-layout-controller-view-and-db</link>
		<comments>http://lebensold.net/main/zend-mvc-screencasts-zend-layout-controller-view-and-db#comments</comments>
		<pubDate>Tue, 25 Mar 2008 21:34:36 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Screencasts]]></category>
		<category><![CDATA[Video Tutorials]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[main]]></category>

		<guid isPermaLink="false">http://jon.lebensold.ca/development/zend-mvc-screencasts-zend-layout-controller-view-and-db</guid>
		<description><![CDATA[I've put together a couple of Zend screencasts in an effort to explain some core concepts in Zend.  I've entitled the series of three videos Working With Zend MVC and it's available free on Idea22. I've also included a sample project to get you started which you can also download.
Topics Covered:

Zend Framework Folder Structure
Zend_View [...]]]></description>
			<content:encoded><![CDATA[<p>I've put together a couple of Zend screencasts in an effort to explain some core concepts in Zend.  I've entitled the series of three videos <a href="http://idea22.com/video/course/id/c200803256032246355/1">Working With Zend MVC</a> and it's available free on Idea22. I've also included <a href="http://idea22.com/public/attachments/c200803256032246355-StartArticleManager.zip">a sample project to get you started</a> which you can also download.</p>
<h3>Topics Covered:</h3>
<ul>
<li>Zend Framework Folder Structure</li>
<li>Zend_View and it's relationship to Zend_Controller</li>
<li>Zend_Controller and the management of form postbacks</li>
<li>Zend_Db Insert and Fetching rows</li>
<li>using Zend_Layout to build headers and footers using the Zend MVC model</li>
</ul>
<p>I've posted the first video here. Comments are much appreciated! <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="480" height="270" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="align" value="center" /><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="true" /><param name="flashVars" value="allowfullscreen=true&amp;logo=http://www.idea22.com/public/swf/i22.png&amp;autostart=false&amp;file=http://www.idea22.com/public/pv/2008032310856324169.flv&amp;image=http://www.idea22.com/public/pv/2008032310856324169.jpg" /><param name="src" value="http://www.idea22.com/public/swf/i22_flv.swf?vv=pv:2008032310856324169:480:270:e" /><embed type="application/x-shockwave-flash" width="480" height="270" src="http://www.idea22.com/public/swf/i22_flv.swf?vv=pv:2008032310856324169:480:270:e" flashvars="allowfullscreen=true&amp;logo=http://www.idea22.com/public/swf/i22.png&amp;autostart=false&amp;file=http://www.idea22.com/public/pv/2008032310856324169.flv&amp;image=http://www.idea22.com/public/pv/2008032310856324169.jpg" allowfullscreen="true" allowscriptaccess="always" align="center"></embed></object><br />
<script type="text/javascript"><!--
google_ad_client = "pub-6788178234188425";
/* 336x280, created 12/12/08 */
google_ad_slot = "2192755389";
google_ad_width = 336;
google_ad_height = 280;
// --></script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://lebensold.net/main/zend-mvc-screencasts-zend-layout-controller-view-and-db/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Demo: AJAX Autocompleter with Script.aculo.us and a Zend JSON web service</title>
		<link>http://lebensold.net/creative-stuff/demo-ajax-autocompleter-with-scriptaculous-and-a-zend-json-web-service</link>
		<comments>http://lebensold.net/creative-stuff/demo-ajax-autocompleter-with-scriptaculous-and-a-zend-json-web-service#comments</comments>
		<pubDate>Mon, 24 Mar 2008 09:16:52 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Creative stuff]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[free code]]></category>

		<guid isPermaLink="false">http://jon.lebensold.ca/creative-stuff/demo-ajax-autocompleter-with-scriptaculous-and-a-zend-json-web-service</guid>
		<description><![CDATA[Today I'm posting a sample project of a modification of a Script.aculo.us auto-completer script that will work with a Zend JSON web service. This example could easily be expanded by providing a richer data collection to the client (currently it's just a string array).
Here's a demo:





    

Implementing the code is really only [...]]]></description>
			<content:encoded><![CDATA[<p>Today I'm posting a sample project of a <a href="http://wiki.script.aculo.us/scriptaculous/show/Autocompleter.Local">modification of a Script.aculo.us auto-completer script</a> that will work with a Zend JSON web service. This example could easily be expanded by providing a richer data collection to the client (currently it's just a string array).</p>
<p>Here's a demo:</p>
<link rel="stylesheet" type="text/css" href="/demo/html/public/css/autocomplete.css" >
<p><script type="text/javascript" src="/demo/html/public/js/prototype.js"></script></p>
<p><script type="text/javascript" src="/demo/html/public/js/scriptaculous-1.8/scriptaculous.js"></script></p>
<p><script type="text/javascript" src="/demo/html/public/js/autocompleter.js"></script></p>
<input id="tbAutoCompleter" name="tbAutoCompleter" />
<div id="dvAutoCompleter" class="auto_complete" >    </div>
<p><script type="text/javascript" src="/demo/wp_ac.js"></script></p>
<p>Implementing the code is really only one line once you have the back end web service configured:</p>
<pre class="javascript"> <span style="color: #003366; font-weight: bold;">var</span> demoAutocompleter = <span style="color: #003366; font-weight: bold;">new</span> MyAjaxAutocompleter<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'tbAutoCompleter'</span>, <span style="color: #3366CC;">'dvAutoCompleter'</span>, <span style="color: #3366CC;">'/demo/html/Ajaxservice/index'</span>, <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>where 'tbAutoCompleter' corresponds to the textbox you wish to bind, 'dvAutoCompleter' is the control in question and '/demo/html/Ajaxservice/index' is the actual Web service giving you JSON data.</p>
<p>I'm still having a hard time POSTing to a Zend controller in a manner as elegant as you would find in ASP.NET's web services, however the code isn't really that bad. My AjaxServiceController looks like this:</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> AjaxserviceController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&gt;_helper-&gt;<span style="color: #006600;">contextSwitch</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
             -&gt;<span style="color: #006600;">addActionContext</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'index'</span>, <span style="color: #ff0000;">'json'</span><span style="color: #66cc66;">&#41;</span>
             -&gt;<span style="color: #006600;">initContext</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> indexAction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
       	<span style="color: #0000ff;">$this</span>-&gt;_helper-&gt;<span style="color: #006600;">viewRenderer</span>-&gt;<span style="color: #006600;">setNoRender</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
       	<span style="color: #0000ff;">$this</span>-&gt;_helper-&gt;<span style="color: #006600;">getHelper</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'layout'</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">disableLayout</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
       <span style="color: #808080; font-style: italic;">/* [Json response] */</span>
		 <span style="color: #0000ff;">$data</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;2008&quot;</span>,<span style="color: #ff0000;">&quot;3d&quot;</span>,<span style="color: #ff0000;">&quot;social&quot;</span>,<span style="color: #ff0000;">&quot;software&quot;</span>,<span style="color: #ff0000;">&quot;technology&quot;</span>,<span style="color: #ff0000;">&quot;tips&quot;</span>,
<span style="color: #ff0000;">&quot;tools&quot;</span>,<span style="color: #ff0000;">&quot;toread&quot;</span>,<span style="color: #ff0000;">&quot;travel&quot;</span>,<span style="color: #ff0000;">&quot;tutorial&quot;</span>,
<span style="color: #ff0000;">&quot;tutorials&quot;</span>,<span style="color: #ff0000;">&quot;tv&quot;</span>,<span style="color: #ff0000;">&quot;typography&quot;</span>, <span style="color: #ff0000;">&quot;wiki&quot;</span>,<span style="color: #ff0000;">&quot;windows&quot;</span>,<span style="color: #ff0000;">&quot;wishlist&quot;</span>,<span style="color: #ff0000;">&quot;wordpress&quot;</span>
,<span style="color: #ff0000;">&quot;work&quot;</span>,<span style="color: #ff0000;">&quot;writing&quot;</span>,<span style="color: #ff0000;">&quot;youtube&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		 <span style="color: #0000ff;">$responseData</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">array_isearch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getRequest</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">getParam</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'search'</span><span style="color: #66cc66;">&#41;</span> , <span style="color: #0000ff;">$data</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
       try <span style="color: #66cc66;">&#123;</span>
&nbsp;
           <span style="color: #0000ff;">$responseDataJsonEncoded</span> = Zend_Json::<span style="color: #006600;">encode</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$responseData</span><span style="color: #66cc66;">&#41;</span>;
           <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getResponse</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">setHeader</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Content-Type'</span>, <span style="color: #ff0000;">'application/json'</span><span style="color: #66cc66;">&#41;</span>
							   -&gt;<span style="color: #006600;">setBody</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$responseDataJsonEncoded</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
       <span style="color: #66cc66;">&#125;</span> catch<span style="color: #66cc66;">&#40;</span>Zend_Json_Exception <span style="color: #0000ff;">$e</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
           <span style="color: #808080; font-style: italic;">// handle and generate HTTP error code response, see below</span>
       <span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> array_isearch<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$str</span>, <span style="color: #0000ff;">$array</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    	<span style="color: #0000ff;">$returnArray</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$v</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/preg_match"><span style="color: #000066;">preg_match</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/$str/i'</span>,<span style="color: #0000ff;">$v</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            	<span style="color: #0000ff;">$returnArray</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$v</span>;
&nbsp;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$returnArray</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>There's not much here, mostly just disabling the Zend Layout, specifying the content and doing the search. The $data array could easily be replaced with a data source (like a collection from Zend_Db).</p>
<p><center><a style="border: 0px none #FFF;" href="/demo/SampleAutoCompletionZend15.zip"><img  style="border: 0px none #FFF;"src="/wp-content/uploads/2008/downloadSearchIcon.png" /><br />
<br />
Download Ajax Auto Completer Sample Zend Framework 1.5 project<br />
</a></center></p>
<p>The nice thing about web service AJAX controls is that they live independently of the application calling them, so you can essentially separate out your data layer in the same way that you would separate Zend_Db resources from a Zend_Controller.</p>
]]></content:encoded>
			<wfw:commentRss>http://lebensold.net/creative-stuff/demo-ajax-autocompleter-with-scriptaculous-and-a-zend-json-web-service/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Yahoo Using Zend?</title>
		<link>http://lebensold.net/main/yahoo-using-zend</link>
		<comments>http://lebensold.net/main/yahoo-using-zend#comments</comments>
		<pubDate>Wed, 05 Mar 2008 06:17:08 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[main]]></category>
		<category><![CDATA[random]]></category>

		<guid isPermaLink="false">http://jon.lebensold.ca/uncategorized/yahoo-using-zend</guid>
		<description><![CDATA[This morning, one of my friends emailed me this screen grab of Yahoo! Mail. Judging by the error, it looks like they're using Zend on IIS and they haven't properly setup their ErrorController so that it logs to something other than the page...

Here's the full error: 
Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller [...]]]></description>
			<content:encoded><![CDATA[<p>This morning, <a href="http://www.flickr.com/photos/project_summit">one of my friends</a> emailed me this screen grab of Yahoo! Mail. Judging by the error, it looks like they're using Zend on IIS and they haven't properly setup their ErrorController so that it logs to something other than the page...<br />
<img src="http://jon.lebensold.ca/wp-content/uploads/2008/03/YahooUsingZend.gif" /></p>
<p>Here's the full error: </p>
<p><code>Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (st)' in C:\wwwroot\library\Zend\Controller\Dispatcher\Standard.php:193 Stack trace: #0 C:\wwwroot\library\Zend\Controller\Front.php(911): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #1 C:\wwwroot\index.php(34): Zend_Controller_Front->dispatch() #2 {main} thrown in C:\wwwroot\library\Zend\Controller\Dispatcher\Standard.php on line 193</code></p>
]]></content:encoded>
			<wfw:commentRss>http://lebensold.net/main/yahoo-using-zend/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
