<?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>Juozas devBlog &#187; giorgio</title>
	<atom:link href="http://dev.juokaz.com/tag/giorgio/feed" rel="self" type="application/rss+xml" />
	<link>http://dev.juokaz.com</link>
	<description>Random ideas, scripts and facts</description>
	<lastBuildDate>Mon, 22 Mar 2010 10:48:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Zend Framework and Doctrine. Part 1</title>
		<link>http://dev.juokaz.com/php/zend-framework-and-doctrine-part-1</link>
		<comments>http://dev.juokaz.com/php/zend-framework-and-doctrine-part-1#comments</comments>
		<pubDate>Mon, 16 Nov 2009 14:09:07 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[doctrine]]></category>
		<category><![CDATA[domain model]]></category>
		<category><![CDATA[giorgio]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[symfony]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=845</guid>
		<description><![CDATA[If you are following twitter (you can find me there also) or any other social network, you might have noticed that there is a huge interest in Doctrine and Zend Framework integration. Since I&#8217;ve been using these libraries for quite a while now, so I&#8217;m going to explain some best practices and ways you can [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://dev.juokaz.com/wp-content/uploads/2009/11/doctrine-orm-php5.png" alt="doctrine-orm-php5" title="doctrine-orm-php5" width="191" height="53" class="size-full wp-image-847" style="float: left; margin-right: 5px;" />If you are following twitter (you can find <a href="http://www.twitter.com/juokaz">me</a> there also) or any other social network, you might have noticed that there is a huge interest in Doctrine and Zend Framework integration. Since I&#8217;ve been using these libraries for quite a while now, so I&#8217;m going to explain some best practices and ways you can do that.</p>
<h5>History</h5>
<p>To start with, in my opinion, <a href="http://framework.zend.com/">Zend Framework</a> never had a proper M from <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller">MVC</a>. It was quite common to use <a href="http://framework.zend.com/manual/en/zend.db.table.html">Zend_Db_Table</a> as base models class, but it was simply not practical. When you start dealing with relations and hierarchical data types it starts to get really tricky, because simply Zend_Db_Table doesn&#8217;t provide an extensive enough functionality.</p>
<p>So half a year ago Zend Framework developers started to look for better solutions. Quite obvious one was to start implementing their own <a href="http://en.wikipedia.org/wiki/Domain_model">Domain</a> layer. You can find quite a few different implementations of that in <a href="http://www.google.co.uk/search?hl=en&amp;source=hp&amp;q=zend+framework+domain+model&amp;btnG=Google+Search&amp;meta=&amp;aq=1&amp;oq=zend+framework+domain+">blogosphere</a>, and I chose this path also. However, after quite some time of developing my own code I realized that it&#8217;s simply not a right thing to do &#8211; I&#8217;m expected to deliver a product and I was &#8220;wasting&#8221; way too much time tweaking, testing, extending etc. my domain model code.</p>
<p>So about 3-4 months ago I completely switched to <a href="http://www.doctrine-project.org/">Doctrine</a>. After evaluating possible solutions I decided to stay with Doctrine for a long time. I don&#8217;t know any other solution coming, I definitely don&#8217;t want (mainly because I don&#8217;t have time) to invest on creating my own library and Doctrine is simply awesome when you get used to it. After all this time I can say that it was a right call &#8211; Doctrine is on a way to being officially supported in Zend Framework (<a href="http://www.symfony-project.org/">Symfony</a> has it right now) and with Doctrine 2.0 (you can see a short presentation of its new features right <a href="http://www.slideshare.net/jwage/doctrine-2-enterprise-persistence-layer-for-php">here</a>) it will be just a perfect tools combination. I would very much agree with <a href="http://giorgiosironi.blogspot.com/2009/11/whats-going-on-with-php-object.html">Giorgio</a> that:</p>
<blockquote><p>Thus, Doctrine 2 is going to become <strong>the first production-ready Orm for php</strong> and to be favored with seamless integration in both <a href="http://framework.zend.com/">Zend Framework</a> and <a href="http://www.symfony-project.org/">Symfony</a>.</p></blockquote>
<h5>Benefits</h5>
<p><img src="http://dev.juokaz.com/wp-content/uploads/2009/04/logo-zend-framework.jpg" alt="Zend framework" title="Zend framework" width="141" height="95" class="size-full wp-image-460" style="float: right; margin-left: 5px;" /> Some time ago I was actually involved in making a decision of choosing Doctrine over other libraries (main Zend Framework components) as part of my work, so here I&#8217;m going to outline some points which made that decision easier. </p>
<p><strong>Relations</strong>. I keep repeating this term every time I speak about Doctrine (or ORM&#8217;s in general), but for me it&#8217;s a key factor in writing code fast. Quick example: let&#8217;s image that our application consists of Order which has a list of Items where Item is Product and price, quantity information. Code for this situation:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$order</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Order<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$item</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> OrderItem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Product</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$product</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// product object from somewhere</span>
<span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quantity</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">price</span> <span style="color: #339933;">=</span> <span style="color:#800080;">9.99</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$order</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Items</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$order</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is how much code I need to write myself. I&#8217;m going to show more examples in the future posts, but because Doctrine is ORM (object relation mapper) all the hierarchical structure of your application data becomes very easy to work with.  </p>
<p>What is more, Doctrine can <strong>generate</strong> all the <strong>model classes</strong> by itself. Now that&#8217;s a treasure! Just specify database connection in the config file, open up a terminal and run:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">php doctrine-cli.php generate-models-db</pre></div></div>

<p>In a few seconds all schema information is retrieved from the actual database and all the classes are created. My biggest project so far with Doctrine was worth more than 10&#8242;000 lines of models code. I don&#8217;t know how to count the time it saved and despite a few minor bugs in Doctrine library it worked exactly as we wanted.</p>
<p>I can list things I like about it for hours probably, so I&#8217;m going to finish this list with the future I consider to be a very big time saver. This feature is dynamics of models: <strong>inheritance</strong> and <strong>behaviors</strong>. Inheritance makes your objects have different classes even though they could be a same table (with for example <a href="http://www.doctrine-project.org/documentation/manual/1_1/en/inheritance#column-aggregation">type_id</a>). All the work needed to handle the actual schema is done in background.</p>
<p>Behaviors work in the same way &#8211; one line of code and a model becomes physically undeletable (called <a href="http://www.doctrine-project.org/documentation/manual/1_1/en/behaviors#core-behaviors:softdelete">soft-delete</a>) or has a separate table to save revisions. Behaviors are very similar to decorators in Zend_Form for example &#8211; they are small classes which can be added to the main class to extend its functionality. So in the end your mode could have all sorts of different functionality depending on how you &#8220;decorate&#8221; it.</p>
<h5>What&#8217;s next?</h5>
<p>I only wrote a few examples how great Doctrine actually is today, but you can expect a lot more. Especially in Zend Framework code side: application.ini settings, application resource to setup auto-loading, script to generate Zend Framework compatible models, testing Doctrine models and much more. I&#8217;m also involved in a work group making Doctrine and Zend Framework integration possible so you can expect great things to come (first proposal <a href="http://framework.zend.com/wiki/display/ZFPROP/Doctrine+1+and+Zend_Tool+Integration+-+Benjamin+Eberlei">here</a>).</p>
<p><em>If you have been following me on twitter you might have noticed that I tweet a lot of interesting material on both Zend Framework, Doctrine and general PHP so I would recommend following me there or subscribing to the RSS </em><a href="http://dev.juokaz.com/feed"><em>feed</em></a><em>. I haven&#8217;t been writing for a while now, but I will try as hard as I can to change it and write much more frequently &#8211; during past months I tried a lot of cool stuff which I would love to talk about.</em></p>
<p><strong>All parts</strong>:</p>
<ol>
<li><a href="http://dev.juokaz.com/php/zend-framework-and-doctrine-part-1">Zend Framework and Doctrine. Part 1</a></li>
<li><a href="http://dev.juokaz.com/php/zend-framework-and-doctrine-part-2">Zend Framework and Doctrine. Part 2</a></li>
<li><a href="http://dev.juokaz.com/php/zend-framework-and-doctrine-part-3">Zend Framework and Doctrine. Part 3</a></li>
</ol>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/php/zend-framework-and-doctrine-part-1/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>
